c++-the difference between process and thread

The comparison between threads and processes is as follows:

1) Process is the unit of allocation of resources (including memory, open files, etc.), and thread is the unit of CPU scheduling;

2) The process has a complete resource platform, and the thread only exclusive essential resources, such as registers and stacks;

3) Threads also have three basic states: ready, blocking, and execution, and they also have the transition relationship between states;

4) Threads can reduce the time and space overhead of concurrent execution;

For, threads can reduce overhead compared to processes, which is reflected in:

1) The creation time of a thread is faster than that of a process, because the process also needs resource management information, such as memory management information and file management information, during the process of creating a thread, and this resource management information is not involved in the process of thread creation. Is to share them;

2) The termination time of the thread is faster than that of the process, because the resources released by the thread are much less than that of the process;

3) Thread switching in the same process is faster than process switching, because threads have the same address space (virtual memory sharing), which means that threads in the same process all have the same page table, so there is no need to switch when switching Page table. For switching between processes, the page table must be switched when switching, and the overhead of the page table switching process is relatively large;

4) Since the threads of the same process share memory and file resources, when data is transferred between threads, there is no need to go through the kernel, which makes the data interaction between threads more efficient;

Therefore, threads are more efficient than processes in terms of time and space efficiency.

Guess you like

Origin blog.csdn.net/www_dong/article/details/113838219