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):
我必须在UNIX中模拟C程序中的shell行为,记住一件重要的事情:父进程接收所有命令并将它们发送给子进程。然后,孩子执行收到的命令,然后将输出发送给父亲。我的想法是,我从父级和我的主子fork()接收命令,这样他自己的孩子就可以执行命令并将输出发送到fifo管道。问题是我无法打破while循环(打印正确的输出):
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
我意识到fifo的大小是可变的,但是第二个孩子在终止时必须发送一个EOF,这样当读取到达它时,返回0.但这似乎不会发生。如果需要,请向我询问更多代码部分。谢谢
1 个解决方案
#1
0
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.
因为父级使用O_RDWR打开了fifo的末尾,所以始终有一个向fifo写入可写文件描述符的进程。 EOF仅在fifo没有可写fds时发生,因此父进程保持这样一个可写fd打开,它永远不会得到EOF。
Try opening with O_RDONLY
in the parent instead.
尝试在父级中使用O_RDONLY打开。
#1
0
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.
因为父级使用O_RDWR打开了fifo的末尾,所以始终有一个向fifo写入可写文件描述符的进程。 EOF仅在fifo没有可写fds时发生,因此父进程保持这样一个可写fd打开,它永远不会得到EOF。
Try opening with O_RDONLY
in the parent instead.
尝试在父级中使用O_RDONLY打开。