difference between process and thread

1. Definition

Process: A program with a certain independent function is a running activity on a certain data set, and a process is an independent unit for the system to allocate and schedule resources.

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

 

2. Relationship

A thread can create and revoke another thread; multiple threads in the same process can execute concurrently.

Compared with a process, a thread is a concept closer to an executive body. It can share data with other threads in the same process, but has its own stack space and an independent execution sequence .

 

3. Difference

  The main difference between processes and threads is that they are different ways of managing operating system resources . Processes have independent address spaces. After a process crashes, it will not affect other processes in protected mode, and threads are just different execution paths in a process. Threads have their own stacks and local variables, but there is no separate address space between threads. The death of a thread is equivalent to the death of the entire process. Therefore, multi-process programs are more robust than multi-threaded programs, but when switching processes, the cost of The resources are larger and the efficiency is lower. But for some concurrent operations that require simultaneous operations and share some variables, only threads can be used, not processes.

1) In short, a program has at least one process, and a process has at least one thread.

2) The division scale of threads is smaller than that of processes, which makes multi-threaded programs have high concurrency.

3) In addition, the process has an independent memory unit during the execution process, and multiple threads share the memory, which greatly improves the running efficiency of the program.

4) Threads are still different from processes during execution. Each independent thread has an entry for program execution, a sequential execution sequence, and an exit for the program. However, threads cannot be executed independently, and must depend on the application program, and the application program provides multiple thread execution control.

5) From a logical point of view, the meaning of multithreading is that in an application, there are multiple execution parts that can be executed simultaneously. However, the operating system does not regard multiple threads as multiple independent applications to achieve process scheduling and management and resource allocation. This is the important difference between processes and threads.

 

4. Advantages and disadvantages

Threads and processes have their own advantages and disadvantages:

Thread execution overhead is small, but it is not conducive to resource management and protection;

The process is the opposite.

At the same time, threads are suitable for running on SMP machines, while processes can be migrated across machines.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325124032&siteId=291194637