#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
int main()
{
int fd[2];
char rbuf[4]={0};
char* wbuf[256];
pid_t pid;
int childexit=0;
int i;
int cmd;
if (pipe(fd) == -1)
{
perror("fd");
exit(0);
}
if ((pid = fork()) == 0)
{
printf("\n");
close(fd[1]);
sleep(1);
while(!childexit)
{
read(fd[0], rbuf, 4);
cmd=atoi(rbuf);
if (cmd == 0)
{
printf("child du over,exitd\n");
childexit=1;
}
else if (handle(cmd) != 0)
return;
sleep(1);
}
close(fd[0]);
exit(0);
}
else
{
close(fd[0]);
wbuf[0]="111";
wbuf[1]="050";
wbuf[2]="211";
wbuf[3]="000";
for(i=0; i<4; i++)
write(fd[1], wbuf[i], 4);
close(fd[1]);
}
}
int handle(int cmd)
{
if((cmd<0) || (cmd>256))
{
printf("child:cmddddd\n");
return -1;
}
printf("child:from pa %d\n", cmd);
return 0;
}
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
int main()
{
int fd[2];
char rbuf[4]={0};
char* wbuf[256];
pid_t pid;
int childexit=0;
int i;
int cmd;
if (pipe(fd) == -1)
{
perror("fd");
exit(0);
}
if ((pid = fork()) == 0)
{
printf("\n");
close(fd[1]);
sleep(1);
while(!childexit)
{
read(fd[0], rbuf, 4);
cmd=atoi(rbuf);
if (cmd == 0)
{
printf("child du over,exitd\n");
childexit=1;
}
else if (handle(cmd) != 0)
return;
sleep(1);
}
close(fd[0]);
exit(0);
}
else
{
close(fd[0]);
wbuf[0]="111";
wbuf[1]="050";
wbuf[2]="211";
wbuf[3]="000";
for(i=0; i<4; i++)
write(fd[1], wbuf[i], 4);
close(fd[1]);
}
}
int handle(int cmd)
{
if((cmd<0) || (cmd>256))
{
printf("child:cmddddd\n");
return -1;
}
printf("child:from pa %d\n", cmd);
return 0;
}
623

被折叠的 条评论
为什么被折叠?



