c - Fork and waiting for the childs outside the pid check -


I want to make two parallel forks and wait for them to end.

 for  (int i = 0; i & lt; 2; i ++) {pid_t pid = fork (); If (pid & lt; 0) {fprintf (stderr, "can not fork!"); Exhaust (EXIT_FAILURE); } Else if (pid == 0) {switch (i) {case 0: // First exhaust exhaust (EXIT_SUCCESS); break; Case 1: // Another child exhaust (EXIT_SUCCESS); break; } break; } Else {// parent}} The problem is that the main program ends before the child, if I add wait (0) to the part of the other parent, then every process , So that they do not work at the same time one by one. I think I can just wait () or waitpid () after the loop, but he is not working either. I thought that after the fork, the parent continues normally, why not wait When is the loop out and if-and structure?  

Sorry for the upset and thanks in advance.

If you add wait (NULL) inside the other original part Parents

If you add wait (NULL) after the loop, the parent process will wait

if you want That all children have the same processes running at the same time, then you will need a second loop, where the basics will be every to collect the extraction status of the hair process It is important to use wait () .

In your case, there are 2 children - Processes, so 2 wait () The system call is enough to add the loop will be like this (I have the printf So that you can see it on your stdout :

 for  (I = 0; i  ++) {Wait (tap); Printf ("My child has not died% d. \", I); }    

Comments

Popular posts from this blog

c++ - ERROR: cannot open source file x11\xlib.h -

c - What are pthread cancelation points used for? -

c# - Show a huge number of data in DevExpress GridView -