简体   繁体   English

在C中重定向子进程的输入和输出

[英]Redirecting input and output of a child process in C

I want to write ac program in which i create multiple child processes and redirect their inputs and outputs to different file descriptors .I googled a lot but couldn't find relevant results. 我想编写一个ac程序,我在其中创建多个子进程并将其输入和输出重定向到不同的文件描述符。我搜索了很多但找不到相关结果。 Please help . 请帮忙 。

Start with dup . dup开始。 You really need to search a bit harder. 你真的需要更努力地搜索。 There is plenty of material on this. 这有很多材料。

The answer depends on your operating system. 答案取决于您的操作系统。 On UNIX-like systems, you use dup() and dup2() to copy file descriptors around; 在类UNIX系统上,使用dup()dup2()来复制文件描述符; each child process will inherit the current set of file descriptors from the parent when it is exec -ed. 每个子进程在exec时将从父进程继承当前的文件描述符集。 So typically you fork the child process, set file descriptors 0, 1, and 2 to whatever you want them to be, and then exec() the actual child program. 所以通常你fork子进程,将文件描述符0,1和2设置为你想要的任何东西,然后exec()实际的子程序。

My favorite is forkpty . 我最喜欢的是forkpty This function forks a child and give you a file descriptor to its stdin/stdout. 此函数会分叉子项并为其stdin / stdout提供文件描述符。 You can use exec after forking, 你可以在分叉后使用exec,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM