3

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 .

mukesh
  • 726
  • 1
  • 11
  • 27

3 Answers3

3

Start with dup. You really need to search a bit harder. There is plenty of material on this.

Duck
  • 26,924
  • 5
  • 64
  • 92
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
1

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,

eyalm
  • 3,366
  • 19
  • 21