我使用一个孩子和一个进程之间的FIFO通信编写了一些简单的代码。但是,我看不到父母的孩子的信息。子级始终可以通过标准输入将消息写给其父级。我错过了哪一点?
import os
pipe_name = 'pipe_test'
def parent( ):
pipein = open(pipe_name, 'r')
while True:
#time.sleep(1)
line = pipein.readline()[:-1]
print ('Parent got "%s"' % line)
def child( ):
pipeout = os.open(pipe_name, os.O_WRONLY)
while True:
childInf = "I'm child my pid = " + str(os.getpid())
message = input("Message to parent > ")
overall = childInf + " " + message
os.write(pipeout, overall.encode('utf-8'))
pipe_path = os.getcwd() + "/" + pipe_name
if os.path.exists(pipe_path):
os.unlink(pipe_path)
os.mkfifo(pipe_name)
pid = os.fork()
if pid != 0:
parent()
else:
child()