Operating system-Programs do not recycle child processes in a specific order

Source: Internet
Author: User
Tags terminates

EG1:

waitpid1.c//2015-08-26 Lucifer zhang//Using the Waitpid function to reap zombie children in no//particular order.    #include "csapp.h" #define N 2int Main () {int status, I;    pid_t pid; /* Parent creates N children */for (i = 0; i < N; i++)//line:ecf:waitpid1:forif (PID = Fork                          ()) = = 0)/* Child *///line:ecf:waitpid1:fork exit (100+i);  Line:ecf:waitpid1:exit/* Parent reaps N Children in no particular order */while (PID = Waitpid ( -1, &status, 0) > 0) {//line:ecf:waitpid1:waitpidif wifexited (status)//line:ecf:waitpid1:wifexited print     F ("Child%d terminated normally with exit status=%d\n", PID, Wexitstatus (status));    Line:ecf:waitpid1:wexitstatuselse printf ("Child%d terminated abnormally\n", PID); }/* The only normal termination is if there be no more children */if (errno! = echild)/ /line:ecf:waitpid1:errnounix_error ("WaitPID error "); Exit (0);}
Line 15th, the parent process creates n child processes, and on line 16th, each child process exits with a unique exit status. In line 19th, the parent process uses WAITPID as the test condition for the while loop, waiting for all of its child processes to terminate because the first parameter is-1, so the call to Waitpid is blocked, knowing that any one of the child processes is terminated. When each child process terminates, the call to Waitpid is returned, and the return value is a non-0 PID of the child process. Line 20th checks the exit status of the child process. If the child process terminates normally, and this is terminated by calling the Exit function, then the parent process extracts the exit State and outputs it to stdout.

When all the child processes are reclaimed, the call to Waitpid returns 1, and the errno is set to Echild. Line 28th checks that the WAITPID function terminates normally, otherwise it outputs an error message.

Test:


Note that the program does not recycle child processes in a specific order. The order in which the child processes are reclaimed is the properties of this particular computer. On another system, even once again on the same system, two sub-processes may be recycled in reverse order . The above test results also illustrate this point. This is an example of a non-deterministic (nondeterministic) behavior that makes it difficult to infer concurrency.

The following example makes a simple change, eliminates the uncertainty, and reclaims the child processes in the same order in which they were created by the parent process.


EG2:

waitpid1.c//2015-08-26 Lucifer zhang//Using waitpid to reap zombie children in the order they were created. #includ    E "Csapp.h" #define N 2int Main () {int status, I;    pid_t pid; /* Parent creates N children */for (i = 0; i < N; i++)//line:ecf:waitpid1:forif (PID = Fork                          ()) = = 0)/* Child *///line:ecf:waitpid1:fork exit (100+i);  Line:ecf:waitpid1:exit/* Parent reaps N Children in no particular order */while (PID = Waitpid ( -1, &status, 0) > 0) {//line:ecf:waitpid1:waitpidif wifexited (status)//line:ecf:waitpid1:wifexited print     F ("Child%d terminated normally with exit status=%d\n", PID, Wexitstatus (status));    Line:ecf:waitpid1:wexitstatuselse printf ("Child%d terminated abnormally\n", PID); }/* The only normal termination is if there be no more children */if (errno! = echild)/ /line:ecf:waitpid1:errnounix_error ("Waitpid error "); Exit (0);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Operating system-Programs do not recycle child processes in a specific order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.