[linux] Several states and state switching of processes and threads

1. The status of the process

1.1 Three states of the process

Process state: The life cycle of a process can be divided into a set of states that characterize the entire process. The process state reflects the life state of a process.

Generally speaking, a process has three states, namely, ready state, running state, and blocked state .

Running : The process is executing.
Ready state (Waiting) : The process is waiting for an event to occur, such as input and output operations, network requests, etc.
Blocked state (Blocked) : The process is suspended, waiting for the release of certain resources or the arrival of a signal.

1.2 Three state transition diagrams

insert image description here

1.3 Transitions between the three states

In theory, the transition between the above three states is divided into six situations;

Run --> Ready:

  1. The main reason is that the process occupies the CPU for too long, and the system allocates the CPU time for the process to be limited;
  2. In a system using a preemptive priority scheduling algorithm, when there is a process with a higher priority to run, the process is forced to give up the CPU, and the process changes from the execution state to the ready state.

Ready --> Run:

When the time slice of the running process is exhausted, the scheduling will go to the ready queue to select the appropriate process to allocate CPU

Run --> Block:

The process being executed cannot be executed due to a waiting event, the process changes from the execution state to the blocking state, such as an I/O request

Blocking --> Ready:

When the event that the process is waiting for has occurred, it enters the ready queue

The following two states are impossible:

Blocking --> Running: Even if the CPU is allocated to the blocked process, it cannot be executed. The operating system will not pick from the blocking queue when scheduling, but from the ready queue

Ready --> Blocking: The ready state is not executed at all, so it is impossible to talk about entering the blocking state.

In some systems, some new states have been added, such as suspended state, runnable state, deep sleep state, light sleep state, suspended state, and dead state.

1.4 Process status under linux

Processes in linux generally have the following states:

R running status (running): It does not mean that the process must be running, it indicates that the process is either running or in the run queue
.
S sleeping state (sleeping): means that the process is waiting for the event to complete (the sleep here is sometimes called interruptible sleep (interruptible sleep)).
D Disk sleep state (Disk sleep) is sometimes called uninterruptible sleep state (uninterruptible sleep).
Processes in this state usually wait for the end of IO.
T stop state (stopped): A process can be stopped (T) by sending a SIGSTOP signal to the process. The suspended process can be resumed 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 specific explanation is detailed in [linux] process concept (five, process status) .
Here are the conversion diagrams between them:
insert image description here

Second, the state of the thread

There are usually five types of threads:

New state (New): A thread object is newly created, and the life cycle of the thread begins.
Ready state (Runnable): After the thread object is created, other threads call the start method of the object, and the thread in this state will be in therunnable thread pool, become runnable, waiting for the right to use the cpu.
Running state (Running): The thread in the ready state obtains the time slice of the cpu and executes the program code.
Blocked state (Blocked): The blocked state is that the thread gives up the right to use the cpu for some reason, suspends or stops running, until the thread enters the ready state, there is a chance to get the favor of the cpu and then enter the running state.
Dead state (Dead): The run method that the thread finishes executing or exits due to an exception, the life cycle of the thread ends.

The blocking state here is divided into three types:

Waiting for blocking: the running thread executes the wait method, then the thread will release all the resources occupied, and jvm will put the thread into"waiting pool"In , it cannot be automatically awakened after entering this state, and needs to call the notify/notifyAll method to be awakened.
Synchronous blocking: When the running thread acquires the synchronization lock of the object, if the synchronization lock is occupied by other threads, jvm will put the thread into"Pool lock"middle.
Other blocking: When a running thread executes the sleep/join method, or issues an I/O request, jvm will put the thread in a blocked state. When the sleep method waits for a timeout, the join method waits for the thread to terminate or timeout, and the I/O processing is completed, the thread will re-enter the ready state.

3. Summary

The state of a thread may be affected by the scheduling policy of the operating system, so different operating systems may have different state definitions. In addition, in multi-threaded programming, programmers can also control the state transition of threads through mechanisms such as locks and condition variables.



Guess you like

Origin blog.csdn.net/qq_66314292/article/details/130109723