Zombie Process Processing Methods

Source: Internet
Author: User
Tags signal handler

Zombie Process: The essence is the process descriptor task_struct;

Maintain the state of the child process, including the child process ID, the terminating state, and the resource utilization of the process (CPU time, memory)

int Wait (INT*STAT_LOC): Returns the PID that terminates the subprocess after success, returns 1, and sets the errno

1, wait calls the blocking process until any of the one by one child processes are terminated, returns immediately, and the PID that returns the value for this terminating process

2, if wait calls, there are multiple word process termination, wait () Select any one by one sub-processes, through the PID identification

3, if the calling process does not have child processes, the call fails, at which time wait returns-1, while errno is set to Echild. ,

pid_t waitpid (pid_t pid,int * status,int options);

Waitpid ( -1,null,0) equivalent to wait (null)

Pid==-1: Waiting for any child process

Pid==0, waits for the process group ID and any child process with the same group ID as the calling process

Pid>0, waiting for the process ID to be a PID child process

Pid<-1, waits for any process with the Process group ID equal to the PID absolute value

If many of the sub-processes have been terminated before Waitpid (), then randomly select one and return immediately

If we don't want to block when we call Waitpid, we can set the options

Options: Including 0,wnohang,wutraced

If we take Wnohang, when we call Waitpid, we will not jam. Direct return to PID or 0

So the extra features include:

1. Waitpid provides a wait () non-clogging version, Waitpid (Child_pid,null,wnohang), which is used to see if the specified subprocess has been terminated, and if not, it is not blocked, but returns 0.

2. Waitpid can wait for a particular process to terminate

During zombie process processing:

The wait () function is insufficient: because the signal is not queued, so when our parent process calls wait (), other sub-processes (assuming 4) terminate the sending of the SIGCHLD signal is blocked (the same signal in the signal processing function is blocked)

After the signal processing function is finished, only one SIGCHLD is submitted to the parent process, and the wait function is called again, leaving 3 zombie processes

If before wait (), 5 sub-processes are generated before the signal handler executes, it is already a zombie process and only randomly chooses one, leaving 4 zombie processes

1. This can lead to uncertainty due to the problem.

2. The zombie process cannot handle completely

The above problem can be resolved by a while loop call to wait, but because wait is blocked, it may cause too long wait waits

Based on wait cannot be non-blocking call, so we can only choose Waitpid, this is the key reason

void sig_chld (int signo) {

pid_t pid;

int stat;

while (Pid=waitpid ( -1;&stat,wnohang)) >0)

{}

Return

}

Zombie Process Processing Methods

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.