<> Deep understanding of processes and threads
<> The following is an abstract analogy ：
single CPU： A single core processor computer = A workshop ;
many CPU： A multi-core processor computer = A factory ;
process ： A workshop = A process ; （ That is, a running program ）
Multi process ： A factory can run multiple workshops at the same time ;
CPU And process ： single CPU Only a single process can run at the same time , many CPU Multiple processes can run at the same time .
thread ： A worker in the workshop = One thread ;
Process and thread ： A process can include multiple threads .
Memory sharing between threads ： The space in the workshop is shared by the workers , For example, many rooms are accessible to every worker .
The memory space of a process is shared , Each thread can use these shared memory .
Memory security ： however , The size of each workshop is different , Some can only accommodate one person at most . When the workshop is full , No one else can get in .
When a thread uses some shared memory , Other threads must wait for it to end , To use this memory .
mutex ： A simple way to prevent others from entering , Just add a lock to the door . The first person locks the door , Those who arrive later see the lock , Just line up at the door , Wait until the lock is opened .
This is called " mutex "–Mutex, Prevent two threads from reading and writing a memory area at the same time .
Semaphore ： The solution at this time , Just hang it at the door n Key . Whoever goes in takes a key , Hang the key back when you come out . Those who arrived later found the key overhead , I knew I had to wait in line at the door .
This practice is called " Semaphore "（Semaphore）, Used to ensure that multiple threads do not conflict with each other .
Locks and semaphores
： It's not hard to see , Mutexes are a special case of semaphores （n=1 Time ）. in other words , The latter can completely replace the former . however , because mutex Relatively simple , And high efficiency , Therefore, under the condition of ensuring exclusive resources , Or this design .
Resource allocation and scheduling logic of operating system
* In the form of multi process , Allow multiple tasks to run simultaneously ;
* In the form of multithreading , Allow a single task to run in different parts ;
* Provide coordination mechanism , On the one hand, it prevents conflicts between processes and threads , On the other hand, it allows resources to be shared between processes and threads .
<> The following is a rigorous explanation ：
Process is an execution process of a program , Is a dynamic concept , It is the basic unit of allocating and managing resources in the process of program execution , Each process has its own address space , At least 5
Three basic states , They are ： Initial state , Executive state , Waiting state , Ready status , Termination status .
Thread is CPU Basic unit of dispatch and dispatch , It can share all the resources owned by the process with other threads belonging to the same process .
<> Relationship between process and thread
A thread is part of a process
A thread can only belong to one process , A process can have multiple threads , But there is at least one thread
<> The difference between process and thread
Understand their differences , I start from the perspective of resource use .（ The so-called resource is the central processing unit in the computer , Memory , file , Network, etc ）
Fundamental difference ： Process is the basic unit of operating system resource allocation , Thread is the basic unit of task scheduling and execution
Overhead ： Each process has its own code and data space （ Program context ）, High switching overhead between processes
; Threads can be viewed as lightweight processes , Threads of the same type share code and data space , Each thread has its own independent running stack and program counter （PC）, The overhead of switching between threads is small
Environment ： Multiple processes can run simultaneously in the operating system （ program ）; And in the same process （ program ） There are multiple threads executing at the same time （ adopt CPU dispatch , Only one thread executes in each time slice ）
memory allocation ： The system allocates different memory space for each process ; For threads , except CPU Outside , The system does not allocate memory for threads （ The resources used by the thread come from the resources of the process to which it belongs ）, Only resources can be shared between thread groups
Inclusion relation ： A thread is part of a process , Therefore, threads are also called lightweight processes or lightweight processes