preface

“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

The difference between a process, a thread, and a coroutine is a hot topic, especially in job interviews.

In every interview, I always feel guilty about answering this question, even though I have memorized the answer in advance, because I don’t understand the difference.

So, this article comes, is also my recent research three differences of a little summary, I hope to help you!

concept

Starting with the concept, the definition of a process is “an abstraction of a running program.” There are several key points about this statement: first, it is a program, and second, it is running.

How to understand this sentence, such as our operating system, according to a variety of software, such as software, music, or social software (WeChat, QQ), the software is a program that, when they are not running, will not produce a process, when they run the process occurs at this time (in this case does not discuss whether these programs using multiple processes).

Next comes the thread, which is defined as the smallest unit of operation that an operating system can schedule. For the most part, they are contained within a process, the actual operating unit of a process. This sentence also has several key points, one is the operation scheduling, two is contained in the process

When you create a process, you actually create a thread. The tasks in a process are actually executed by threads. Processes contain threads.

However, the above explanation is based on the premise that the Linux operating system and the kernel version is greater than 2.6. The design of process threads is different for different operating systems, and even for Linux, the design of process threads is different for different kernel versions.

Finally, there are coroutines, defined, according to Wikipedia, as a class of components of a computer program that promote subroutines for cooperative multitasking, allowing execution to be suspended and resumed.

To tell you the truth, this sentence is really “listen to your words, such as listen to words”. To better understand, there are two main differences between coroutines and threads: one is whether they are scheduled by the system, and the other is whether they are scheduled preemptively or cooperatively.

For the thread, it is scheduled by the operating system, and by its scheduler to determine which thread is running, scheduling algorithm is preemptive, and belongs to an algorithm in the operating system kernel; In the case of coroutines, the scheduling algorithm is determined by the programmer or programming language, and the scheduling algorithm is cooperative scheduling, that is, the coroutine is actively ceded for other coroutines to execute (via the keyword yield).

Relationship between

A process contains multiple threads with a 1: N relationship between them.

A thread can contain more than one coroutine, which is usually a 1: N relationship, but also an N: M relationship, that is, multiple threads for multiple coroutines, such as Goroutine.

The difference between

The differences between the three have been briefly explained by definition, and will be compared in other ways.

Process vs. Thread

1. Process is the basic unit of operating system resource allocation, and thread is the basic unit of operating system scheduling

2. The memory address space of the process is isolated, and the memory address space of the thread is shared

3. Processes are independent of each other, threads are independent and shared

4. Large resource consumption during process context switch; Fewer resources are consumed when thread context is switched

Threads vs. coroutines

Threads are scheduled by the operating system, and coroutines are scheduled by the user or programming language

2. Threads are preemptive scheduling and coroutines are cooperative scheduling

3. Large resource consumption during thread context switch; Less resource consumption when coroutine context switches (process > thread > coroutine)

4, thread can use the advantages of CPU multi-core, realize parallel; Coroutines can’t take advantage of multiple CPU cores (1: N model) and can only be concurrent (doesn’t mean coroutines are slower than threads)

5. Thread switching involves kernel mode, while coroutine switching is performed purely in user mode

Write in the last

The above is my research process, threads, coroutines this period of summary, if useful, trouble to point a praise!!

reference

Difference between a “coroutine” and a “thread”