mukesh
mukesh

Reputation: 716

Redirecting input and output of a child process in C

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 .

Upvotes: 3

Views: 3180

Answers (3)

eyalm
eyalm

Reputation: 3366

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,

Upvotes: 1

Duck
Duck

Reputation: 27542

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

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81674

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.

Upvotes: 2

Related Questions