开发者

Redirecting input and output of a child process in C

开发者 https://www.devze.com 2023-03-04 19:42 出处:网络
I want to write a c program in which i create multiple child processes and redirect their inputs and outputs to different file descriptors .I goo开发者_开发知识库gled a lot but couldn\'t find relevant

I want to write a c program in which i create multiple child processes and redirect their inputs and outputs to different file descriptors .I goo开发者_开发知识库gled a lot but couldn't find relevant results. Please help .


Start with 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; each child process will inherit the current set of file descriptors from the parent when it is exec-ed. 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.


My favorite is forkpty. This function forks a child and give you a file descriptor to its stdin/stdout. You can use exec after forking,

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号