lighthou lighthou - 5 months ago 37
C Question

Running multiple forked processes and constantly reading their standard out, while printing to their standard in

I'm looking to run X amount of processes that I'm able to iterate through in order to run programs where there's a master and 'slaves' that take the masters orders and return a string.

I'm writing in C. I'm wondering how I'd be able to set up pipes and forking between there processes to read from standard in and out. I'm currently able to have them work one at a time until the are killed, but I would like to simply read one line then move to the next process. Any help?

Answer Source

Generally, the common strategy for this sort of programming is to set up an event loop. You would set up pipes and connect them to stdin and stdout of your program. You don't specify what language you're using. In C, you would create two pipes, one for reading, and one for writing. Then you would fork. After the fork, in the child, you close stdin and stdout, and you use the dup2 system call to copy one end of the pipe filedescriptors to the child.

In the parent, you connect each process to an event loop, which lets you know when one of your FDs is ready for reading or writing.

Take a look at these class notes for discussion of using pipes and dup2.

Here's an introduction to libevent, one of the common event loops for C.

For other languages you'll do something similar. For example for Python, take a look at the asyncio support for subprocesses.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download