简体   繁体   中英

Break a while loop that reads from fifo channel

I have to simulate the shell behavior within a C program in UNIX, having in mind one important thing: the parent process receives all the commands and sends them to the child. The child then executes the command received, and then sends the output to the father. The idea is that I received the command from the parent and my primary child fork() so that his own child can execvp the command and send the output to the fifo pipe. The problem is I cannot break the while loop(which prints the correct output):

mkfifo("output",S_IFIFO|0644);
while(read(fifod2,&c,1))
      printf("%c",c);

I realise the fifo is of variable size, but the second child must have sent an EOF when terminated, so that when the read reaches it, returns 0. But that doesn't seem to happen. Ask me for more portions of code, if needed. Thank you

Because the parent opened its end of the fifo with O_RDWR , there is a process with a writable file descriptor to the fifo at all times. The EOF only occurs when there are no writable fds to the fifo, and so since the parent process is keeping such a writable fd open, it'll never get an EOF.

Try opening with O_RDONLY in the parent instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM