//child process
char buf[20];
read(fd[0][0], buf, 20);
printf("%s", buf);
//parent process
write(fd[0][1], "12", 20);
write(fd[0][1], "14", 20);
write(fd[0][1], "15", 20);
--output--
12
//then the program just exit. It cannot print out 14 and 15.
May I know that how can solve this problem? Can I make the child process waiting until it really read data from pipe?
I edited my program. And it can read all the data. However, the program just stop. It cannot continue to process. I think that it stop in the child process.
//child process
buf[6];
int i;
while ((i = read(fd[0][0], buf, 6)) > 0) {
printf("%s", buf);
}
//parent process
write(fd[0][1], "12", 2);
write(fd[0][1], "14", 2);
write(fd[0][1], "15", 2);
printf("done!\n");
--output--
121415done
//The program just stopped in child process.
Edit: Works bad if write size is > write data tho. Garbage at end.
readssize_t read(int fildes, void *buf, size_t nbyte);writessize_t write(int fildes, const void *buf, size_t nbyte);