Description in Unix, all processes except process 0 (that is, the pid=0 exchange process, swapper process) are created by other processes using the system call fork, where the process called fork creates a new process is the parent process, and the process that is created for it is the child process , so that processes other than process 0 have only one parent process, but a process can have more than one child process. The operating system kernel identifies the process with a process identifier (Identifier, or PID). Process 0 is a special process created at system boot time, when it calls fork to create a child process (that is, pid=1 process 1, also known as Init), process 0 is switched to the swap process (sometimes called an idle process), and Process 1 (the INIT process) is the ancestor of all the other processes in the system. Zombie process and orphan process when a child process finishes running (typically called exit, a fatal error at run time, or a terminating signal is received), the exit status (return value) of the child process is returned to the operating system, and the system notifies the parent process of the event that the child process was closed with a sigchld signal. The Process Control block (PCB) of the child process still resides in memory at this time. In general, when SIGCHLD is received, the parent process uses the wait system call to get the exit status of the child process, and then the kernel can release the PCB of the finished child process from memory, and if the parent process does not do so, the child process's PCB will always reside in memory and become a zombie process. The orphan process is a child process that is still running after the parent process is finished. In Unix-like systems, orphan processes are generally "adopted" by the Init process and become the children of the INIT process. In order to avoid a zombie process, the actual application generally takes the form of:-The parent process to the SIGCHLD signal processing function is set to sig_ign (ignore the signal);-fork two times and kill a sub-process, so that the two-level sub-process becomes an orphan process by init "adoption", cleanup. What does the so-called "zombie process" mean in UNIX programming, what happens when a zombie process is created, and how does it kill the zombie process? In the fork ()/execve () procedure, it is assumed that the parent process is still present at the end of the child process, while the parent process fork () does not have the SIGCHLD signal handler call Wait () or waitpid () waiting for the child process to end, and the signal is not explicitly ignored. The child process becomes a zombie process and does not end normally. Even the root kill-9 cannot kill the zombie process at this point (zombies are actually dead processes, you certainly can't kill a dead person). The remedy is to kill the parent process of the zombie process (the parent process of the zombie process must exist), the zombie process becomes the "orphan process", and adoptive to process Init,init 1th will always be responsible for cleaning up the zombie process. In a process called the exit of the, the process does not disappear immediately, leaving behind a data structure called the zombie process (Zombie). In the 5 states of the Linux process, the zombie process is a very special one, it has abandoned almost all memory space, no executable code, and can not be dispatched, just keep a position in the process list, record the process's exit status and other processes to collect (such as for the parent process), in addition, The zombie process no longer occupies any memory space. From this point of view, although the zombie process has a cool name, but its influence is far from the real zombie brothers, the real zombie always frightening, and the zombie process in addition to leaving some information for people to mourn, the system has no effect. The system calls exit, which is to get the process to exit, but only to turn a normal process into a zombie process and not destroy it completely. Although the zombie process has little effect on other processes, consumes no CPU time, the memory consumed is almost negligible, but there is still a feeling of discomfort in the mind. and the number of processes in the Linux system is limited, in some special cases, if there are too many zombie processes, it will also affect the generation of new processes. How to clean up the zombie process: Use Wait () and waitpid () system calls to clean up the zombie process, or to kill the parent process of the zombie process (the parent process of the zombie process must exist), and the zombie process as an "orphan process", which is always responsible for cleaning up the zombie process by adoptive to process Init,init 1th. The following metaphor describes the life of the process and makes it easier to see the stages of the zombie process: As a fork, a new process comes to the ground, but it is only a clone of the old process at this time. Then with the exec, the new process reborn, independent from home, began a career for the serving. The person has the sickness and death, the process also is the same, it can be natural to die, namely runs to main function the last "}", calmly away from us; it could be suicide. There are 2 ways to call the Exit function, one is to use return within the main function, either way, It can leave a suicide note and keep it in the return value; it can even be murdered, and other processes end his life in other ways. After the process dies, a zombie is left behind, and wait and Waitpid act as a corpse, pushing the zombies to cremation and ultimately to the invisible. This is the whole life of the process. The harm of the zombie process due to the end of the child process and the run of the parent process is an asynchronous process, that is, the parent process can never predict when the child process will end. So not because the parent process too busy to wait for the child process, or do not know when the child process ends, and lost the end of the child process state information? not. Because UNIX provides a mechanism to ensure that as long as the parent process wants to know the state information at the end of the child process, it canTo get. The mechanism is that when each process exits, the kernel frees all of the resources of the process, including open files, memory usage, and so on. However, it still retains certain information (including process ID, exit status of the termination status of the process, run time of the amount of the CPU taken by the Proces s, etc.) until the parent process is wait/waitpid to fetch the . but this causes the problem, if your process does not call Wait/waitpid, then the preserved piece of information will not be released, its process number will be occupied, However, the system can use a limited number of processes, if a large number of zombie processes, because there is no available process number, resulting in the system can not generate new processes. This is the threat to the zombie process should be avoided. Processing: * Explicitly ignoring the SIGCHLD signal refers to a code like this: signal (SIGCHLD, sig_ign); * Install SIGCHLD signal handle refers to code like this: Of course, it is not recommended to use signal (), should use Sigaction (). The zombie process holds a lot of information that is important to programmers and system administrators, first of all, how did this process die? Is it normal to quit, or is there an error, or is it forced to quit by other processes? Second, what is the total system CPU time and total user CPU time that this process consumes? The number of page faults that occurred and the number of received signals. This information is stored in the zombie process, imagine that without a zombie process, the process of the exit, all the information associated with it is immediately invisible, and when the programmer or the system administrator need to use, and then have to stare. So how do we collect this information and end these zombie processes? It depends on the waitpid call and the wait call. Both of these roles are the collection of information left by the zombie process and the process completely disappearing. pid_t Wait (int *status) 1) Once the process has called wait, it blocks itself immediately, and the wait automatically parses if a child process of the current process has exited, and if it finds a child process that has become a zombie, wait collects the information for that child process. And it is completely destroyed and returned; If no such child process is found, wait is blocked until one appears. The parameter status is used to hold some state when the collection process exits, which is a pointer to type int. But if we don't care about how this subprocess dies, we just want to get rid of this zombie process (and in most cases we'll think about it), we can set this parameter to NULL., like this: PID = Wait (NULL), if successful, wait returns the process ID of the child process being collected, and if the calling process does not have child processes, the call fails, and wait returns 1 while errno is set to Echild. Parameter status if the value of the parameter status is not null,wait, the state of the process exit is fetched and entered, which is an integer value (int) indicating whether the child process exits normally or is abnormally terminated (a process can also be signaled by other processes). And the return value at the normal end, or the end of the signal. Since this information is stored in a different bits of an integer, it can be cumbersome to read in a regular way, and people have designed a special macro to do the work, so let's take a look at the two most common ones: wifexited (status) This macro is used to indicate whether a child process is exiting normally, and if so, it returns a value other than 0. (Note that, although the name is the same, the parameter status here differs from the wait-only argument-pointer status to an integer, but the integer that the pointer points to, remember not to confuse it.) Wexitstatus (status) When Wifexited returns a value other than 0, we can use this macro to extract the return value of the child process, and if the child process calls exit (5) Exits, Wexitstatus (status) returns 5 if the child process calls exit (7), Wexitstatus (status) will return 7. Note that if the process does not exit normally, that is, wifexited returns 0, this value is meaningless. The basic state of the process in Linux is: 1, Execution (R) state: The CPU is executing, that is, the process is consuming CPU. 2, Ready (W) Status: All conditions that the process already has to perform, are waiting to allocate CPU processing time slices. 3, Stop (S) Status: The process cannot use the CPU. Three functions: fork (); function: Create a new process. (Fork () <0[error], fork () ==0[child process], fork () >0[parent process]wait (); function: Really end process (corpse). exec (); function: executes an external program. Orphan process Orphan process: A parent process exits, and one or more of its sub-processes are still in progress, then those child processes will be orphaned, and the orphan process will be adopted by the INIT process (process number 1) and the Init process completes the state collection. Zombie resources can waste resources, and the orphan process will not. Link:http://www.cnblogs.com/mydomain/archive/2010/10/24/1859826.htmlhttp://gengning938.blog.163.com/blog/static/ 12822538120116188816625/
Zombie process and orphan process under Linux