与孩子的孩子互动

问题描述 投票:0回答:1

我需要直接与我用stdin产生的过程的stdoutsubprocess进行交互。我可以这样做:

proc = subprocess.Popen("/bin/bash", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())

但是,只要进程分叉,我就无法再与之交互。我有这个测试程序:

// child.c
#include <unistd.h>
int main() {
    execve("/bin/bash", NULL, NULL);
}

我编译的:

gcc -o child child.c

当我在child调用中使用/bin/bash而不是Popen尝试上面的python代码时,我得到一个broken pipe错误。 我尝试了什么: 由于python创建的文件描述符在默认情况下是不可继承的,所以我尝试改变它:

inr, inw = os.pipe()
outr, outw = os.pipe()
os.set_inheritable(inr, True)
os.set_inheritable(inw, True)
os.set_inheritable(outr, True)
os.set_inheritable(outw, True)

proc = subprocess.Popen("./child", stdin=inr, stdout=outw, stderr=outw)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())

os.close(inr)
os.close(inw)
os.close(outr)
os.close(outw)

但我仍然得到同样的错误。我认为这应该是一件容易的事,我可能会遗漏一些东西。任何帮助表示赞赏。 [编辑]在我编辑我的帖子之前,我使用test而不是child作为我的测试c可执行文件的名称。我了解到它确实是我的PATH中的一个程序,位于/usr/bin/test。 将可执行文件名更改为child成功解决了我的问题。经过数小时的试验和错误,我知道答案会变得简单...... 非常感谢Barmar指出这一点!

python linux subprocess file-descriptor
1个回答
1
投票

当你打电话给test时,你打电话给/bin/test,而不是你的程序。

重命名您的C程序,使其不与标准命令冲突,或使用路径名访问它,因此它不会搜索$PATH

proc = subprocess.Popen("./test", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
© www.soinside.com 2019 - 2024. All rights reserved.