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 googled a lot but couldn't find relevant results. Please help .
Asked
Active
Viewed 3,030 times
3
-
See http://stackoverflow.com/questions/584868/rerouting-stdin-and-stdout-from-c – jweyrich May 09 '11 at 14:20
-
@jweyrich your link is irrelevant to this question – Haozhun Mar 09 '12 at 08:57
3 Answers
2
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.

Ernest Friedman-Hill
- 80,601
- 10
- 150
- 186
-
Since the phrase "file descriptors" was used, then I guess it must be UNIX/Linux. For completeness: on Windows use CreateProcess and look at the last three members of the STARTUPINFO struct. – cdarke May 09 '11 at 14:48