<title>The relationship between cooking and process threading</title>The relationship between cooking and process threads
what? cooking and process are related to THREADS. After a fuck, let's see what the relationship is. Technical articles sometimes with professional nouns will appear to force aligning higher, more professional, But for beginners are often very deep barriers. The so-called noun, is not before, in order to facilitate people to form a unified understanding, played an objective Name. Once a thing is objective, it becomes less vivid, less image. When you understand the obscure nouns of some of the predecessors, you may wish to replace them with common concepts in life. Understand the principle is the most important, not because of the text waste righteousness.
It is the cooking scene that everyone has Seen.
Let me begin by stating the concepts to be clarified today:
Process
进程是程序的上下文,包含要执行程序的代码。一个进程的简化执行过程如下:1、加载程序到内存2、CPU执行程序3、执行结束或将进程的上下文内容保存。
Thread
线程是进程的可执行单元。一个进程中可以有多个线程,进程如果阻塞之后,将被CPU至为阻塞态,此时线程被切出内存。
Taking stir fry as an example understanding processes and threads
If you are a chef now, the daily job is to stir-fry hot and sour shredded potatoes (i love them too).
okay, now I'm going to start assigning you resources:
Potato Peel one person: Zhang San
One of the shredded potatoes: John Doe
Chopped onion ginger Garlic One person: wang er
The one who gave you the ignition: careless
A person in charge of a plate: Mashiha
This is how the process works, First you drive these people into the kitchen (in memory), and these people work methodically until you fry the potato thread and get Mashiha. however, encountered a sudden situation, the superior suddenly to check (blocking state), so you can only let the brothers, are suspended work, and keep the success of the work, cut the potato silk can not throw it, to use a plate to hold up (keep the program context). After the superior inspection, the unfinished work is taken out and Continued. After a dish of potatoes is fried, start the next plate of shredded potatoes.
The description above is the concept of process.
Now I'm going to throw you a question: after that three potatoes Peel off, do you have to wait until the next potato is fired? yes, in the process is the case, the above all the process in the program is called the program counter, the counter is stored in the program command ADDRESS. One by one from the counter, take it, Add 1. When these steps become a whole, Zhang San is idle, John Doe is free, and the people below are not being used Efficiently. So if at this time the first plate of potato silk did not finish, and the second plate of potato wire to do, that only waiting.
So we have a thread:
Separate the parts of a process that can be separated into units:
Potato Peel one person: Zhang San
One of the shredded potatoes: John Doe
Chopped onion ginger Garlic One person: wang er
The one who gave you the ignition: careless
A person in charge of a plate: Mashiha
Everyone is a thread. Threads have their own program counters, which do not depend on each other. For example, the small potato peel and chopped onion ginger garlic is not related, in the multi-core CPU can be executed synchronously.
ok, the kitchen is starting to work NOW.
Potato Peel Zhang san, loaded with a frame of potatoes. Using a core of the cpu, try to peel the Potatoes. At the same time cut onion ginger garlic Wang er, loaded a lot of raw materials, also used a CPU core, efforts to cut onion ginger garlic. After the completion of the Zhang san, John Doe get Zhang San potatoes (the memory address space of the common program), with a core of the CPU (there may be Zhang San also may be Wang er idle core), quickly cut potatoes into silk. At this time the preparation work is over, began to stir-fry, careless Ignition. suddenly, Bartender said, The customer also called 10 points potato silk. So in the case of this potato silk is not finished, Zhang San has been idle, took orders, efforts to cut potatoes, and so On. In a very short period of time, they fired 11 parts of shredded Potatoes. Everyone has been used efficiently.
This article references: 1, "operating system essence and Design principle" 2, know:
What is the difference between a thread and a process? -know
Zhonyong Life is a practice, in enjoyment, more in Misery. no, Uninvited. I was really drunk to see the answers Above. Say a few words of my understanding. Let's start with a general overview: processes and threads are a description of a time period, a description of the CPU working time Period. The following background: cpu+ram+ various resources (such as video cards, optical drives, keyboards, GPS, and so on Peripherals) constitute our computer, but the operation of the computer is actually CPU and related registers and RAM between the things. One of the most basic facts: the CPU is too fast, too fast, too fast, The register can only catch up with his footsteps, ram and other devices hanging on each bus is completely a candle. What happens when multiple tasks are to be executed? In turn? or who is the highest priority? No matter what kind of strategy, a word is the CPU seems to be taking Turns.
A must know of the fact: the implementation of a program code, the implementation of a function of the process, when the cpu, the relevant resources must also be in place, that is, the graphics card ah, GPS AH what must be in place, and then the CPU began to Execute. Everything except the CPU here is the execution environment of this program, which is the program context we DEFINE. When the program is finished, or the CPU allocated to him is running out of time, it is going to be switched out and waiting for the next cpu. The last step in being switched out is to save the program context, because this is the next time that he is run by the CPU and must be Saved.
In tandem, the fact that the CPU seems to be all the tasks are a rotation of the execution, the concrete way is to load the context of program a, and then start to execute a, Save the context of program a, the next program B to execute the program context, and then start to execute b, Save the context of program B ....
========= important things come up ======== process and thread is such a background, two nouns is just the corresponding CPU time period description, noun is such a function. * Process is the total program execution time of the shifting context switch = CPU load context +cpu execution +CPU Save context thread what is it? The granularity of the process is too large, each time to have the top and bottom of the call, save, Recall. If we liken the process to a computer-run software, then the execution of a software can not be a logical execution, there must be more than one branch and a number of program segments, like to implement program a, is actually divided into a,b,c, such as a combination of multiple blocks. Then the concrete execution here can become:
Program A gets CPU = "CPU loading context, starts executing program A's a small segment, then executes A's b-segment, then executes the c-segment of a, and finally the CPU holds the context of a."
The execution of A,b,c here is to share the context of a, and the CPU does not have context switching when it executes. The a,b,c here is the thread, which means that the thread is a much smaller CPU time period that shares the context of the PROCESS.
To this end of the full text, another summary: the process and the thread is a description of the time period, is the CPU working time period description, but the particle size is different.
The relationship between cooking and process threads