3 minutes to figure out the difference between processes and threads

Foreword

Process (process) and thread (thread) is the basic concept of the operating system, but they are more abstract, not easy to grasp.

v2-b551a60a10a89f8428e5d2ab45c6e353_hd.png


Recently, I read an article material, found a good analogy, they can explain the clear and understandable.

1.

v2-0e134431a90be4572575c70518fb4bdf_b.jpg

Core computer is that CPU, it assumed all computing tasks. It's like a factory, always running.


2.

v2-9acb2da5dc4074f2b0f8486182ebfb65_b.jpg

Assumed 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.

v2-3379a56fecc915802aa5b52bfd724d1f_b.jpg

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


4.

v2-630a30b3393d04a40d51b56b452ab946_b.jpg

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


5.

v2-a8d38e17917093ef790921b14395c3e3_b.jpg

Thread-like worker ratio in the workshop. A process can include multiple threads.


6.

v2-710acd5c6f459eaa88b228bb9a4616aa_b.jpg

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.

v2-7cee6d022c5149ca09bf8e6ae4d114c9_b.jpg

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.

v2-5c6d99a49a51b7dc7d145eb40ee197c2_b.jpg

A simple way 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.

v2-72eb5e6fb24a6112c6b8287b8ee502c3_b.jpg

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. This is like certain memory areas, can only provide a fixed number of threads.


10.

v2-68d5e8fa137ceb949fccd34b852103e1_b.jpg

Then 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.
Welcome to the concern I kind of public-ho [programmers] marks the beginning of the article will be updated on the inside, documentation will be on the inside.

11.

v2-2ce18b5e79abe872fefc4711f7d6c306_b.jpg

Operating system design, and therefore can be summed up in 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 the sharing of resources between threads and between processes.


At last 

Welcome to share with everyone, like the point of a praise yo remember the article, thanks for the support!


Guess you like

Origin blog.51cto.com/14442094/2437414