Programs, processes and threads, concurrency and parallelism

1. Procedure

A computer program is a set of instructions that a computer can recognize and execute, which runs on an electronic computer and is an information tool that meets certain needs of people.

--Baidu Encyclopedia

The program is the bridge between the operator and the computer. The operator uses the programming language to convert his needs into a program through data structures and algorithms, and tells the computer what to do. The computer converts the programming language into a digital language through compilation, and puts into operation. In short, a program is a sequence of instructions .

2. Process and thread

A process is a unit for resource allocation by the operating system, an entity of a program, and a materialization of a program running process. When the program is not being executed, it is an inanimate entity, but when the program is executed by the operating system, it can become a living entity, and this living entity is called a process. In short, a process is a dynamic program.

A thread is the smallest unit that the operating system can perform operation scheduling, and it is included in a process. A thread is an execution path of a process. It can be understood that each thread constitutes a process. Threads in the same process share resources. By scheduling threads instead of processes, the computer can greatly save costs.

3. Concurrency and parallelism

We know that a CPU can only execute one instruction at the same time. If the CPU executes the instructions of a process sequentially within a period of time, then macroscopically, a CPU can only complete one process, but if within a period of time, multiple The instructions of the process are quickly rotated and executed. Finally, the macroscopic view shows that one CPU completes multiple processes at the same time. The process of completing multiple processes within a period of time by one CPU through fast rotation is called concurrency .

If a computer is equipped with multiple processors, it can execute multiple instructions at the same time. This process is called parallelism .

More intuitively, concurrency means that one processor processes multiple tasks at the same time, and parallelism means that multiple processors process multiple tasks at the same time. Simultaneous processing in concurrency is reflected macroscopically, and at the microscopic level, only one instruction is executed at a single time; parallel simultaneous processing can be reflected at a microscopic level, that is, multiple instructions are executed at a single time.

Four. Examples

Let's give an example to help understand the above five concepts, building blocks.

A large building block is composed of five parts, and the five parts are composed of several small building blocks. The manual that the player reads is the program , which describes the steps and specific operation methods that the player should perform. The player implements the operations in the manual by reading the manual . Dynamic process. The five parts of the big building block are distributed to five players to complete, and the five completion progress of the five parts constitutes the completion progress of the big building block. These five small progress are five threads. If this big building block is completed by only one player, and within a limited time, the player quickly and sequentially put together five parts to form a big building block, then at the end, it can be said that the player has completed five parts at the same time, but in fact the player is a The process of building blocks one by one and completing five parts by a single player is called concurrency. If this big building block is completed by five players, then at the end, it can be said that the five players have completed the five parts at the same time, that is, the assembly progress of the five parts is carried out at the same time, and the process of five players completing the five parts is called parallel .

Guess you like

Origin blog.csdn.net/m0_73249076/article/details/129309772