2.7 Process exit, orphan process, zombie process

table of Contents

1. Process exit

2. Orphan process

3. Zombie process


1. Process exit

#include <stdlib.h>
void exit(int status);

#include <unistd.h>
void _exit(int status);
    status参数:是进程退出时的一个状态信息。父进程回收子进程资源的时候可以获取到。

Procedure to test the exit function:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    printf("hello\n");   //有\n
    printf("world");     //无\n
    exit(0);
}

operation result:

Procedure to test the _exit function:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    printf("hello\n");   //有\n
    printf("world");     //无\n
    _exit(0);
}

operation result:

Why is there a difference between the two output results?

The first printf statement with "\n" will refresh the buffer. So both programs output hello.

The second printf statement does not have "\n". From the first figure, we can see that the exit function will refresh the buffer, the _exit function will not refresh the buffer, so the first program will also output world, but the second program will not.


2. Orphan process

program:

#include <unistd.h>
#include <stdio.h>

int main()
{
    pid_t pid=fork();
    if(pid>0)
    {
        printf("I am a parent process! pid=%d ppid=%d\n",getpid(),getppid());
    }
    else if(pid==0)
    {
        sleep(1);
        printf("I am a child process! pid=%d ppid=%d\n",getpid(),getppid());
    }
    for(int i=0;i<3;i++)
    {
        printf("i=%d pid=%d\n",i,pid);
    }
    return 0;
}

operation result:

 

When an executable program is executed in the terminal, it will switch to the program execution by default, which is why the output of the program is displayed in the terminal.

Among them, the id 12145 of the parent process of the parent process in the program is the current terminal, which can be verified:

Because the terminal is the parent process of the parent process in the program, the terminal knows when the parent process in the program ends, and switches back to the terminal after the parent process in the program ends, so it will display the red frame in the previous figure. part. But the terminal does not know whether the parent process has a child process that has finished running, so the content of the child process in the program will be displayed later. Why is the output of the child process also displayed in the current terminal? Because after fork, the file descriptor table of the child process and the parent process are the same (shared when reading), representing the same terminal.

The orphan process will be adopted by the init process whose process ID is 1, and init will complete the resource release of the orphan process. The orphan process is harmless.


3. Zombie process

program:

#include <unistd.h>
#include <stdio.h>

int main()
{
    pid_t pid=fork();
    if(pid>0)
    {
        while(1)
        {
            printf("I am a parent process! pid=%d ppid=%d\n",getpid(),getppid());
            sleep(5);
        } 
    }
    else if(pid==0)
    {
        printf("I am a child process! pid=%d ppid=%d\n",getpid(),getppid());
    }
    for(int i=0;i<3;i++)
    {
        printf("i=%d pid=%d\n",i,getpid());
    }
    return 0;
}

operation result:

Use ps aux to view the process information, as shown below:

It can be seen from the figure that the child process 12216 is a zombie process. Because the child process has finished running, the parent process has not reclaimed the kernel area data of the child process, and the parent process has been running. Zombie processes have been occupying system resources, so we must find a solution.

One of the solutions:

Let the parent process finish running:

Because after the parent process ends, the parent process of the child process (ie, the zombie process) will become the init process, and the init process will reclaim the resources of the zombie process.

In fact, sometimes the program has to be running all the time and cannot be killed with ctrl+c.

Solution two:

The wait function or waitpid function is called in the parent process to recycle the resources of the child process. This method will be discussed later.


Reference: Niuke.com C++ high-paying job search project "Linux High Concurrency Server Development" 2.7 process exit, orphan process, zombie process

Exclusive offer link:

https://www.nowcoder.com/courses/cover/live/504?coupon=AvTPnSG

Guess you like

Origin blog.csdn.net/m0_38062470/article/details/113797732