The difference between process and thread-easy to understand

Preface

Some knowledge points are easy to forget after a long period of time, and they will be investigated again. It is better to organize them when you have time, so that it is convenient for others and self-examination.

A popular metaphor for processes and threads

Assume that the power of the factory is limited and can only be used by one workshop at a time. In other words, when one workshop starts, all other workshops must stop. The implication behind this is that a single CPU can only run one task at a time. The process is like the workshop of a factory, it represents a single task that the CPU can handle. At any time, the CPU always runs a process, and other processes are in a non-running state. There can be many workers in a workshop. They work together to complete a task. Threads are like workers in the workshop.

A process can include multiple threads. The workshop space is shared by workers, for example, many rooms are accessible to every worker. This symbolizes that the memory space of a process is shared, and each thread can use these shared memory. However, the size of each room is different, and some rooms can only accommodate one person at most, such as a toilet. When there are people inside, no one else can enter. This means that when a thread uses some shared memory, other threads must wait for it to end before they can use this memory. An easy way to prevent others from entering is to put a lock at the door. Those who arrived first locked the door, and those who arrived later saw the lock and lined up at the door, waiting for the lock to open before entering. This is called "mutual exclusion" (Mutual exclusion, abbreviated as Mutex), which prevents multiple threads from reading and writing a certain memory area at the same time. There are also rooms that can accommodate n people at the same time, such as the kitchen. In other words, if the number of people is greater than n, the extra people can only wait outside. This is like some memory area, which can only be used by a fixed number of threads. The solution at this time is to hang n keys at the door. Those who enter take a key, and hang the key back when they come out. Those who arrived later found that the key was empty, and knew that they had to wait in line at the door. This approach is called "semaphore" (Semaphore), used to ensure that multiple threads will not conflict with each other. It is not difficult to see that mutex is a special case of semaphore (when n=1). In other words, the latter can be used instead of the former. However, because mutex is relatively simple and efficient, this design is still used when resource monopoly must be guaranteed.

The design of the operating system can therefore be summarized into three points:
1. In the form of multi-process, allowing multiple tasks to run at the same time;
2. In the form of multi-threading, allowing a single task to run in different parts;
3. Providing a coordination mechanism, on the one hand Prevent conflicts between processes and threads, and on the other hand, allow resources to be shared between processes and threads.

First of all, a general summary: Process and thread are both a description of a time period, which is a description of the CPU working time period.

The rest of the content can refer to:

https://www.zhihu.com/question/25532384
http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html


Guess you like

Origin blog.csdn.net/Ryan_lee9410/article/details/83582885