【Process and thread】The best explanation

Table of contents

1. CPU management

2. Process

2.1. Overview

2.2. Data structure

2.3. Process status

2.4. Process and memory

2.5. PCB queue

3. Thread

3.1. Overview

3.2. Implementation


1. CPU management

Processes and threads are concepts in the operating system. This concept is actually a management strategy for the CPU. The entire operating system can be regarded as the management software of computer hardware, while processes and threads are the management of the CPU.

As the core of the computer, the CPU's responsibilities are summarized as follows:

Fetch and execute.

The CPU is a component that only knows how to process instructions, and what instructions are specifically processed is determined by the program counter. The program counter is like the keeper of the CPU. It will continuously send the address of the instruction to be executed to the CPU in the memory, and the CPU will execute the fetch instruction, fetch the instruction to be executed from the corresponding address, and then execute it. The whole process is shown in the figure below:

There are several problems in the entire cpu fetching process:

  • Utilization rate is not high

  • lack of control

Low utilization rate:

Assuming that the CPU is executing the instructions of a certain program, it will continue to execute the instruction codes of this program. When the entire program has slow operations, such as IO operations, the speed of IO is much lower than that of the CPU. Speed, then the CPU will be in a state of waiting for IO for a period of time. If the waiting time is used to perform other tasks in terms of CPU efficiency, a lot of content can be performed.

Lack of control:

There are priorities between programs. For example, the current CPU is running a program command to print a file. Because it needs to deal with IO devices, this program is relatively time-consuming. At this time, a program instruction to launch a nuclear bomb came in. Obviously, the program with higher priority waited until the program with lower priority was executed, and the previous instruction was properly executed, so there was no way to know.

Ideas to solve the above problems:

Let the CPU switch between programs, and let the programs execute alternately.

Instead of letting the program finish executing in one go, divide the CPU time into time slices. One second belongs to program A, the next second belongs to program B, and the next second belongs to program A... Let the programs execute alternately, so even if When encountering time-consuming operations such as IO, you can also use the idle time during it. For example, an IO operation originally takes three seconds, which means that the CPU has to wait for three seconds to return the result after executing the IO operation. I met IO in this second, and gave it to other programs in the next second, and came back in the next second, so that the time in the middle was used.

2. Process

2.1. Overview

We mentioned earlier that in order to make full use of the CPU, it is necessary to slice the CPU time and cross-execute programs. But after doing so, there is a problem to be solved:

How would you describe a program in action?

To make the program switch, it is necessary to describe these running programs, to describe where a certain program has been executed and so on. In this way, when scheduling back and forth, it can continue to run from the last time. Of course, the description of the thread must be more than one where it ran last time. There are also many details. For example, each running program can be given a priority. Each running program has a serial number to distinguish it. A description of a running program that is injected into this class is called a process.

Process definition:

Used to describe and manage the "running process" of a program. It refers to a running activity of a program on a certain data set. It is a basic unit for allocating resources .

Features of the process:

  • Concurrency, due to the execution of the CPU switching between programs, multiple processes will have a feeling of being executed concurrently.

  • Independence, a process is the basic unit for allocating resources and running scheduling.

The difference between the process rain program:

A program is static, a set of instructions stored in storage for a long time. Procedural dynamic is a one-time execution process of the program and exists for a short period of time. A program may have multiple processes.

2.2. Data structure

The process consists of three parts:

  • PCB

  • the code

  • data

There is nothing to talk about data and codes. These things must be required for the program to execute. The key point is to pay attention to the PCB.

Process Control Block (PCB): Used to record important messages such as where the current execution is going, priority, name, etc. The PCB is created when the process is created and canceled when the process is canceled. PCB is the unit of operating system management process.

2.3. Process status

Theoretically, a process has three states:

Running: The process occupies the CPU and runs on the CPU. Ready state (Ready): has the running conditions, not running yet, waiting for CPU resources. Blocked state (Block): The process that has started to execute is waiting for a resource during execution. Also called waiting state (wait). The above are only the three necessary basic states in theory, and the specific implementation of each type of operating system will be different.

2.4. Process and memory

Process and memory The data and code of each program are loaded into the memory space, and each process will have an independent corresponding data structure (page table in memory management) to record where the data and code of the process are stored. Each program is loaded into the memory to generate a corresponding process. When a process is generated, a PCB belonging to the process will be generated at the same time. The page table entry will be recorded in the PCB, that is, where the current program has been executed. The CPU will schedule and execute each process according to the PCB queue.

2.5. PCB queue

Processes waiting to be scheduled for execution:

Correspondingly, there are blocking queues, etc., all of which are similar to the structure in the above figure. The CPU will select a process from the ready queue to execute according to the scheduling algorithm. When the executed process cannot continue to advance, it will temporarily put the process into the blocking queue, and then select a process from the ready queue to execute again. After the previous process is ready again, a certain At this point in time, the CPU switches back and continues to advance the process.

3. Thread

3.1. Overview

Threads can be understood as multiplexing processes. The emergence of threads brings two advantages:

  • Switching between programs is more lightweight

  • Higher concurrency

1. Switching between programs is more lightweight:

Switching between processes will cause CPU context switching, which has a high performance overhead. If you don’t know the CPU context very well, you can move to:

Explain the state of the CPU in detail__BugMan's Blog-CSDN Blog

2. Higher concurrency:

Let's take an example:

Assume that the following instructions are to be executed in a certain time slice of the CPU:

After the first line of code calls the IO device, even if it has been used, the resource is no longer needed, but before the execution of the Nth line of code ends, the IO device is still occupied by the process, and other processes that need to use the resource can only wait. Of course, this problem does not exist in a single-CPU architecture, but once it is a multi-CPU architecture, this situation will occur.

3.2. Implementation

There are two implementations of threads:

  • user-level thread

  • kernel-level thread

User-level threads:

At the program level, the user implements logical threads through manual coding, and manually implements the thread switching logic through coding, that is, the scheduling in the user space is based on threads, and the scheduling on the CPU is based on processes.

Kernel-level threads:

User space scheduling is in units of processes, and CPU scheduling is in units of threads.

To sum up, threads can be understood as the multiplexing of processes. When the operating system supports threads, the context switching of the CPU is still performed in units of processes. The control block of a thread is called TCB, and its function and structure are almost the same as those of a process. The TCP of a thread belonging to the same process is stored in the memory space of the process.

Guess you like

Origin blog.csdn.net/Joker_ZJN/article/details/132345120