The relationship and difference between processes, threads and coroutines

The relationship and difference between processes, threads and coroutines

1. Process (Process)
Process is a running activity of a program in a computer on a certain data set. It is the basic unit of resource allocation and scheduling in the system, and the basis of the operating system structure.

2. Thread (Thread)
A thread is sometimes called a Lightweight Process (LWP) and is the smallest unit of program execution flow. A standard thread consists of thread ID, current instruction pointer (PC), register set and stack.

In addition, a thread is an entity in a process and a basic unit that is independently scheduled and dispatched by the system. A thread does not own system resources by itself, only a few resources necessary for running, but it can share all the resources owned by the process with other threads belonging to the same process.

Threads have their own independent stacks and shared heaps, shared heaps, but not shared stacks. Thread switching is generally also scheduled by the operating system. A thread has five states: initialized, runnable, running, blocked, and destroyed.

Tip  For the operating system, a thread is the smallest unit of execution, and a process is the smallest unit of resource management. Both processes and threads are managed by the operating system.

3. Coroutines

A coroutine is a function that is more lightweight than a thread . Just as a process can have multiple threads, a thread can have multiple coroutines. Coroutines are not managed by the operating system kernel, but are completely controlled by the program, that is, executed in user mode. The advantage of this is that the performance is greatly improved, because it does not consume resources like thread switching.

Hint that a  coroutine is neither a process nor a thread, but a special function . The function can be "suspended" somewhere, and can resume running outside where it was suspended. Therefore, coroutines are not a dimensional concept compared to processes and threads.

A process can contain multiple threads, and a thread can also contain multiple coroutines. In short, there can be multiple such special functions running in a thread, but one thing must be clear: the running of multiple coroutines in a thread is serial. If it is a multi-core CPU, then multiple processes or multiple threads within a process can run in parallel. But in a thread, the coroutine is absolutely serial, no matter how many cores the CPU has, after all, although the coroutine is a special function, it is still a function. A thread can run multiple functions, but these functions are run serially. While one coroutine is running, other coroutines must be suspended.

The relationship between processes, threads and coroutines is shown in the following figure:

Private message me to receive the latest and most complete C++ audio and video learning and improvement materials, including ( C/C++ , Linux , FFmpeg , webRTC , rtmp , hls , rtsp , ffplay , srs )

 

4. Comparison of processes, threads and coroutines

The comparison of processes, threads, and coroutines is as follows:

A coroutine is neither a process nor a thread, a coroutine is just a special function. Coroutines, processes and threads are not one dimensional.
A process can contain multiple threads, and a thread can contain multiple coroutines. Although multiple coroutines in a thread can be switched, these multiple coroutines are executed serially, and only one thread can be running at a certain time, which cannot take advantage of the multi-core capability of the CPU.

Like processes, coroutines also suffer from context switching issues.

The switcher of the process is the operating system, and the switching timing is determined according to the operating system's own switching strategy, and the user is indifferent. The switching content of the process includes the page global directory, the kernel stack and the hardware context, and the switching content is stored in the memory. The process switching process adopts the method of "from user mode to kernel mode to user mode", and the switching efficiency is low.

The thread switcher is the operating system, and the switching timing is determined according to the operating system's own switching strategy, and the user is indifferent. The switching content of the thread includes the kernel stack and hardware context. Thread switching content is saved on the kernel stack. The thread switching process adopts the method of "from user mode to kernel mode to user mode", and the switching efficiency is medium.

The switcher of the coroutine is the user (programmer or application), and the switching timing is determined by the user's own program. The switching content of the coroutine is the hardware context, and the switching memory is stored in its own variable (user stack or heap). The switching process of the coroutine is only in user mode (that is, it does not fall into kernel mode), so the switching efficiency is high.

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/124435921