Threads and processes

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:

1、A process can have multiple threads, but at least one thread; a thread can only live in the address space of a process.
2、Resources are allocated to processes, and all threads of the same process share all resources of the process.
3、CPUThe thread is allocated to the thread, that is, the thread running by the processor is actually running.
4、Threads need to cooperate and synchronize in the execution process. Message passing is used to realize synchronization between threads of different processes.
There are multiple processes that allow multiple tasks to run simultaneously. A process can create multiple threads to execute the code.

Leave a Reply

Your email address will not be published. Required fields are marked *