Orphan process and zombie process

Source: Internet
Author: User
Tags terminates

Orphan process and zombie process

I. Definition: What is orphan process and zombie process

Zombie Process: A child process exits if its parent process has not yet called wait () or waitpid (). This child process is the zombie process.

Orphan process: One parent process exits, and one or more of its child processes are still running, then those child processes will become orphans. The orphan process will be adopted by the INIT process (process number 1) and the Init process completes the state collection for them.

The zombie process will lead to a waste of resources, while the orphans will not.

Child process lasts 10 seconds of Zombie State (Exit_zombie)
------------------------------------------------------
#include
#include
#include
#include

Main ()
{
pid_t pid;
PID = fork ();
if (PID < 0)
printf ("Error occurred!/n");
else if (PID = = 0) {
printf ("Hi father! I ' m a zombie/n ");
Exit (0); (1)
}
else {
Sleep (10);
Wait (NULL); (2)
}
}

(1) Send sigchild signal to parent process
(2) parent process processing Sigchild signal

When you execute exit (0), you determine your state based on the state of its parent process:
If the parent process has exited (no wait), then the child process will become an orphan process adoptive to the INIT process
If its parent process has not exited, and there is no wait (), then the process will send a sigchild signal to the parent process and enter the zombie state to wait for the parent process to corpse. If the parent process has not executed wait (), then the child process will continue to be in zombie state, and if the parent process does not execute wait, the zombie process is passed to the Init process after death, and the init process processes the zombie process and eventually dies.

Child process will become an orphan process
------------------------------------------------------
#include
#include
#include
#include

Main ()
{
pid_t pid;
PID = fork ();
if (PID < 0)
printf ("Error occurred!/n");
else if (PID = = 0) {
Sleep (6);
printf ("I ' m a orphan/n");
Exit (0);
}
else {
Sleep (1);
printf ("Children bye!/n");
}
}

#./a.out
Children bye!
# I ' m a orphan
(Enter the # after carriage return)
#


Second, what is the harm:
The zombie process consumes system resources and, if many, can severely affect server performance
The orphan process does not take up system resources and is ultimately managed by the Init process to release it from the init process.
Processing Flow:
As long as Daddy waits for Wait (sys/wait.h) son, son will become Ghost Zombie (zombie), Unix default dad always want to see the state of his son after death (in order to revenge)
If daddy's going to see you better than son
The son will be adopted by init (id = 1), and the final result is that zombie son thoroughly Goodbye, system resources released
Else
{
Son's zombie will always exist, system resources occupy ...
If Daddy dead
The son will be adopted by init (id = 1), and the final result is that zombie son thoroughly Goodbye, system resources released

else similar sons zombie more and more, the system is dying!!!
}

signal (SIGCHLD, sig_ign); Ignoring the SIGCHLD signal, which is often used as a technique for concurrent server performance
Because concurrent servers often fork many child processes, the end of a child process requires
The server process goes to wait to clean up resources. If this signal is handled in a way that is set to
Ignore, allowing the kernel to transfer the zombie process to the INIT process to handle, eliminating the
A large number of zombie processes occupy system resources. (Linux only)

Third, how to prevent the zombie process
First understand how to create a zombie process:
1, the child process after the end of the parent process issued a SIGCHLD signal, the parent process by default ignored it
2. The parent process does not call the wait () or waitpid () function to wait for the end of the child process
The first method: Capture the SIGCHLD signal and call the wait function inside the signal processing function
The UNIX Network programming code for Richard Steven

Int
Main (int argc, char **argv)
{
...
Signal (SIGCHLD, SIG_CHLD);
for (;
}
...
}

void
SIG_CHLD (int signo)
{
pid_t pid;
int stat;

while (PID = Waitpid ( -1, &stat, Wnohang)) >; 0)
printf ("Child%d terminated/n", PID);
Return
}

The second method: two times fork (): reproduced
In the advanced programming for Unix environment, there is a very clear description of this in section 8.6.

Instance
Recall 8. Discussion of the zombie process in section 5. If a process wants F o r k a child process, but does not require it to wait
Child process terminates, and does not want the child process to be in a zombie state until the parent process terminates, the trick to implement this requirement is to call F o r k
Twice. Program 8-5 implements this.
Call S L e E p in the second child process to ensure that the first child process is terminated when printing the parent process I d. After F o r K,
Both parent and child processes can continue--we cannot predict which one will be executed first. If you do not make the second child process sleep, the
After F o r K, it may execute first than its parent process, so it prints the parent process I d will be the parent process that created it, not
I-n i-t process (process ID 1).

#include
#include
#include "Ourhdr.h"

int main (void)
{
pid_t pid;

if ((PID = fork ()) < 0)
Err_sys ("fork Error");
else if (PID = = 0) {/* First child */
if ((PID = fork ()) < 0)
Err_sys ("fork Error");
else if (PID > 0)
Exit (0); /* Parent from second fork = = First child */

/* We ' re the second child; Our parent becomes init as soon
As our real parent calls exit () in the statement above.
Here's where we ' d continue executing, knowing that's when
We ' re done, Init would reap our status. */

Sleep (2);
printf ("Second child, parent PID =%d/n", Getppid ());
Exit (0);
}

if (Waitpid (PID, NULL, 0)! = PID)/* Wait for first child */
Err_sys ("Waitpid error");

/* We ' re the parent (the original process); We continue executing,
Knowing that we ' re not the parent of the second. */

Exit (0);
}
Avoid zombie process by forking twice

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/metasearch/archive/2008/05/31/2498853.aspx

Orphan process and zombie process

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.