Summary of programs, processes, and threads (concept)

Introduce

As we all know, the three concepts of program, process, and thread complement each other and are said to be a hot spot for interviews. In this case, there should be three of them in the notes.

definition

1. Procedure

The program is an executable file stored on the storage medium.

Second, the process

A process is an execution instance of a program. It is a running program. It is dynamic and has its own process of creation, existence, and extinction. This process is called "life cycle".

Three, thread

A thread is an independent control flow in a process, consisting of an environment (including register banks and program counters) and a series of execution instructions.

summary

1. The program is static, while the process is dynamic;
2. The program is an ordered collection of some instructions, and the process is the process of program execution;
3. The program has only a static state, and the state of the process is changing, which Including the creation, scheduling and demise of processes;

To put it bluntly, a program is a pile of data and code stored in a storage medium. When it runs, it is a process, and the running program (process) will have some status, such as: suspend, wait, ready, run wait , These first briefly understand. Open the task manager on the computer and you will see a lot of running programs, that is, processes. (See the picture) The
Insert picture description here
right column of CPU data keeps changing, indicating that the CPU is managing the process, and there are data such as memory and disk beside the CPU Assign resources to the process; but the process is not the smallest unit for CPU scheduling, resource allocation and execution!
what is that? Not to mention.

Distinguish between programs and processes, and then talk about personal understanding of threads. The further refinement of the process is the thread, and the thread is an execution path within the process. To put it bluntly, the thread is inside the process, the process is a tree, the thread is a branch, some are responsible for growing up, and some are responsible for growing on both sides, but there will always be a backbone, and the main function in the program is temporarily understood as the backbone.

We have all used some anti-virus software. Here is an example of Kingsoft Internet Security:
when it is not running, it is a static program. When we start it, the CPU schedules it and allocates some resources to it for execution. At this time it becomes a process, what about threads?
Insert picture description here
Looking back at the picture, think about whether you have clicked on trash cleanup, computer acceleration, etc. while letting it carry out lightning detection and killing. Anyway, as long as Xiaofan opens it, these several options are named by name, and each function can be understood As a thread, it shares the resources allocated by the CPU to the Kingsoft Internet Security process (of course, each thread also has its own program counter, register set, and stack), and implements its own responsible functions; when the process ends, these threads are It's not natural that it can't run. So, the thread exists depending on the process. Since there is a problem of resource contention for shared resources, there is a thread safety problem to be learned later. The two threads want to use only one resource at the same time. Isn't it a problem if they are not handled well? The solution is to learn the mutex and semaphore later on. Since the process can be scheduled by the CPU, the thread is naturally also scheduled by the CPU, so the thread is the smallest unit of CPU scheduling, resource allocation and execution!

After understanding these three concepts, let's talk about the CPU.
We all know that the CPU is like a human brain and can run many programs. For example, we can still play games while listening to QQ music. These two are running at the same time, but there is only one CPU. How can we let several processes run at the same time? ? In fact, this is an illusion. The CPU actually calls a process to run for a period of time, and then calls another process to run for a period of time. The "time" here is called a "time slice". It ’s short, it feels like it ’s running at the same time, just like the kind of tape-and-reel movie that I watched a long time ago. The tapes are all pictures one by one, but when played, the speed is faster than human vision The processing function is fast, and the brain can't process it, so there will be a dynamic experience. This is also the case when the CPU executes the process, because its switching frequency is so fast that you ca n’t react too much, and mistakenly believes that multiple processes are caused by multiple CPUs executing individually. Parallelism is actually caused by the concurrency caused by the rapid switching of one CPU. If you do n’t feel it, you can open a bunch of programs insanely. At this time, the computer will have some cards, because the task is too much, the speed of the CPU switching back and forth is slow.
A CPU here (using time slices) to execute multiple tasks at the same time is called concurrent;
while multiple CPUs executing multiple tasks at the same time is called parallel;
intuitive parallelism is visually concurrent.
In a word, a program may have many functions after it runs into a process, but a process can only achieve one function at a time. How can a process achieve true parallelism of all functions? Doesn't this lead to threads? Each function is assigned a thread, so that all threads share the process's own resources and run. If multiple threads implement multiple functions at the same time, the parallel effect is achieved. Therefore, the characteristics of threads can be summarized naturally. It divides complex processes into multiple threads to run independently, which can improve the response and execution efficiency of the application program, realize all functions in a shorter time, release the CPU in advance, and improve the utilization of CPU rate.

End

This Xiaofan is used to organize notes. The content is for reference only.

Published 3 original articles · Likes0 · Visits 22

Guess you like

Origin blog.csdn.net/qq_43711326/article/details/105454360