Linux kernel source Learning zombie Process

Source: Internet
Author: User

Orphan process and zombie process

After the normal child processes fork their parent process, they establish a parent-child relationship.

When the child process is terminated, it notifies the parent process and empties the memory it occupies and leaves its own exit message in the kernel (exit code, 0 if it runs smoothly, or an integer that is >0 if there is an error or an exception condition). In this message, it explains why the process exited. When the parent process learns that the child process is terminated, it is the responsibility to use the wait system call for that child process. This wait function takes out the exit information of the child process from the kernel and empties the space occupied by the information in kernel. This is the normal general situation.

If the parent process is older than the child process end, the child process becomes an orphan (orphand) process. The orphan process is passed on to the Init process, and the Init process becomes the parent process of the process. The INIT process calls the wait function when the child process is terminated.

Of course, a bad program can also completely cause the exit information of the child process to remain in the kernel (the parent process does not call the wait function on the child process), in which case the child process becomes a zombie (zombie) process. When a large number of zombie processes accumulate, the memory space is squeezed out.

What is the meaning of zombie presence?

The zombie state is a certain state of the process in the system, that is, the time it sends a message to its parent process to be reclaimed after it exits, but the parent process can be processed in a very short time under normal circumstances, and the zombie status of the zombie process will last for a long time.

The purpose of setting the zombie state for a process is to maintain the child process information so that the parent process gets it at a later time. This information includes the process ID, termination status, and resource utilization information (CPU time, memory usage, and so on) of the child process.

What is the zombie process that is created and left in memory?

What remains in memory is the process descriptor for processes, which is the data structure used to describe the process, that is, the task_struct instance of the process.

Why did the child zombie process of the parent process go away after the parent process died ?

If a process terminates, and the process has a child process that is in zombie state, then the parent process ID of all its zombie child processes is reset to 1 (init process). The init process that inherits these child processes cleans them up (the INIT process will wait for them, thus removing the zombie state). Because the zombie process becomes an orphan process after the parent process dies, the INT process is responsible for the operation of the body of the orphan process, which should be performed at intervals of time. This is also a remedy for the zombie process.

How to prevent the production of zombie processes?

1) Try to wait for the child process

1. Wait child process in parent process after fork

2. Find a way to capture the message sent by the kernel at the end of the child process and wait in the function that handles the message

void sig_chld (int signo) {

pid_t pid;

int stat;

PID = Wait (&stat);

printf ("Child%d exit\n", PID);

Return

}

int main () {

Signal (SIGCHLD, &SIG_CHLD);

}

The above procedure first registers a signal processing function (SIG_CHLD) with the SIGCHLD signal in the main function, and then when the child process exits, the kernel submits a sigchld when it is captured by the main process and enters the signal processing function sig_chld, and then Sig_ When wait is called in chld, the exited child process can be cleaned up. This way the retired child process will not become a zombie process.

The following blog mentions this wait method is flawed, the correct method should be called Waitpid, what is the difference between the two? I don't know . http://www.cnblogs.com/yuxingfirst/p/3165407.html

2) call Fork two times. The following procedure implements this.

The idea is simple, first Fork The original process to get son process, and then Fork son process of the child process, and then let the son process die, so, because the son process first quit, grandson process is Init take over, actually out of the relationship with the original parent process, it will not be dead (this time the parent process of grandson process is equivalent to Init process) http://yejun8500.blog.163.com/blog/static/463360020104555814706/


Linux kernel source Learning zombie Process

Related Article

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.