Linux: Understanding the multiple states of a process

Article directory

  • understanding state
  • Operating status
  • blocking state
  • pending state
  • Process under Linux system
    • Analysis of status
    • View status

This article summarizes the various states of the process

For understanding the status of the process, the following thinking model diagram is usually included in the textbooks


So how do you understand what’s in the picture above?

Understanding status

How to understand status? In fact, it is very simple to understand the status. The status is a variable in PCB. For example, in PCB, there may be a variable defined as int status , and defined outside the code

#define NEW 1
#define RUNNING 2
#define BLOCK 3

From this, you can use the above macro definition to modify the status.

pcb->status=NEW;
...

Therefore, the so-called state change process is actually the process of modifying the integer variable. By modifying the integer variable, the state can be modified. So what is the use of this state representation?

if(pcb->status == NEW)
{<!-- -->
//PCB enters the run queue
...
}
else if(pcb->status == BLOCK)
{<!-- -->
// PCB enters the blocking queue
...
}

The status of the process is actually a variable of the PCB, so the status is only related to the PCB and has nothing to do with the code data

Running status

The running state is one of the many states of the process. So how to understand the running state?

As long as the process is in the running queue, the status is running status

The above is the definition of running status, but it also introduces a new concept. What is a run queue?

Generally speaking, a CPU can be responsible for managing a run queue. The default device here is a single-core device. Then the management method of CPU, memory and operating system is as follows :


Each CPU maintains a run queue at the system level

For the above figure, CPU maintains a runqueue queue, which manages a series of PCB, and each of them PCB also manages their corresponding executable programs. At this time, the status data in PCB corresponding to each executable program is NEW means that these programs are in the running state and can be scheduled at any time. Therefore, the above concepts combined with the above picture are actually the concept of running state.

Blocking status

After summarizing the running status, we need to learn about the blocking status. So what is the blocking status?

Take the following example:

#include <stdio.h>

int main()
{
int a;
scanf("%d", & amp;a);
printf("%d",a);
return 0;
}

This is a very simple program, input a value, and then output the value. When it starts running, the program enters the memory, and the operating system generates PCB for it to manage it. Then it becomes a process, but if no value is entered, in other words, the data on the keyboard is not ready, so the resources that the process wants to access are not ready, and the code of the process cannot be executed backwards, so it is blocking state

As a management software, the operating system will naturally know about this at the first time. It immediately knows that the process cannot proceed normally and is blocked, so it changes the status to BLOCK, but This is not the end. The operating system will remove this process from the run queue when modifying the status. Where to move it? The answer is waiting queue

That introduces a new word, what is a waiting queue? This brings us back to the previous question, how does the blocking state occur?

In the code we write, there must be some codes that need to access system resources. For example, the simplest input and output requires the keyboard and monitor, and some codes may even use various hardware devices such as disk network cards. From another perspective, in essence, the user wants to get the information the user wants from these hardware, but now the user does not input and the keyboard data is not ready, then the data the process needs to get from scanf is It is not ready, so there is no access condition, and the code of the process cannot proceed normally. At this time, the operating system knows this, because in the underlying logic of the operating system, it can manage the entire software and hardware resources, so The operating system also has its own solutions to this problem of abnormal hardware devices.

The operating system will also describe the hardware devices it manages and build their own structures for the hardware devices, so the following demonstration is available:


The operating system will also create a description of the hardware device it manages, here called dev. This structure will also describe a series of states, such as type, and what device it corresponds to. For example, status corresponds to the current hardware situation, such as pointers, which can connect data for chain access, and the wait_queue mentioned below

As mentioned earlier, when a running process is determined to be blocked, it can no longer be in the running queue, so where should it go? This leads to the concept of waiting queue. When a process enters the blocking state, it will be placed in the waiting queue to wait.


When the status of the process in the blocking queue returns to the running state, the process in the blocking queue can be taken back to the running queue.

So, what is the nature of process state changes?

  1. Change the variable of status in PCB
  2. Connect PCB to different queues

From this we can draw a conclusion: In the operating system, there will be a lot of queues, running queues, device waiting queues waiting for hardware, etc. All processes in the system are linked by double linked lists

Pending status

What is a hang?

Let us clarify a concept earlier. If a process is in a blocked state, it means that the process cannot be scheduled when the resources it is waiting for are not ready at this moment. However, the process is still in the memory. If the number is relatively small, It will not have any impact. What will happen if the number reaches a certain level so that the resources in the operating system are seriously insufficient?


In this case, the operating system’s choice is to replace the memory data to the peripheral. This operation is for all blocked processes, as shown in the following figure:


Where is the swap location? The operating system transfers the information to the swap partition. When the process is scheduled by the operating system, the code and data loaded into these partitions will be reloaded. In other words, this is a swap The process of swapping in and out, and the state of the process after exchanging information to the disk is called the suspended state.

What are the disadvantages?

Such behavior must have drawbacks. Since it involves the process of data input and output, this process will definitely lead to a decrease in efficiency. However, this is a choice made by the operating system to ensure the good operation of the entire system. If it is not made If you choose this option, there is a risk that the entire system will hang up. Therefore, in actual development, the size of the swap partition is usually set to be the same as the size of the memory.

Processes under Linux system

This article summarizes the concept of operating systems. In most cases, it is based on systems under Linux. Then you need to specifically enter the source code of the operating system to find specific information:

State representation of processes in Linux

/*
 * 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 *task_state_array[] = {<!-- -->
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"T (tracing stop)", /* 8 */
"Z (zombie)", /* 16 */
"X (dead)" /* 32 */
};

From the above study, we know that a process has a running state, a blocked state and a suspended state. When implemented in the Linux kernel source code, its implementation must also be designed based on this principle.

For example, as seen in the source code above, there are running states, sleeping and disk sleep states, and stopped and tracing stop status, as well as zombie and die status. These statuses are analyzed below:

Analysis of status

  1. RRunning status: It means that the process is running or in the running queue, and it does not necessarily mean that it is running.
  2. S Sleep state: Indicates that the process is waiting for the completion of an event
  3. DDisk sleep state: It represents an uninterruptible sleep state, which will be explained in detail later.
  4. TStop status: Indicates that the process has been stopped and can be restarted through signals, etc.
  5. XDeath status: indicates that the process has been killed

View status

To view the status, you can use a scheduled script to monitor the status in real time, and use the ps command to monitor the status.

First, write a C program with an infinite loop, so that the running status can be monitored in real time.

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

int main()
{<!-- -->
    while(1);
    return 0;
}
# Run the program
[test@VM-16-11-centos process]$ vim process.c
[test@VM-16-11-centos process]$ make
gcc -o process process.c
[test@VM-16-11-centos process]$ ./process

# Use script to observe process status
 PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
  365 2397 2397 365 pts/0 2397 R + 1003 0:05 ./process

At this time, the status column displays R + , so what does the plus sign mean?

There are many types of processes in Linux. For example, there are foreground processes and background processes. Then the process with a plus sign is called a foreground process. The most obvious difference between foreground processes and background processes is , cannot enter commands for bash to interpret.

In Linux, you can use & amp;


But the corresponding problem is that the program cannot be terminated using Ctrl + C. You need to use the kill command to terminate the process.

Hibernation

There are also processes in sleep state in Linux, which actually correspond to processes in blocking state. Processes in this state will not be run, and there are two sleep states, Light sleep and deep sleep

Light sleep

Taking the previous example as an example, when the currently running program needs to enter a value, if the user does not enter a value at this time, the program will be kicked out of the running queue and wait in the waiting queue. At this time, the program is in Blocked state, but if the operating pressure of the Linux system is too high at this time, it can save resources by killing the process.

The cost of killing a process is very high. If the process is just some running programs, it is not a big loss, but what if it is writing information to the disk? For this process, if the process is killed, it will be a big loss to the user.

Deep sleep

Therefore, in order to avoid such a situation, the Linux system deliberately created a disk sleep state. It is called the disk sleep state. It is specially designed for disks and is also called depth. Sleep state, for a process that enters deep sleep state, it will not be terminated at will, and the operating system has no power to operate the process in this state. The purpose of this is to protect the process that interacts with the disk. Termed unexpectedly by the operating system

Termination

There is a state in Linux which is the T and t states. Nowadays, there is almost no distinction between the two systems. These two states They all pause the process. tracing stop is more directed to tracking the process, such as the process of tracking breakpoints in debug mode.

Why?

Since when accessing software resources, you may need to temporarily stop access to the process, just set the process to STOP

Practical use?

In the debugging tool gdb of Linux, there is an operation called break point, and the essence of break point is when debug program, You can trace the program. If a breakpoint is encountered, the process will stop. In other words, the debugger is actually the process running, and the process of stopping at the breakpoint is actually the process of the process being terminated.

syntaxbug.com © 2021 All Rights Reserved.