The program can read all the data from the pipe. However, the program just stop. It cannot continue to process. I think it stop in the child process.
//I created two pipe before fork (fd[0] and fd[1]).
//child process
if(pid == 0){
close(fd[a][1]);
buf[6];
int i;
while ((i = read(fd[a][0], buf, 6)) > 0) {
printf("%s", buf);
}
close(fd[a][0]);
exit(0);
}
//parent process
write(fd[a][1], "12", 2);
write(fd[a][1], "14", 2);
write(fd[a][1], "15", 2);
write(fd[b][1], "12", 2);
write(fd[b][1], "14", 2);
write(fd[b][1], "15", 2);
printf("done!\n");
close(fd[0][1]);
close(fd[1][1]);
wait(NULL);
printf("Really done!!!\n");
... // The program cannot run after wait(NULL);
--output--
121415done
You are issuing
readin a while loop, effectively blocking yourself until something goes wrong. If you need to read6characters, this is how you would write the child process: