A brief description of the relationship between processes, threads and CPUs

This article briefly describes the relationship between processes and threads. In order to facilitate understanding, this article will also briefly describe the working principle of CPU .

  A process is a running activity of a program in a computer on a certain data set. It is the basic unit of resource allocation and scheduling in the system, and the basis of the operating system structure. In other words, a process is a running activity of a program with a certain independent function on a certain data set, and a process is an independent unit for the system to allocate and schedule resources.

  A thread is an entity of a process and is the basic unit of CPU scheduling and dispatch . It is a basic unit smaller than a process that can run independently.

 

The relationship between processes and threads:

  (1) A thread can only belong to one process, and a process can have multiple threads, but at least one thread.

  (2) Resources are allocated to a process, and all threads of the same process share all the resources of the process.

  (3) The processor is assigned to the thread, that is, the thread is really running on the processor.

       (4) During the execution of the thread, cooperative synchronization is required. Threads of different processes should be synchronized by means of message communication.

 

 

Open your task manager and you will see the process column. As shown below




 
The tree diagram of programs, threads, and processes is as follows

 




 

The design of the operating system can therefore be boiled down to three points:

( 1 ) In the form of multi-process, multiple tasks are allowed to run at the same time;

( 2 ) In the form of multi-threading, a single task is allowed to be divided into different parts to run;

( 3 ) provide a coordination mechanism, on the one hand to prevent conflicts between processes and threads, and on the other hand to allow resources to be shared between processes and threads.

 

  For example, when we are playing a fighting game program, when player 1 punches, player 2 blocks. These two actions occur at the same time, so multi-threading is involved at this time, so the game process must have at least two threads, respectively Moves and blocks. For example, we use QQ to chat and open QQ music at the same time. At this time, everyone feels that QQ chat and QQ music are carried out at the same time, but the CPU can only handle one thing at the same time.

    I think everyone should have read or liked to read a comic book when they were young. When you flip through the book quickly, the characters in the book seem to move, which is the so-called frame-by-frame animation. The processing of the CPU The principle of events is similar.

    When you run QQ chat and QQ music, the job of the CPU is to constantly switch processes. If you switch 6 times per second, you will definitely feel stuck. If you switch fast enough, you will feel that you are running QQ music and QQ music at the same time. QQ chat.

 

in conclusion:

  (1) A thread is part of a process

  (2) The CPU schedules threads

  (3) The system allocates resources to processes and does not allocate resources to threads

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327057814&siteId=291194637