Orphan process and zombie process

Source: Internet
Author: User

Orphan process:

The process of a child process being adopted by the INIT process as a result of the parent process exiting is the orphan process, which is the parent of the orphan process being changed to the INIT process, which reclaims its kernel space resources after the orphan process exits.

Zombie Process:

The process has exited, but its father process has not yet reclaimed the kernel resources of the process as a zombie process, that is, the process in the kernel space of the PCB (Process Control block) is not released.

1. Here is an example of an orphan process, in which the parent process exits first, and then the child process outputs its own father process number again:

#include <stdio.h>#include <unistd.h>int main(){    int pid;    if((pid = fork()) == -1)        perror("fork Err");    else if(!pid){        printf("Child: pid : %d, ppid : %d \n", getpid(), getppid());        sleep(2);        printf("Child: pid : %d, ppid : %d \n", getpid(), getppid());}    else{        sleep(1);        printf("Parent: pid : %d, ppid : %d \n", getpid(), getppid());}    return 0;}

The results of the operation are as follows:

As a result, the parent process of the child process has changed before and after.

2. The following is an example of a zombie process in which the parent process exits but does not process the child process, and then the parent process calls the system function to list the current foreground process information, with the following code:

#include <stdio.h>#include <unistd.h>#include <stdlib.h>int main(){    int pid;    if((pid = fork()) == -1)        perror("fork err");    else if(!pid){        exit(0);}    sleep(1);    system("ps");    return 0;}

The results of the operation are as follows:

The process of the red box is a zombie state.

How to clear the zombie process:
1.改写父进程,在子进程死后要为它收尸。具体做法是接管SIGCHLD信号。子进程死后,会发送SIGCHLD信号给父进程,父进程收到此信号后,执行waitpid()函数为子进程收尸。2.把父进程杀掉。父进程死后,僵尸进程成为"孤儿进程",过继给1号进程init,init始终会负责清理僵尸进程.它产生的所有僵尸进程也跟着消失。
Wait () and Waitpid ()

Wait ():

The parent process that calls the wait () function reclaims the kernel process resources for the child process after it has been blocked waiting for any one of the process's child processes to end.

Waitpid ():

The Waitpid () function can be used to wait for the specified child process (the child process that specifies the PID) to end. The function declaration is as follows:

Process resources can be viewed through the/proc/{pid}/maps file.

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.