Processes and threads
preface
Modern operating systems have the ability to run multiple tasks simultaneously. For example: you can listen to music on the computer while chatting QQ.
To realize multiple tasks running at the same time is inseparable from the two important concepts of process and thread.
process
A process is a program that is loaded into memory and executed.
Before threads were introduced, time-sharing operating systems ran processes in a time-slice rotation: the operating system assigned each process a period of time, called a time slice.
If the process is still running at the end of the time slice, the process is paused and the operating system allocates CPU to another process. (This process is called context switching)
When a process is paused, it saves the state of the current process (process id, resources used by the process, etc.), recovers it from the previously saved state (contents of CPU registers and program counters), and continues execution at the next switch back.
If the process is blocked or stops running before the time slice ends, the operating system allows the CPU to switch immediately without waiting for the time slice to run out.
The time slice size is usually 10~100ms, plus the CPU speed is very fast. So at a macro level, all the programs are “simultaneous.” In fact, with a single-core CPU, only one process is occupying CPU resources at any one time.
Note: When threads are introduced, threads are the unit of CPU allocation time.
thread
There are often several sub-tasks in a process. Can you make these subtasks “simultaneous” as well? That’s where threads come in.
A thread performs a subtask. Thus, a process consists of multiple threads (at least one thread), each of which is responsible for a separate subtask.
For example, when the user uses the virus scan function, let the virus scan thread perform it. At the same time, if the user uses the garbage cleaning function, then you can suspend the virus scanning thread, first respond to the user’s garbage cleaning operation, let the garbage cleaning thread to execute. When the response is complete, switch back and execute the virus scanning thread.
Processes enable concurrency in the operating system, and threads enable concurrency within processes.
The difference between processes and threads
A process is an independent running environment, while a thread is a task that is executed within the process.
The essential difference between the two is whether they occupy the memory address space and other system resources (such as I/O) independently:
-
Processes occupy a certain memory address space, so there is memory isolation between processes, data is separated, data sharing is complex but synchronization is simple, each process does not interfere with each other; Threads share the memory address space and resources occupied by their own processes. Data sharing is simple, but synchronization is complex.
-
Process alone occupies a certain memory address space, a process problem will not affect other processes, does not affect the stability of the main program, high reliability; A thread crash may affect the stability of the entire program, resulting in low reliability.
-
The process alone occupies a certain memory address space, the creation and destruction of the process not only need to save the register and stack information, but also need to allocate resources recycling and page scheduling, high overhead; Threads only need to store register and stack information, which is less expensive. For this reason, threads are also called lightweight processes.
Another important difference is that processes are the operating system’s basic unit of resource allocation, while threads are the operating system’s basic unit of scheduling, or CPU time allocation.