c - waiting for ALL children with waitpid option __WALL -


I want a simple and elegant solution for the parenting process of all my children I have the following code, but it does not work That's how I want it and I do not know why anyone can explain to me, why?

  pid_t pid; For (int i = 0; i & lt; N; i ++) {if ((pid = fork ()) == -1) Return -1; // error, return from parent and if (pid == 0) {printf ("child:% d \ n", pid); Exit (0); // I have completed the baby} and if (PID & gt; 0) {waiting pump (-1, null, __WALL); // Waiting for all children, clone or non-stop printf ("I am a parent. All children have finished. \ N"); Return 0; // Everything is OK, returning from parents}}   

My output is:

  Children: 0 I'm a parent to end all children Have children: 1 I am a parent. All children have been eliminated Children: 2 and so on   

I want this:

  Children: 0 Children: 1. . I am a parent All the children have ended   

Why does the __WALL option not work for me?

In your code, the basics in each loop are making a child-process; The hair-process is printing its message, while the basic process is waiting for the exit of this specific child process, which can be collected before the loop is re-entered. Working like this, i + 1 - will not be made until the hair process is finished.

To see if you expected, your code might look like this (although the __ wall option is not being used): < pre> pid_t pid; For (int i = 0; i & lt; N; i ++) {if ((pid = fork ()) == -1) Return -1; // error, when returning from parent (pid == 0) {printf ("child:% d \ n", i); Exit (0); // baby i have accomplished}} (i = 0; i

In this case, the original process will start to collect the first N processes for its withdrawal status. When this waiting ends, the proper message will be printed.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -