简体   繁体   中英

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. 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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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