The difference between clear and understandable processes and threads

Process (process) and thread (thread) is the basic concept of the operating system, but they are more abstract, not easy to grasp. Recently, I read an article material, found a good analogy, they can explain the clear and understandable.

1, the core of the computer is the CPU , it assumed all computing tasks. It is like a factory , always running.

2, assume that the power plant is limited, can only supply a workshop. In other words, a workshop started when the plant must shut down the other. Meaning behind is that a single CPU can run only one task .

3, the process is like a factory workshop , it represents a single CPU can handle the task. At any one time, CPU always run a process, other processes is not running.

4, a workshop, there are many workers. They complete a collaborative task.

5, thread like in the workshop workers . A process can include multiple threads.

6, workshop space is shared by workers, such as many of the rooms is that each worker can be in and out of. This symbolizes a process memory space is shared, each thread can use these shared memory.

7, however, different size of each room, and some rooms can only accommodate a maximum of one person, such as a toilet. Someone inside when others can not go. This represents a thread when using certain shared memory, other threads must wait for it to end, in order to use this piece of memory.

8, a simple method of preventing others from entering, plus a lock that door. First man locked the door, and then to see people locked, you line up at the door, and so on and then go to open the lock. This is called "mutual exclusion lock" (Mutual exclusion, abbreviated Mutex), to prevent multiple threads simultaneously read and write a block of memory .

9, and some rooms can accommodate n individuals, such as the kitchen. That is, if the number is greater than n, the extra man to wait outside. Like some memory areas, we can supply a fixed number of threads .

10, when the solution is to put the key in the door of n. Go in they took a key, when the key hang out and then back into place. After people have found the key to the overhead, you must know that waiting in line at the door. This practice is called "semaphore" (Semaphore), used to ensure that multiple threads do not conflict with each other .

Easy to see, mutex semaphore is a special case of (n = 1 hours). In other words, you can replace the latter with the former. However, because the mutex is simple and efficient, it must ensure the availability of resources in the exclusive, or the use of this design.

11, operating system design, it can be attributed to the following three points:

(1) to form a multi-process, allowing multiple tasks to run at the same time;

(2) in the form of a multi-threaded, allowing a single operation tasks into different parts;

(3) provide coordination mechanism, on the one hand and between the threads to prevent conflict between processes, on the other hand allow sharing between processes and resources between threads

Guess you like

Origin www.cnblogs.com/kyoner/p/11026408.html