Can you tell me the difference between a process and a thread

image.png
Definition of the two A
process is a running activity of a program with certain independent functions on a certain data set, and a process is an independent unit for the system to allocate and schedule resources.

A thread is an entity of a process and the basic unit of CPU scheduling and dispatching. It is a basic unit smaller than a process that can run independently. Threads basically do not own system resources themselves, but only have some resources that are essential in operation. (such as the program counter, a set of registers and stacks), but it can share all the resources owned by the process with other threads belonging to the same process.

The difference between process and thread A
process is the smallest unit of resource allocation, and a thread is the smallest unit of program execution;

A process has its own independent address space. Every time a process is started, the system will allocate an address space for it, and establish a data table to maintain the code segment, stack segment and data segment. Threads do not have independent address spaces. They use the same address space to share data ;

CPU switching a thread costs less than switching processes;

Creating a thread is less expensive than a process;

Threads consume far fewer resources than processes.

Communication between threads is more convenient. Under the same process, threads share data such as global variables and static variables. Communication between processes needs to be carried out by means of communication (IPC); (but it is difficult for multi-threaded programs to handle synchronization and mutual exclusion. )

Multi-process programs are safer and more resilient. The death of one process will not affect another process (because of having an independent address space). Multi-threaded programs are more difficult to maintain. When one thread dies, the entire process dies (because of shared address space);

The process has high requirements for resource protection, high overhead, and relatively low efficiency. The thread resource protection requirements are not high, but the overhead is small, the efficiency is high, and it can be switched frequently;

To strengthen understanding, make a simple analogy: process = train, thread = carriage
Thread travels under the process (simple carriage cannot run)

A process can contain multiple threads (a train can have multiple cars)

Difficulty sharing data between different processes (passengers on one train are difficult to transfer to another train, such as station transfers)

It is easy to share data between different threads in the same process (it is easy to change the A car to the B car)

Processes consume more computer resources than threads (multiple trains are more expensive than multiple cars)

Processes do not affect each other, a thread dying will cause the entire process to die (one train will not affect another train, but if a train in the middle of a train catches fire, it will affect all cars)

The process can be extended to multiple machines, and the process is suitable for multi-core at most (different trains can drive on multiple tracks, and the carriages of the same train cannot be on different tracks)

The memory address used by the process can be locked, that is, when a thread uses some shared memory, other threads must wait for it to end before using this piece of memory. (like a toilet on a train) - "mutex lock"

The memory address used by the process can limit the amount of usage (such as the restaurant on the train, how many people are allowed to enter at most, if it is full, you need to wait at the door until someone comes out) - "semaphore"

Supongo que te gusta

Origin blog.csdn.net/m0_54828003/article/details/127325367
Recomendado
Clasificación