我想做一个代理进程来打开真正的进程。
就像我将 linux 重命名espeak
为espeak_real
,我的应用程序重命名为espeak
.
espeak
打开espeak_real
,我得到输出。
我希望能够:
- 将它的 STDIN 打印到控制台
- 将它的 STDIN 打印到另一个进程的 STDIN
- 打印第二个进程的 STDOUT
我正在尝试在 C 中执行此操作(我猜也可以使用原始 bash)。
我不完全明白你在做什么,但它似乎是fork
,exec
和应该做pipe
的组合。dup2
app
可以pipe
用来获取一对文件描述符,通过管道连接(写入一个的内容是从另一个读取的)。
然后它可以分叉,并且孩子可以exec
app_real
。
但是在fork
and之间exec
,dup2
可用于将您想要的任何文件描述符更改为 0,1 和 2(但关闭真正的 0,1,2)。
短代码示例:
int pipe_fds[2];
pipe(pipe_fds);
if (fork()==0) {
// Child
close(fds[1]); // This side is for the parent only
close(0); // Close original stdin before dup2
dup2(fds[0],0); // Now one side of the pipe is the child's stdin
close(fds[0]); // No need to have it open twice
exec(...);
} else {
// Parent
close(fds[0]); // This side is for the child only
write(fds[1],data,len); // This data goes to the child
}