[英]pipe causing processes to freeze
The program report
launches two accessed
processes. 程序
report
启动两个accessed
过程。 Report basically feeds accessed
a list of filenames and accessed
prints if they have been accessed in x days. 基本上,报表Feed
accessed
文件名列表和accessed
打印文件(如果在x天之内被访问过)。
However, my implementation is causing accessed
to freeze somehow. 但是,我的实现导致
accessed
以某种方式冻结。 After running reports
, nothing gets printed. 运行
reports
,什么都不会打印。 When I run ps
, I can see two accessed
programs hanging around, not dying. 当我运行
ps
,我可以看到有两个accessed
程序在运行,没有死。
At first, I thought the method of reading from stdin was wrong in accessed
, but I manually piped some filenames to it cat filenames.txt | ./accessed
起初,我以为从标准输入读取的方法是错误的
accessed
,但我手动管道部分文件名给它cat filenames.txt | ./accessed
cat filenames.txt | ./accessed
, and it works. cat filenames.txt | ./accessed
,它可以工作。 So report
program must be wrong. 因此,
report
程序一定是错误的。
I attached gdb to the frozen accessed
processes and it seems that it is frozen at the while loop getline
. 我将gdb附加到冻结的
accessed
进程中,似乎它在while循环getline
冻结了。 So I changed the while loop to a single getline
statement and it suddenly works. 因此,我将while循环更改为单个
getline
语句,它突然起作用了。 However, I need to read stdin
until EOF. 但是,我需要读
stdin
直到EOF。 Any help on the possible sources of errors is very much appreciated. 非常感谢对可能的错误源的任何帮助。 This is causing me much headache.
这让我很头疼。
Schematics: 示意图:
--------
| |--------> Access1 ---> print stuff out
|report|
|______|--------> Access2 ----> print stuff out
Each process closes its own input pipe (after dup2'ing the read end). 每个进程都关闭自己的输入管道(在读取端dup2之后)。
However, they leave the other process's input pipe open. 但是,它们使另一个进程的输入管道保持打开状态。 Each one will never see an EOF, even after the parent process closes it, since there's the possibility that the other child process might decide to send data to it.
即使在父进程关闭后,每个人也永远不会看到EOF,因为存在另一个子进程可能决定向其发送数据的可能性。
The children should close each others' pipes (or be started without inheriting the other process's pipes). 子进程应关闭彼此的管道(或在不继承其他进程的管道的情况下启动)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.