[Apue] orphan process and zombie Process

Source: Internet
Author: User

Basic concepts:

In Unix/Linux, a child process is created by a parent process and a new process is created by a child process. The end of the Child process and the running of the parent process are asynchronous processes, that is, the parent process can never predict when the child process ends. After a process terminates its work, its parent process needs to call wait () or waitpid () to obtain the termination status of the child process.

Orphan process: if a parent process exits and one or more child processes are still running, those child processes will become orphan processes. The orphan process will be adopted by the INIT process (process number 1) and collected by the INIT process.

Zombie Process: A process uses fork to create a child process. If the child process exits and the parent process does not call wait or waitpid to obtain the status information of the child process, the sub-process descriptor is still stored in the system. This process is called a zombie process.

Problems and hazards

UNIX provides a mechanism to ensure that the parent process can obtain the state information when the child process ends. This mechanism means that when each process exits, the kernel releases all resources of the process, including opened files and occupied memory. However, some information (including the process ID, the termination status of the process, and the running time of the amount of CPU time taken by the process) is retained for the process ). It is released only when the parent process is obtained through wait/waitpid. But this leads to problems,If the process does not call wait/waitpid,The information retained will not be released, and the process number will be occupied all the time, but the process number that the system can use is limited. If a large number of zombie processes are generated, the system cannot generate a new process because there is no available process number. this is the danger of botnets and should be avoided. The orphan process is a process without a parent process, and the orphan process is the responsibility of the INIT process.,

Whenever an orphan process occurs, the kernel sets the parent process of the orphan process as init, And the INIT process cyclically wait () Its exited child process. Therefore, the orphan process does not have any harm.

Orphan process and zombie process example

Orphan process:

 

# Include <stdio. h> # include <stdlib. h> # include <errno. h> # include <unistd. h> int main () {pid_t PID; // create a process pid = fork (); // creation failure if (PID <0) {perror ("fork error: "); exit (1);} // sub-process if (pid = 0) {printf (" I am the child process. \ n "); // output process ID and parent process ID printf (" PID: % d \ tppid: % d \ n ", getpid (), getppid ()); printf ("I will sleep five seconds. \ n "); // sleep for 5 S, ensure that the parent process exits sleep (5) First; printf (" PID: % d \ tppid: % d \ n ", getpid (), getppid (); printf ("child process is exited. \ n ");} // The parent process else {printf (" I am Father process. \ n "); // The parent process sleeps for 1 s to ensure that the child process output process ID sleep (1); printf (" Father process is exited. \ n ") ;}return 0 ;}

 

Zombie process:

# Include <stdio. h> # include <unistd. h> # include <errno. h> # include <stdlib. h> int main () {pid_t PID; pid = fork (); If (PID <0) {perror ("fork error:"); exit (1 );} else if (pid = 0) {printf ("I am child process. I am exiting. \ n "); exit (0);} printf (" I am Father process. I will sleep two seconds \ n "); // wait for the sub-process to exit sleep first (2); // output process information system (" PS-O PID, ppid, state, tty, command "); printf (" Father process is exiting. \ n "); Return 0 ;}

 

 

Solution to zombie process:

1) signal Mechanism

When the child process exits, it sends a sigchld signal to the parent process, and the parent process processes the sigchld signal. Call wait in the signal processing function to process botnets.

2) fork twice

The child process becomes an orphan process, and its parent process becomes the INIT process. Through the INIT process, the zombie process can be processed.

 

Refer:

Http://www.cnblogs.com/Anker/p/3271773.html

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.