0

I'm making a little program using a lot of forks. The first parent have to wait for everychildren. For only one child it's easy, there is the child-end signal (SIGCLHD). But what if my first child ends before the last child? My main program runs before the end of everychilds and I need the main program wait for children.

Each child ends with an execution of another program, that explain why I can't synchronize with something like semaphores.

// Working pretty good
execvp(
    c->command_name, /* program to execute */
    c->argv          /* argv of program to exécuter */
);

Here is my "fork-structure":

main
 |
 |------
 |     |
 |     |
 |     |------
 |     |     |
 |    EOE    |
 |           |
 |          EOE
 |
 |
EOE

Legend:

  • EOE means "End Of Execution
  • The line top-bot is the timeline
  • Each new step to the left is a new child.
  • Each vertical bar represents the execution of a bar

Thanks!

4

2 に答える 2

0

試しましたか

// for each child spawned:
children[i] = fork(); // children is an array of pid_t
if (children[i] == 0) {
    execvp(
        c->command_name, /* programme à exécuter */
        c->argv          /* argv du programme à exécuter */
    );

// parent process keeps doing its thing, and then
for (i = 0; i < NUMBER_OF_CHILDREN; i++) {
    waitpid(children[i], NULL, 0);
}
于 2013-05-25T22:56:13.637 に答える