23,217
社区成员




#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include <sys/wait.h> // wait() requires this header file
int main()
{
int fd[2];
char buf[128] = {0};
if(pipe(fd) < 0 )
{
printf("creat pipe failed \n");
exit(-1);
}
int pid;
pid = fork();
if( pid < 0)
{
printf("creat son failed\n");
}
else if(pid > 0)
{
int status;
printf("pid = %d, into father\n", pid);
close(fd[0]);
write(fd[1], "write from father", strlen("write from father"));
wait(&status); // call wait in calling process
}
else
{
sleep(1); // no matter how long it sleeps
printf("pid = %d, into son\n", pid);
close(fd[1]);
read(fd[0],buf,128);
printf("read from father : %s\n", buf);
}
return 0;
}