WeChat search
The brain is in the fried fishPay attention to this fried fish with fried liver. In this paper,
GitHub
github.com/eddycjy/blogIs included, with my series of articles, materials, and open source GO books.

Hello, everyone. I’m Fried Fish.

With gold and silver, it’s interview season. In my Go reader exchange group, there are many friends discussing some GO interview questions they have met in the interview process.

Today’s hero is a required skill for engineers, and that is “What is coroutine, the difference and connection between coroutine and thread?”

Not only to understand the thread, but also to explain the coroutine, and explain the difference between the two, but because the mention of thread, inevitably involves the process, so this article will also comb the introduction of “process, coroutine, coroutine” three essays knowledge.

So hopefully that gives you some food for thought.

process

What is a process

A process is an operating system abstraction of a running program. A process is the smallest unit of resource allocation.

Why is there a process

Why is there a process? To put it bluntly, in order to reasonably squeeze the performance of the CPU and allocate the running time slice, can not “idle”.

In a computer, its computing core is the CPU, which is responsible for all computation-related work and resources. A single CPU can only run one task at a time. It is not reasonable for a single process to run and completely occupy a single CPU.

So why squeeze CPU performance? Because the CPU is so fast, so fast, so fast, registers can only keep up with it, RAM and other bus-attached devices can’t keep up.

The reason for multiple processes

A phenomenon occurs when a task on a process is always running. The task may not always be executing a “computational” task. It may be executing a network call. If the task is blocked, the CPU will be wasted.

Again, multiple processes, multiple CPUs, multiple processes. Multi-process refers to the computer system can execute multiple processes at the same time. The transformation from one process to another process is managed by the operating system kernel. Generally, multiple software are run at the same time.

thread

With multiple processes, it is presumably possible to run multiple processes simultaneously on an operating system. So why are there threads when there are processes?

Here’s why:

  • Information between processes is difficult to share data, and parent-child processes do not share memory, so they need to exchange information between processes through inter-process communication (IPC), which results in high performance overhead.
  • Create a process (usually an invocationforkMethod has a high performance overhead.

Now we’re looking at in-process. Can we do something in the process?

A process can consist of multiple execution units called threads. Each thread runs in the context of the process, sharing the same code and global data.

Multiple processes, you can have more threads. Multiple threads share data more easily than multiple processes, and threads are generally more efficient than processes in context switching.

Here’s why:

  • It is very easy and fast to share data between threads.

    • You simply copy the data to a shared area in the process, but care should be taken to avoid multiple threads modifying the same memory.
  • Creating threads is 10 times or more faster than creating processes.

    • Threads are all children of the same process, such as memory pages, page tables, etc. Do not need.

What about coroutines

What is a coroutine

A Coroutine is a user-mode thread. Usually when a coroutine is created, a chunk of memory is allocated from the process’s heap to serve as a stack for the coroutine.

The thread stack is 8 MB, whereas the coroutine stack is usually only KB in size, whereas the Go language coroutines are even more dramatic at 2-4KB, which is very lightweight.

The birth of coroutines

According to Wikipedia, Marvin Conway coined the term “coroutine” in 1958 and used it to build an assembler, and the first published commentary on coroutines was published in 1963.

In other words, in history, there was “coroutine” first, and then there was “thread”. Thread was expanded after adding functions such as stack on the basis of coroutine.

But why didn’t coroutines catch on at first? This is more difficult to verify, and most of the probability is related to the background of the computer age 60 years ago.

Now, people further abstract the logic of coroutine scheduling to “wait for IO, give up, IO is finished”. On this basis, people find that coroutine can solve many code logic “chaos” in multi-threaded environment.

Advantages of coroutines

Since the thread seems to have filled the regret of the process so well, how come there is a “coroutine”, is to repeat the wheel?

The advantages of coroutines (via infoq@8two) are as follows:

  • Save CPU: to avoid the system kernel level thread frequent switching, resulting in a waste of CPU resources. Good steel is used for the cutting edge. As coroutines are user-mode threads, users can control the creation and destruction of coroutines by themselves, which greatly avoids the resource waste caused by system-level thread context switching.
  • Save memory: On 64-bit Linux, a thread allots 8MB of stack memory and 64MB of heap memory. System memory constraints prevent us from opening more threads to achieve high concurrency. In the coroutine programming mode, it can easily have hundreds of thousands of coroutines, which is incomparable to thread.
  • Stability: As mentioned earlier, threads share data through memory, which also leads to a problem where any one thread fails and all the threads in the process crash along with it.
  • Development efficiency: The use of coroutines in the development program, it is very convenient to asynchronize some time-consuming IO operations, such as writing files, time-consuming IO requests, etc.

Coroutines are essentially user-state threads, so some people say that coroutines are “light threads”, but we must distinguish between user-state and kernel state, it is critical.

conclusion

At the end of the day, the question “What is a coroutine, the difference and connection between a coroutine and a thread? This kind of question, the interviewer will usually introduce the process, thread, coroutine.

In order to facilitate memory and interpretation, we recommend the combination of stories will be better, this part can refer to Ruan Yifeng Da Shen translation of the process and a simple explanation of the thread, will bring a lot of good feelings.

And the key part is, what’s the difference between a coroutine and a thread?

We can through the introduction in the article, from the coroutine -> thread history to illustrate. Then further compare the advantages and disadvantages of coroutine and thread, can be a better interpretation of the difference and connection.

The better part, after explaining the basic concepts and differences, extends further to the job you are interviewing for, such as the Go language, and introduces the specific application and implementation of the Go language coroutine.

After all, the Go language can easily open hundreds of thousands of coroutines without any fuss. This will better reflect the depth and breadth of your knowledge of coroutines and threads, rather than simply memorizing concepts.

If you have any questions, please feel free to give feedback and exchange in the comment section. The best relationship is mutual achievement. Your thumb up is the biggest motivation for Fried Fish’s creation.

The article is updated continuously, you can search “brain into fried fish” to read, reply [
000I have prepared the first line of big factory interview algorithm solutions and information; In this paper,
GitHub
github.com/eddycjy/blogHas been included, welcome STAR to urge more.

reference

  • What’s the difference between a thread and a process?
  • Why do we have coroutines when we have multithreading?
  • A simple explanation of processes and threads