您的代码将无法正常工作。
/proc/pid/fd/0是
/dev/pts/6文件的链接。
$ echo’foobar’> / dev / pts / 6
$ echo’foobar’> / proc / pid / fd / 0
由于这两个命令均写入终端。此输入将转到终端,而不是进程。
如果stdin最初是管道,它将起作用。
例如,
test.py是:
#!/usr/bin/pythonimport os, sysif __name__ == "__main__": print("Try commands below") print("$ echo 'foobar' > /proc/{0}/fd/0".format(os.getpid())) while True: print("read :: [" + sys.stdin.readline() + "]") pass运行为:
$ (while [ 1 ]; do sleep 1; done) | python test.py
现在从另一个终端写一些东西到
/proc/pid/fd/0它会
test.py



