Detailed zombie process and orphan process

Source: Internet
Author: User
Tags sleep terminates
First, zombie process: If a process at the end of its own to recycle all the resources allocated to it, the system will not produce the so-called zombie process. Then we say what information remains after a process terminates.
Why do I need to keep this information after the termination? There are many ways to terminate a process, and some information is useful for the parent process and the kernel after the process terminates, such as the ID number of the process, the exit state of the process, the CPU time that the process is running, and so on. Therefore, when the process terminates, it reclaims all the memory allocated to it by the kernel, closes all the files it opens, and so on, but retains the minimal information available for the parent process to use.
The parent process can use system tuning such as wait/waitpid to clean up the child process and do some finishing work. 1, the concept of Linux system zombie process and the reality of zombies (although I have not seen) similar, although dead, but because no one to their body, but also around.
Zombies refer to those processes that have been terminated, but still retain some information and wait for their parent process to bury them. 2, the resulting process when the parent process calls fork to create a subprocess, the child runs until it terminates, and it is immediately removed from memory, but the process descriptor remains in memory (the process descriptor occupies very little memory space). The state of the subprocess becomes Exit_zombie and sends a SIGCHLD signal to the parent process at which time the parent process should call the wait () system call to obtain the exit status of the child process and other information. After the wait call, the zombie process is completely removed from memory.
So a zombie exists at a time when its termination to the parent process calls wait functions such as the gap, usually disappears quickly, but if the programming is unreasonable, the parent process does not call wait and other system calls to collect the zombie process, then these processes will always exist in memory.                                                                                                                                        
  3. Generate a Zombie process 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #inclu
  de<sys/types.h> 5 int Main () 6 {7 pid_t id=fork (); 8 if (id<0) 9 {perror ("Fork ");
 return 0;
 The else if (id==0) ("I am a child,pid:%d,ppid:%d\n", Getpid (), Getppid ());
 Exit (0);
 else {printf ("I am a child,pid:%d,ppid:%d\n", Getpid (), Getppid ());
 Sleep (100);
 return 0; 25} [write here Picture description] (http://img.blog.csdn.net/20170505231115837?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvd2vpemhlbmdibw==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast) from the above, we can see that there is a zombie process in the system.

But if we wait for the parent process to wake up and exit, we look at the system process again and see that the zombie process just disappeared. This is why. The parent process has not been dead for its child process, so the zombie process disappears after the parent process exits. Does the parent process clean up the subprocess when it exits? It's not .... The real reason is that after the parent process dies, all of its subprocess are passed to the INIT process, the INIT process becomes the new process of the zombie process, and the INIT process periodically invokes the wait system call to clear its zombie child.
Therefore, you will find that in the example above, after the parent process dies, the zombie process disappears, in fact, the INIT process for its corpse. 4, zombie process damage Although the zombie process has freed up memory, but it also takes up the memory resources of the system (memory is the memory that records process-related information (such as: Process ID (Process ID is limited), process state, etc., all others are freed, but if there are more zombie processes in the system, it wastes
A large amount of memory, if many, can seriously affect the performance of the server. 5. Avoidance of Zombie processes (1) The parent process waits for the child process to end through functions such as wait and waitpid, which causes the parent process to hang (2) If the parent process is busy, you can use the sigThe NAL function installs handler for SIGCHLD, because the parent process receives the signal at the end of the child process and can invoke the wait recycle in handler (3) If the parent process does not care about when the subprocess ends, you can use signal (SIGCHLD, sig_ign) Notifies the kernel that it is not interested in the end of the child process, then the kernel reclaims it and no longer sends a signal to the parent process (4) There are some tricks, that is, fork two times, the parent process fork a subprocess, and then continue to work, and the child process exits after fork a grandchild process Then the sun process is taken over by Init, and after the sun process is over, Init will reclaim it.
However, the recycling of child processes has to do itself. I. The orphan process: 1. Concept a parent process exits, and one or more of its child processes are still running, and those child processes will become orphan processes.
The orphan process is adopted by the INIT process (process number 1) and the Init process completes the state collection of them.
Note: Zombie processes can lead to waste of resources while orphans do not.                                                                                                                                        
  2. Generate an orphan process 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #includ
  e<sys/types.h> 5 int Main () 6 {7 pid_t id=fork ();
 8 if (id<0) 9 {perror ("fork");
 return 0;
 The else if (id==0) {printf ("I am a child,pid:%d,ppid:%d\n", Getpid (), Getppid ());
 Sleep (10);
 printf ("I am a child,pid:%d,ppid:%d\n", Getpid (), Getppid ());  19 Else   {sleep (1);//Ensure that the original subprocess first prints printf ("I am a father,pid:%d,ppid:%d\n", Getpid (), Getppid ());
 Exit (0);//The parent process rolls in before subprocess return 0;





 25}

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.