Relationship: Thread is the basic execution unit of a process. All tasks of a process are executed in threads; if a process wants to execute tasks, it must have a thread. Differences: 1. Threads in the same process share the address space of this process, but the address spaces between processes are independent; 2. Threads in the same process share the resources of this process, but the resources between processes are independent.
The operating environment of this tutorial: Windows 10 system, Dell G3 computer.
The relationship between threads and processes
Thread definition
Thread is the basic execution unit of the process , all tasks of a process are executed in threads
If a process wants to perform tasks, it must have a thread, and the process must have at least one thread
When the program starts, a thread will be opened by default. This The thread is called the main thread or UI thread
Process definition
A process refers to an application running in the system
Each process is independent, and each process runs in its own dedicated and protected memory
The difference between processes and threads
Address space: Threads in the same process share the address space of this process, while processes have independent address spaces.
Resource ownership: Threads in the same process share the resources of this process (such as memory, I/O, CPU, etc.), but the resources between processes are independent.
After a process crashes, it will not affect other processes in protected mode, but if a thread crashes, the entire process will die. So multi-process is more robust than multi-threading.
When switching processes, a large amount of resources are consumed and the efficiency is high. So when it comes to frequent switching, it is better to use threads than processes. Similarly, if concurrent operations are required to be performed at the same time and share certain variables, you can only use threads and not processes.
Execution process: Each independent process has an entrance to program running, a sequential execution sequence, and a program entrance. . However, threads cannot execute independently and must exist in the application program, and the application program provides multiple thread execution control.
Threads are the basic unit of processor scheduling, but processes are not.
Extended information: The meaning of multi-threading
Advantages
Disadvantages
More related knowledge, Please visit the FAQ column!
The above is the detailed content of What is the relationship and difference between threads and processes. For more information, please follow other related articles on the PHP Chinese website!