13
WeChat search [ brain into fried fish ] Follow this fried fish with liver-fried liver. This article GitHub github.com/eddycjy/blog has been included, and there are my series of articles, materials and open source Go books.

Hello everyone, I am fried fish.

Recently, it is the season of interviews. In my Go reader exchange group, many friends appeared in some Go interview questions that they encountered during the interview process.

Today’s male protagonist is a compulsory skill for engineers, that is, " What is a coroutine, the difference and connection between a coroutine and a thread? "

It is necessary to understand threads as well as explain coroutines, and explain the difference between the two. However, since threads are mentioned, processes are inevitably involved. Therefore, this article will also introduce the three essays of "processes, coroutines, and coroutines" at the same time. Knowledge.

I hope it can arouse your thoughts.

process

What is the process

The process is an abstraction of a running program by the operating system, and the process is the smallest unit of resource allocation.

进程在操作系统中的抽象表现

Why is there a process

Why is there a "process"? is to rationally squeeze the performance of the CPU and allocate the running time slice . It cannot be "idle".

In a computer, its computing core is the CPU, which is responsible for all computing-related work and resources. A single CPU can only run one task at a time. If a process is running, it will completely occupy the only CPU, which is very unreasonable.

Then why squeeze the performance of the CPU? Because the CPU is really too fast, too fast, and too fast, the registers can only catch up to his footsteps, and RAM and other devices hanging on the buses are even more unmatched.

The reason for multi-process

If you are always running tasks on a process, a phenomenon will occur. That is, tasks may not always be executing "computational" tasks. It is very likely that they are executing network calls. If they block, the CPU will be wasted?

进程的上下文切换

This has appeared in multiple processes, multiple CPUs, and multiple processes. Multi-process means that the computer system can execute multiple processes at the same time, and the transition from one process to another is managed by the operating system kernel, generally running multiple software at the same time.

Thread

With multiple processes, it must be possible to run multiple processes at the same time on the operating system. So why do we need threads if we have processes?

The reasons are as follows:

  • It is difficult for information between processes to share data. Parent and child processes do not share memory. Inter-process communication (IPC) is required to exchange information between processes, which has a high performance overhead.
  • The performance overhead of creating a process (usually calling the fork method) is relatively large.

Everyone turned their attention to the process. Can you do something in the process?

进程由多个线程组成

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

With multiple processes, there can be more threads. Multithreading is easier to share data than multiple processes. In context switching, threads are generally more efficient than processes .

The reasons are as follows:

  • Data can be shared between threads very conveniently and quickly.

    • Just copy the data to the shared area in the process, but you need to be careful to avoid multiple threads from modifying the same memory.
  • Creating a thread is 10 times faster than creating a process or more.

    • Threads are all children of their own under the same process, such as memory pages, page tables, etc. are not needed.

What's going on with coroutine

What is a coroutine

Coroutines are user-mode threads. Usually when creating a coroutine, a section of memory is allocated from the process's heap as the stack of the coroutine.

The thread stack is 8 MB, while the size of the coroutine stack is usually only KB, and the Go language coroutine is even more exaggerated, only 2-4KB, which is very lightweight.

The birth of the coroutine

According to Wikipedia, Malvern Conway invented the term "coroutine" in 1958 and used it to construct assemblers. The original published commentary on coroutines was published in 1963.

That is to say, in history, there are "coroutines" first, and then "threads". Threads are expanded after adding functions such as stacks on the basis of coroutines.

But why didn't the coroutine become popular in the first place? This is more difficult to verify, and the high probability is still related to the background of the computer age 60 years ago.

Nowadays, people further abstract the logic of coroutine scheduling as "wait for IO, give up, and IO is finished". On this basis, people have found that the coroutine approach can solve a lot of code logic "chaos" in a multithreaded environment.

Advantages of coroutine

Now that the thread seems to have filled up the regrets of the process well, how come a "coroutine" comes out again? Is it to reinvent the wheel?

The advantages of coroutine (via InfoQ @八两) are as follows:

  • Save CPU: Avoid frequent switching of system kernel-level threads, resulting in waste of CPU resources. Good steel is used on the blade. The coroutine is a user-mode thread, and users can control the creation and destruction of the coroutine by themselves, which greatly avoids the waste of resources caused by system-level thread context switching.
  • Save memory: In 64-bit Linux, a thread needs to allocate 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, you can easily have hundreds of thousands of coroutines, which is unmatched by threads.
  • Stability: As mentioned earlier, threads share data through memory. This also leads to a problem. When any thread fails, all threads in the process will crash together.
  • Development efficiency: Using coroutines in the development program can easily asynchronousize some time-consuming IO operations, such as writing files, time-consuming IO requests, etc.

Coroutines are essentially threads in user mode, so some people say that coroutines are "light threads", but we must distinguish the difference between user mode and kernel mode, which is very important.

to sum up

In the final analysis, when encountering questions such as " What is a coroutine, and the difference and connection between a coroutine and a thread? " in daily or interviews, the interviewer routinely introduces the process, thread, and coroutine.

In order to facilitate memorization and interpretation, I recommend that you combine the story with it. For this one, you can refer to the " Process and Threads A Simple Explanation , which will bring a lot of good impressions.

And the most critical part is what is the difference and connection between ?

We can use the introduction in the article to explain from the historical process of the coroutine -> thread. Then, by further comparing the advantages and disadvantages of coroutines and threads, you can better interpret the differences and connections.

The more excellent part, after you can explain the basic concepts and differences, further extends the position you interviewed , such as Go language, you can introduce the specific application and implementation of Go language coroutines.

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

If you have any questions, welcome feedback and exchanges in the comment area. The best relationship between . Your is fried fish . Thank you for your support.

The article is continuously updated, and you can read it on [My brain is fried fish], and reply [160f6586034586 000 ] I have prepared the first-line interview algorithm questions and information; this article GitHub github.com/eddycjy/blog has been included , Welcome Star to urge you to update.

reference


煎鱼
8.4k 声望12.8k 粉丝