Search code examples
cmultiprocessingsignalsfork

Assign new tasks to forked() processes


I need to create a multi-process program that:

  1. Creates 5 processes using fork();
  2. Sends stuff to do the the child processes using pipes
  3. When a child process completes its stuff, it should receive new stuff to do from parent process until all stuff are completed.

Right now my idea is to wait() on completed child tasks (and it exits) and then to create a new child process so I always have a maximum of 5 processes.

Is there a way to "re-use" the already existing process? Maybe "signaling" something? Cannot find it on Google.

Using C.


Solution

  • I solved my problem in this way:

    • Children write the result of their calculation on a pipe A (this pipe is not blocking). Then they wait their next input on a pipe B (this pipe is blocking).

    • Parent loops on all children trying to read something from pipe A (this pipe is not blocking so parent goes on if child didn't send anything). If pipe A contains a result, the parent sends another task to that child on pipe B.

    There is a pipe A and a pipe B for each child.