For while (PID = Waitpid ( -1, &stat, Wnohang) > 0) I don't understand.

Source: Internet
Author: User
while (PID = Waitpid ( -1, &stat, Wnohang)) > 0)

Need to write to the signal processing function, if there are 10 sub-processes

As long as the parent process is able to receive the last signal, the resulting zombie process from the previous loss can be recovered.

The parent process is able to receive the last signal, even if the parent process starts processing the signal function at the end of the child process to the 5th.

If the processing time is too long, the remaining 5 will also end up in the process of processing function execution. At this point, due to the signal processing mechanism will cause the remaining 5 transmitted signals will be blocked, of course, because the signal is not queued

So the pending set will only have one signal (when the block set corresponds to 1 o'clock, if a corresponding signal arrives, the pending set to 1 indicates a pending signal). At this point, the child process has been all over, when the signal function is completed, the kernel open block signal set, then. The signal that was blocked before is passed in.

Next, recycle the remaining zombie processes again.

The following shows the code, showing the number of executions of the signal function and the count of the number of recoveries. You can see the difference.

void hander (int n) {    static int i = 0;    i++;    printf ("    i:%d\n", i);    while (waitpid (0, NULL, Wnohang) > 0)   {       static int j = 0;    j + +;    printf ("j:%d\n", j);   }       }

  

Waitpid ( -1, &stat, Wnohang) recycles the subprocess, recycles the successful one, returns the PID of the subprocess, and if the Wnohang is specified, it will no longer block the recycle, at which point the return rule is. If there are child processes but no end (no zombie process), Waitpid returns 0, if there is a zombie process, recycle a zombie process, return to the zombie process PID, so you have to use the loop to deal with. Recycle the zombie process

For while (PID = Waitpid ( -1, &stat, Wnohang) > 0) I don't understand.

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.