[Linux] Process status

[Linux] Process status

The concept of process status

Before understanding the process status, you must first know that a running process does not perform calculations on the CPU all the time. Instead, it is under the management of the operating system and takes turns using the CPU with other running processes. Does the operating system manage the process? Putting it on the CPU for calculation is based on the process status.

blocking state

The blocking state is a state of non-advancement caused by the process waiting for a certain resource to be ready.

The blocking state subjectively gives the impression that the process is "stuck". For example, when the process is downloading a certain software and the network is disconnected, the process of downloading the software will be "stuck" and the process needs to wait for resources such as the network. Only when you are ready can you proceed. Therefore, when a process is blocked, it must be waiting for some kind of resource. The process enters the blocking state because it wants to wait for specific resources to be used by others before it can be used by itself.

Since the operating system needs to manage a variety of hardware, the operating system needs to use a certain structure to describe the hardware, and then organize these structures to facilitate management. When describing these hardware, there will be a description of the waiting queue. This The queue will record the PCB of the process that is waiting for the current hardware resources. The operating system implements this general principle to put the process into a blocking state:

image-20230804144704352

pending state

The suspended state is a state in which the operating system releases the code and data of the process in memory while the process is waiting for a certain resource to be ready.

In the suspended state, the PCB of the process is still on the waiting queue of the hardware. When the operating system thinks that the resources that the process is waiting for will take a long time to be ready, it will release the code and data of the process in the memory to make extra memory space. It is used to do other things and improve memory utilization. When the resources that the process is waiting for are ready, the operating system will load the code and data of the process into the memory and start the process.

Process status under Linux

Under the Linux system, the following process status is defined 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 */
};

Note: The process under Linux will set a variable in task_struct to record the process status.

R state

The R state is the running state, which means that the task_struct of the process is in the running state waiting queue in the operating system and is executed during the operating system's cycle execution of the code of the process in the running state.

Note: A running process does not mean that it is always executed on the CPU, but is executed in a round-robin fashion like other running processes.

To verify the R status, write the following code:

#include <stdio.h>

int main()
{
    
    
  while(1)
  {
    
    }
  return 0;
}

Run the program under Linux system and view the process status:

image-20230823170847439

Since the execution of this code does not require any resources, it will not enter the blocking state, so it will remain running.

S state

The S state is an interruptible sleep state, which is a blocking state in the Linux system.

To verify the S state, write the following code:

#include <stdio.h>

int main()
{
    
    
  while(1)
  {
    
    
    printf("hello world\n");
  }
  return 0;
}

Run the program under Linux system and view the process status:

image-20230823171246424

Although the program in this code is an infinite loop operation, because the code in the loop structure runs very quickly, printing data to the screen is very slow. Therefore, most of the time when the program is running, it is waiting for the screen to be printed. resources, so when querying the status, most of the time the S status is queried. In addition, the process in the hibernation state can be interrupted and you can choose to enter ctrl+zthe termination process.

D state

The D state is an uninterruptible sleep state, which is a blocking state in the Linux system.

The D state is when the process transfers data from the memory to the disk. The pressure on the disk is too high, resulting in a very slow data transfer speed. The process must wait for the data transfer to complete and remain in a dormant state. In order to prevent the operating system from killing the dormant process dies, so the process is set to an uninterruptible sleep state. D state is not a common state. If the process is in D state, it generally means that the computer disk pressure is too high.

T state

The T state is a paused state, and the process will no longer continue to run in the paused state.

In order to verify the T status, write the following code:

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

int main()
{
    
    
  int i = 1;
  while(1)
  {
    
    
    printf("hello world %d\n", i);
    i++;
    sleep(1); //Linux系统提供的休眠函数,头文件是unistd.h
  }
  return 0;
}

To run a program under a Linux system, kill -19 进程idpause the process and view the process status:

image-20230823184608894

The process can be restarted by typing kill -18 进程id:

image-20230823185051979

Supplement: The + after the process status under Linux means that this is a foreground process:

image-20230823185204139

Those without + in the process status are background processes:

image-20230823185408461

When the foreground process is running under Linux, bash is invalid, but when the background process is running, bash is available. In addition, the background process can be kill -9 进程idshut down using .

t state

The t state is a tracking pause state, which is a special case of the T state.

In order to verify the t status, you can borrow the gdb tool and use gdb to start debugging a program:

image-20230823190655655

Z state

The Z state is called the zombie state. In the zombie state, the process has completely stopped, but the task_struct and part of the data of the process are retained. The purpose of the process entering the zombie state is to know whether there are problems with the running results of the process, because the retained data contains information such as exit codes, and the exit code is data used to determine whether there are problems with the running results of the process. After the process terminates, it will enter the zombie state and wait for recycling by the parent process.

In order to verify the T status, write the following code:

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

int main()
{
    
    
    pid_t id = fork();
    if (id == 0)
    {
    
    
        //子进程
        while(1)
        {
    
    
            printf("我是子进程,我的pid:%d,我的ppid:%d\n", getpid(), getppid());
            sleep(1);
        }
    }
    else if (id > 0)
    {
    
    
        while(1)
        {
    
    
            printf("我是父进程,我的pid:%d,我的ppid:%d\n", getpid(), getppid());
            sleep(1);
        }
    }
    return 0;
}

To run a program under a Linux system, kill -9 进程idkill the child process and view the process status:

image-20230823200716754

Harms of zombie processes: Since the task_struct and data of the process are retained in the zombie state, memory space will be occupied. If multiple child processes are created and not recycled, a large amount of space will be occupied, causing serious memory leaks.

X status

The X state is called the death state. In the death state, the process is completely stopped, and the operating system will quickly recycle and release the task_struct, code and data of the process.

Orphan process

An orphan process is a process that is managed by the operating system when the parent process stops while the process is running, and the parent process is converted to the operating system (process No. 1).

To verify the orphan process, write the following code:

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

int main()
{
    
    
  pid_t id = fork();
  if (id == 0)
  {
    
    
    //子进程
    while(1)
    {
    
    
      printf("我是子进程,我的pid:%d, 我的ppid:%d\n", getpid(), getppid());
      sleep(1);
    }
  }
  else if (id > 0)
  {
    
    
    //父进程
    int cnt = 5;
    while(1)
    {
    
    
      printf("我是父进程,我的pid:%d, 我的ppid:%d\n", getpid(), getppid());
      sleep(1);
      if (--cnt == 0) break;
    }
  }
  return 0;
}

In order to facilitate compilation, write the following makefile:

myproc:myproc.c
		gcc -o $@ $^
.PHONY:clean
clean:
		rm -f myproc

After preparing the code and makefile, compile the program, then run the program, and use while :; do ps axj | head -1 && ps axj | grep myproc | grep -v grep; sleep 1; donethe command to query the process status every second:

image-20230824134857086

The parent process of the parent process in the process myproc is bash. After it stops and enters the zombie state, bash as its parent process will perform recycling operations, so its zombie state cannot be seen. The child process in the process myproc is still in progress, but the original If the parent process is stopped, a new parent process is required to recycle it. Otherwise, the zombie state cannot be recycled after it is stopped.

After becoming an orphan process, it becomes a background process. You can choose to use killall 进程名instructions to kill all processes with the same process name.

Guess you like

Origin blog.csdn.net/csdn_myhome/article/details/132472734