Linux process programming practice 2-process status, simulation of zombie processes and orphan processes

1. Linux process status

The classic three states of the operating system are: ready, blocking, running.
For the Linux kernel, we can see the status of the process in the kernel source code.

/*
* The task state array is a strange "bitmap" of
* reasons to sleep. Thus "running" is zero, and
* you can test for combinations of others with
* simple bit tests.
*/
static const char * const task_state_array[] = {
    
    
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"t (tracing stop)", /* 8 */
"X (dead)", /* 16 */
"Z (zombie)", /* 32 */
};

  • R running status (running) : does not mean that the process must be running, it indicates that the process is either running or in the run queue.
  • S sleep state (sleeping): means that the process is waiting for the event to complete (the sleep here is sometimes called interruptible sleep
  • D Disk sleep is sometimes called uninterruptible sleep. Processes in this state usually wait for the end of IO.
  • T stop state (stopped) : You can stop (T) the process by sending a SIGSTOP signal to the process. The suspended process can continue to run by sending the SIGCONT signal.
  • X dead state (dead) : This state is just a return state, you will not see this state in the task list
  • The Z zombie state (Zombies) is a special state . When the process exits and the parent process (using the wait() system call, described later) does not read the return code of the child process exit, a dead (corpse) process will occur.
  • The dead process will remain in the process table in a terminated state, and will always wait for the parent process to read the exit status code. Therefore, as long as the child process exits, the parent process is still running, but the parent process does not read the child process status, and the child process enters the Z state

Second, the zombie process

2.1 What is a zombie process?

If the child process exits first , and the parent process has not exited , the child process must wait until the parent process captures the exit status of the child process before it really ends, otherwise the child process becomes a zombie process at this time. It is called a zombie process because although he died, it still exists in the process table .

2.2 Simulate the zombie process

The principle of the zombie process is that the child process exits first, and the parent process does not capture the exit status of the child process, this is the child process becomes a zombie process, we use fork () to create a child process, and after 10s drop out

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

int main()
{
    
    
  pid_t ret = fork();
  if(ret < 0)
  {
    
    
    printf("创建进程失败!\n");
  }
  else if(ret == 0)
  {
    
    
    int count = 0;
    while(count++ < 5)
    {
    
    
      printf("我是子进程(id:%d)\n",getpid());
      sleep(2);
    }
    exit(0);
  }
  else
  {
    
    
    while(1)
    {
    
    
      printf("我是父进程(id:%d)\n",getpid());
      sleep(1);
    }
  }
  sleep(1);
  return 0;
}

After the above code is compiled and run, you can see the following effects
Insert picture description here

We open another window and write a monitoring process script to view the status of the process

while :; do ps axj | head -1 && ps axj | grep 06-zombie| grep -v grep; sleep 1 ; echo "#############"; done

Insert picture description here
This way the zombie process is simulated

Q1: Why are there zombie processes?

  • The purpose of setting the zombie state to the process is to maintain the information of the child process so that the parent process can obtain it at some later time.
  • This information includes the process ID, termination status, and resource utilization information (CPU time, memory usage, etc.) of the child process.
    • If a process terminates, and the process has child processes in a zombie state, then the parent process ID of all its zombie child processes will be reset to 1 (init process). The init process that inherits these child processes will clean them up (the init process will wait for them to remove the zombie state).

Q2: What are the hazards of zombie processes?

  • The reason why the zombie process exists is to maintain the information of the child process. If the parent process does not read the exit status of the child process , the child process will always maintain the zombie state !
  • Maintaining the exit status itself requires data maintenance, which is also the basic information of the process, so it is stored in task_struct (PCB). In other words, the Z status never exits, and the PCB must always be maintained.
  • The structure itself occupies memory, if the Z state never exits, it will cause a waste of memory resources !
  • Cause a memory leak !

3. Orphan Process

3.1 What is an orphan process?

If the parent process exits first , and the child process has not exited , the parent process of the child process will become the init process.

note:

  • Parent process
  • If the parent process ends first, the child process will be entrusted to process No. 1

3.2 Simulate the orphan process

The simulation of the zombie process is achieved by creating a child process through fork() and exiting before the parent process. Similarly, an orphan process only needs to let the parent process exit before the child process, and only need to modify the code logic slightly. Can achieve

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

int main()
{
    
    
  pid_t ret = fork();
  if(ret < 0)
  {
    
    
    printf("创建进程失败!\n");
  }
  else if(ret > 0)
  {
    
    
    int count = 0;
    while(count++ < 5)
    {
    
    
      printf("我是父进程(id:%d)\n",getpid());
      sleep(2);
    }
    exit(0);
  }
  else
  {
    
    
    while(1)
    {
    
    
      printf("我是子进程(id:%d)\n",getpid());
      sleep(1);
    }
  }
  sleep(1);
  return 0;
}

Compile and run the above code:
Insert picture description here
we open another window and write a monitoring process script to view the status of the process

while :; do ps axj | head -1 && ps axj | grep 07-orphan | grep -v grep; sleep 1 ; echo "#############"; done

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40076022/article/details/113883985