我正在编写一个外壳程序,并尝试实现process substitution。fork继承所有filedescriptor,分配的内存等。我知道execve也应该保留这种信息,所以只要O_CLOEXEC标志没有设置,就应该保留每个打开的filedescriptor。
我尝试了一个简单的python脚本:
fd.py:
#!/usr/bin/env python3
import sys, os
if __name__ == "__main__":
if len(sys.argv) == 1:
new_fd = open("the_content_file", "w+")
print("father table : ", os.listdir("/dev/fd"))
if os.fork() == 0:
os.execve("/PATH/OF/SCRIPT/fd.py", ["fd", "content"], os.environ)
else:
print("child table : ", os.listdir("/dev/fd"))
pass
作为输出,我得到:
father table : ['0', '1', '2', '3', '4']
child table : ['0', '1', '2', '3']
在fork之后,我保留了相同的fd表,但是每当我在可执行文件上使用execve时,我丢失了所有文件,并默认打开了fd。为什么打开的fd消失了?谢谢
https://stackoverflow.com/questions/56690216
复制相似问题