Process (process) thread (thread)
folk understanding
Processes focus on managing the allocation and scheduling of memory resources, and threads focus on using the CPU to run code
Threading is equivalent to the number of pipelines in a factory, and the process is the product being produced on a certain pipeline.
Process is the basic unit of allocating resources, and threads are the basic units of execution.。In a purely metaphorical way, if you look at the computer as a whole as a national budget, the process is all kinds of special budgets (education, defense, etc.), and the thread is a more subdivided level of a specific purpose budget (such as specific production, employee allowances) it’s basically clear.
Make a simple analogy: process = train, thread = carriage.
- Threads move under the process (simple cars can’t run).
- A process can contain multiple threads (a train can have multiple compartments).
- It’s hard to share data between different processes (it’s hard for passengers on one train to change to another train, such as a station transfer)
- Data can be easily shared between different threads in the same process (A carriage is easy to change to B carriage).
- Processes consume more computer resources than threads (using multiple trains consumes more resources than multiple carriages)
- There is no interaction between processes, and a thread hang-up will cause the entire process to hang up (one train will not affect another train, but if one of the cars in the middle of one train is on fire, it will affect all the cars)
- Processes are independent of each other, sharing resources between threads of the same process. Threads within a process are not visible in other processes.
- The process can be extended to multiple machines, and the process is best suited to multiple cores (different trains can operate on multiple tracks, the same train can’t run on different tracks)
- The memory address used by a process can be locked, and when a thread uses some shared memory, other threads must wait until it finishes to use that memory. (for example, toilets on trains) – “mutex”
- The memory address used by the process can limit usage (e.g. the restaurant on the train, the maximum number of people allowed in, if it is full and needs to be at the door, etc.) – “semaphore”
The relationship between threads and processes: