问题
两个子进程分别将内容输入到管道中,父进程从中读出内容然后输出
代码
#include <unistd.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <sys/wait.h>#include <strings.h>int main(void){pid_t p1,p2;char buf[1024];int fd[2],a1,a2;char* str1="Child process 1 is sending a message!\n";char* str2="Child process 2 is sending a message!\n";if (pipe(fd)==-1){printf("Create a pipe failed\n");return 0;}p1=fork();if(p1==0){close(fd[0]);write(fd[1],str1,strlen(str1));wait(NULL);close(fd[1]);return 0;}waitpid(p1,&a1,0);p2=fork();if(p2==0){close(fd[0]);write(fd[1],str2,strlen(str2));wait(NULL);close(fd[1]);return 0;}waitpid(p2,&a2,0);close(fd[1]);int len=read(fd[0],buf,sizeof(buf));write(STDOUT_FILENO,buf,len);close(fd[0]);return 0;}
pipe问题学习自/qq_42914528/article/details/8408