我有一个管道; reader端连接到子进程的STDIN,父进程有writer端。有没有办法让父母检测其子女是否实际读取(系统调用read(2)
)管道?
可以修改父母的逻辑和孩子的环境。子进程是一个黑盒子;它可以是任何东西,所以我们不能修改孩子的内在逻辑。我们不能向管道发送“探测器”,例如在其上写一个字符并等待它被搅动。
让我通过(Python)代码片段来澄清它:
reader, writer = os.pipe()
pid = os.fork()
if pid == 0:
os.close(writer)
os.dup2(reader, sys.stdin.fileno())
os.execvp('anycommand', ['anycommand', 'arg'])
# executing anycommand which read()s STDIN somewhen and unknown times
else:
os.close(reader)
while True:
while not is_pipe_being_read(pid, reader, writer):
print "not reading... doing parent businnes"
time.sleep(1)
print "reading! stop doing parent businnes, feed data depending on parent businnes so far"
os.write(writer, "some data")