On the zombie process and the orphan process

1. Zombie process

Definition: A zombie process is a child process that ends before the parent process. At this time, the child process is not recycled and the resources occupied by the child process are released. At this time, the child process is called a zombie process, and the foreign name of the zombie is zombie, so the zombie process is in the Linux state Referred to as Z,
if the parent process ends at this time, the init process will automatically take over the child process and collect the corpse for it, and it can still be cleared. But if the parent process is a loop and will not end, then the child process will always remain in the zombie state.

Insert picture description here

The realization of the zombie process

  1. To achieve the zombie process, we only need to create two processes in the code, let the child process exit, and the parent process loops output to achieve the goal.
#include<stdio.h>
#include<unistd.h>
int main()
{
    
    
  pid_t ret=fork();//创建子进程
  if(ret<0)
  {
    
    
    return 0;
  }
  else if(ret==0)
  {
    
    

    printf("I am child,pid is:%d\n",getpid());
    printf("My father pid is:%d\n",getppid());
  }
  else 
  {
    
    
    while(1)
    {
    
    
      printf("I am father,pid is %d\n",getpid());
      printf("My father pid is:%d\n",getppid());
      sleep(1);
    }
  }
  return 0;
}

After letting the program run, we analyze the parent-child process:

1. The PID of the parent process is: 20518
2. The PID of the child process is: 20520

Insert picture description here

We use the command ps aux | grep main to view the information of the parent process and the child process, and we will find that the child process has become Z

The harm of zombie processes

  1. Zombie processes will occupy system resources. If there are many zombie processes, there will be no available process numbers and the system cannot create new processes.

How to deal with zombie processes

Use the command kill -9 [pid]

Orphan process

A parent process exits, and one or more of its child processes are still running, then those child processes will become orphan processes. The orphan process will be adopted by the init process (process number is 1), and the init process will complete the state collection work for them. An orphan process is a process without a parent process. Whenever an orphan process appears, the kernel sets the parent process of the orphan process to init, and the init process will cyclically wait() its child processes that have exited. In this way, when an orphan process bleakly ends its life cycle, the init process will handle all its aftermath work on behalf of the party and the government. So the orphan process is not harmful

Create an orphan process: let the child process fall into a loop, the parent process exits first

#include<stdio.h>
#include<unistd.h>
int main()
{
    
    
  pid_t ret=fork();
  if(ret<0)
  {
    
    
    return 0;
  }
  else if(ret==0)
  {
    
    
    while(1)
    {
    
    
      printf("I am child,pid is:%d\n",getpid());
      printf("my father pid is:%d\n",getppid());
      sleep(1);
    }
  }
  else 
  {
    
    
    printf("I am father,pid is:%d\n",getpid());
    printf("my father pid is:%d\n",getppid());
  }
  return 0;
}

Insert picture description here
We can see that this orphan process has been adopted by Process One.

Guess you like

Origin blog.csdn.net/ahuyccc/article/details/115305397