1. Process
Process is a computer program about a data set on a running activity, the system is the basic unit of resource allocation and scheduling, is the basis of the operating system structure.
2. Threads
Threads, sometimes called Lightweight processes (LWP), are the smallest unit of a program’s execution flow. A standard thread consists of a thread ID, the current instruction pointer (PC), a set of registers, and a stack.
In addition, the thread is an entity in the process, is the system independent scheduling and dispatching of the basic unit. A thread owns no system resources itself, only a few resources that are essential to running, but it can share all the resources owned by a process with other threads that belong to the same process.
Threads have their own separate stack and shared heap, shared heap, not shared stack. Thread switching is also typically scheduled by the operating system. Threads have five states: initialized, runable, running, blocked, and destroyed.
Tip To the operating system, thread is the smallest unit of execution, process is the smallest resource management unit. Both processes and routines are managed by the operating system.
3. Coroutines
Coroutines are a more lightweight function than threads. 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 programs, that is, executed in user mode. The benefit is that there is a significant performance improvement because there is no resource consumption associated with thread switching.
The hint coroutine is not a process or thread, but a special function. This function can be “suspended” somewhere, and can resume running outside the suspended point. Therefore, coroutines are not a dimensional concept compared with processes and threads.
A process can contain multiple threads, and a thread can contain multiple coroutines. In short, there can be more than one of these special functions running in a thread, but it must be made clear that the multiple coroutines running in a thread are serial. With a multi-core CPU, multiple processes or threads within a process can run in parallel. But coroutines are absolutely serial in a thread, no matter how many cores there are in the CPU. After all, a coroutine is a special function, but it’s still a function. Multiple functions can be run in a thread, 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 below:
4. Process, thread, coroutine comparison
The process/thread/coroutine comparison is as follows:
- A coroutine is neither a process nor a thread; it is simply a special function. Coroutines, processes, and threads are not in the same dimension.
- A process can contain multiple threads, and a thread can contain multiple coroutines. Although multiple coroutines in a thread can be switched, these coroutines are executed serially, and only one thread can be running at any time, not taking advantage of the CPU’s multi-core capability.
- Coroutines, like processes, suffer from context switching problems.
- The process switcher is the operating system, and the switching time is determined by the operating system’s own switching policy, and the user is insensitive. The process switches, including the page global directory, kernel stack, and hardware context, are stored in memory. The process switchover is from user mode to kernel mode and then to user mode, which is inefficient.
- The thread switcher is the operating system, and the switching time is determined by the operating system’s own switching strategy, and the user is insensitive. Thread switching involves kernel stack and hardware context. Thread switching content is stored in the kernel stack. The thread switching process adopts the mode of “from user state to kernel state and then to user state”, with medium switching efficiency.
- The switcher of the coroutine is the user (the programmer or the 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 coroutine is only user state (that is, it does not fall into kernel state), so the switching efficiency is high.