[Linux] Zombie Process + Orphan Process + Daemon Process

Three special process states

Zombie process

  • basic concept

Zombie process is when the child process ends before the parent process , and the parent process does not recycle the child process , the resources of the child process have not been released , at this time the child process will become a zombie process

  • harm

A special mechanism in Unix : When each process exits, the memory releases all the resources of the process, but still retains certain information for it (this information includes: process number , exit status, running time, etc.). The parent process will only be released when it is obtained through wait / waitpid.

Since the end of the child process and conduct parent process is an asynchronous process, if the parent process has not calling wait / waitpid to get the information already retained the child process exits, which led to the retention of the child process information can not be released , a direct result of The process number of the child process cannot be released , but the process number of the operating system 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.

  • Avoid method
  1. The parent process is suspended through wait/waitpid and other functions, and has been waiting for the end of the child process.
  2. If the parent process is busy and cannot be suspended, you can use the signal function to bind a signal processing function pointer handler with SIGCHLD, and call wait/waitpid in the signal processing function to recover the residual information of the child process
  3. If the parent process does not care when the child process ends, you can use signal (SIGHLD, SIG_IGN) to notify the kernel. The parent process does not care about the status of the child process. After the child process ends, the kernel will reclaim the residual information of the child process. Send the SIGHLD signal to the parent process
  4. Tip: fork twice. The parent process forks a child process A, then the parent process continues its work, the child process A forks out of the grandchild process B, and then the child process A exits immediately, the grandchild process B will be taken over by the init process, and the grandchild process B will be recycled by init after the end , Do not need to be recycled through the parent process, but the recycling of the child process A still needs the parent process to handle
  • Approach

What should we do if a zombie process is accidentally generated?

Solution: Kill the parent process of the zombie process ( the parent process of the zombie process must exist), thereby turning the zombie process into an orphan process , and adopting it to the first process init, the init process will always be responsible for cleaning up the zombie process

  • View method

We run the following code under Linux to generate a zombie process

Program explanation: A child process is fork in the main thread. After the child process executes the print statement, the exit is executed to exit the thread, but the parent process has been sleeping and cannot recover the child process information, causing the child process to become a zombie process

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
    
    
    pid_t child = fork();
    if(child < 0)
        perror("fork process fail!\n");
    else if(child == 0)
    {
    
    
        printf("fork succ,this run in child process, Child PID = %d\n",getpid());
        exit(0);
    }
    else
    {
    
    
        printf("this run in parent process, Parent PID = %d\n",getpid());
        sleep(600);
    }                                                                                                                                                                     

    return 0;
}

After running: It is found that an a.out process with a process status of Z has been generated , which is a zombie process

image.png

image.png

ps command: display information about all active processes

Usage: ps aux | grep process name


Orphan process

  • definition

An orphan process refers to a type of process that continues to run after its parent process is completed or terminated.

These orphan processes are adopted by the init process, and the init process completes the state collection work for them

  • Introduction

In Unix-like operating systems, the child process is created by the parent process.

The end of the child process and the running of the parent process are an asynchronous process, that is, the parent process can never predict when the child process ends.

When a parent process exits and one or more of its child processes are still running, then these child processes will become orphan processes, adopted to the init process (process number is 1), and the init process will complete the status collection work for them .

Whenever an orphan process appears, the kernel will set the parent process of the orphan process as the init process, and init will call wait() cyclically to handle the subsequent exit of these orphan processes

  • Simulate the orphan process

Program explanation: The main process forks a child process and exits immediately after sleeping for 1ms. The child process sleeps for 6ms, causing the parent process to exit before the child process, and the child process becomes an orphan process

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

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");
    }
} 

There is no harm in the orphan process


Daemon

  • definition

A daemon is a special process that runs in the background to perform specific system tasks. Many daemons are started when the system boots, and continue to run until the system shuts down. Others only start when needed, and automatically end after completing the task.

Translation of adult words: In the Linux system, we will find that many processes have already started running when the system is started, also known as services, which are also what we call daemon processes.

Guardian features:

  1. The process leaves the terminal (reason: in order to prevent the information in the running process from being displayed on the terminal)
  2. Background process

The life cycle of the daemon is from the start of the system to the end of the system

  • View

Use ps axj to see which daemons are running in the current system

  • a means not only lists the current user process, but also lists the processes of all other users
  • x means not only lists the processes with the controlling terminal, but also lists the processes without the controlling terminal
  • j means to list information related to job control

image.png

The following characteristics can be seen from the daemon in the figure:

  • The daemons are all started by the super user (UID is 0)
  • No control terminal (TTY?)
  • The terminal process group ID is -1 (TPGID represents the terminal process group)

Guess you like

Origin blog.csdn.net/weixin_45437022/article/details/109660825