Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the scheduling principle of goroutine in Go language

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

What is the scheduling principle of goroutine in Go language? many beginners are not very clear about it. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

1. Basic knowledge about concurrency

Before we talk about the scheduling principle of goroutine, there are some operating system-related knowledge that we need to know, such as:

1. What is concurrency?

Concurrency: two or more tasks are executed over a period of time. We do not care whether these tasks are carried out at the same time, we just know that these tasks can all be carried out during this period of time, of course, this period of time can be very long or very short.

two。 What is the minimum concurrency unit of concurrency?

Process is the smallest unit of computer resource allocation, is the basic unit of CPU resource allocation, and has independent memory.

Thread is not only the smallest unit of computer scheduling, but also the smallest unit of program execution, is in the process, a process often has one or more threads.

3. How do computers achieve concurrency?

The time-sharing call of the computer is the foundation of concurrency. CPU executes different jobs by quickly switching jobs, and the basic scheduling unit can be blocked during execution, so it will give up CPU resources. When the scheduling unit is awakened again, it can use CPU resources, and the operating system ensures the whole scheduling process.

Second, the basic knowledge of Goroutine

In addition, with regard to the scheduling principle of goroutine, we need to figure out the following questions.

What is 1.goroutine?

Goroutine: is a kind of lightweight thread in Go-- co-program.

1) compared with threads, the advantage of co-programming is that it is very lightweight and the cost of context switching is very small.

2) for a goroutine, there is an attribute of sched in each structure G that is used to hold its context. In this way, goroutine can easily switch back and forth.

3) because the context switching occurs in the user mode, it is not necessary to enter the kernel state at all, so the speed is very fast. And only a small amount of information such as PC and SP of the current goroutine needs to be saved.

4) in the go language, each concurrent execution unit is a goroutine.

Goroutine concurrency in Go language adopts CSP (communicating sequential processes) concurrency model, pays attention to data sharing by means of communication, and is realized through goroutine and channel. (note: this part of knowledge will be sorted out in a separate chapter. )

two。 Since it has a smaller granularity than a thread, what does it have to do with a thread?

The threading model of the Go language is a special two-level threading model, as follows:

The implementation of the two-level thread model is very complex, similar to the kernel-level thread model, there can be multiple kernel-level threads in a process, but the threads in the process do not correspond to the kernel threads one by one. This thread model first creates multiple kernel-level threads, and then uses its own user-level threads to correspond to the created kernel-level threads, and its own user-level threads need their own programs to schedule. Kernel-level threads are dispatched to the operating system kernel.

III. Scheduling strategy of Goroutine

Let's take a look at the Go thread implementing the MPG model:

S (Sched): the structure is the scheduler, which maintains queues that store M and G, as well as some state information of the scheduler.

M (Machine): an M is directly associated with a kernel thread.

P (processor): represents the context required by M and is the processor that handles user-level code logic. G (Goroutine): in fact, it is essentially a lightweight thread.

Their relationship is as follows:

Introduction:

An M associates two things, one is a kernel thread and the other is an executable process.

A context P will have two types of Goroutine, one is running, the blue G in the figure, and the other is the queued, gray G, which is stored in the runqueue in the process.

The number of context P here also represents the number of Goroutinue runs, which is generally set to several, and several will run concurrently in the machine. Of course, the number of P can be set here, either by the value of the environment variable GOMAXPROCS, or by calling the function runtime.GOMAXPROCS () at run time, up to a maximum of 256.

With the above knowledge, we know some basic concepts of Goroutine, but we still don't know how concurrency of Go is scheduled. This topic requires us to split several scenarios of Goroutine (create, destroy, and run).

1. Before executing the go statement, let's take a look at what preparations the program has made, that is, what the initialization startup process of the program looks like.

There are three key points in the above code, which are runtime.schedinit,runtime.main,runtime.mstart

Step1: runtime.schedinit: this step is the initialization of the scheduler, which sets the size of the GOMAXPROCS, which cannot exceed its upper limit of 256. of course, it creates a set number of Ps, all of which are idle, and then stores all the created Ps in the idle list associated with pidle in the sched.

Step2: the program will continue to execute runtime.newproc to create the first goroutine of the program, and this goroutine will execute runtime.main, the main function we see. After that, main will actively create a kernel thread M, which is only used for system monitoring. This kernel thread is related to the scheduling of goroutinue in the program.

Step3: after runtime.mstart, the program starts to execute. If you need to create a goroutine later, you will call the go statement to create it.

What does the 2.goroutine creation process look like?

When go func () is called, runtime.newproc is called to create a goroutine, which creates its own stack space and maintains stack address and program counter information in G's sched (Note: this data is used when goroutine is scheduled. To be exact, after the goroutine gives up the cpu, the next time the cpu is retrieved, this information will be reloaded into the cpu register. )

The created goroutine will be placed in the runqueue in the context P used by its corresponding kernel thread M. Wait for the scheduler to decide when to fetch the goroutine and execute it, which is usually scheduled in chronological order, which is a first-in, first-out queue.

3. How are the newly created goroutine scheduled?

After the goroutine is created, the scheduler decides when to execute the goroutine, a process called scheduling.

The newly created goroutine will initially be stored in the runqueue of the context P associated with a thread M, but in subsequent scheduling, some goroutine will be placed in the global queue because runtime.gosched is called.

The selection process for thread M is performed in the following order:

1. Take the goroutine from the runqueue in the P corresponding to M to execute it, and if not, execute 2.

two。 Try to fetch a goroutine from the global queue to execute, if any, execute! If not, execute 3.

3. From the P of the other thread M, steal some goroutine to execute, steal failed, execute 4. Note: if you steal here, you will steal half of it as soon as you steal it. The algorithm used is called work stealing. )

4. Thread M finds that there is nothing to do and goes to rest, that is, the thread's sleep, which is waiting to be awakened.

4. How does the running goroutine stop? Once it is stopped, what about the goutinue behind it?

After talking about the scheduling of goroutine, we have to consider a question: when will the goroutine being executed stop and what will happen after it stops? What about the goroutine hanging in the runqueue after the P corresponding to M?

Case 1: runtime park

When the runtime park function is called, the goroutine is set to the waiting state, thread M discards its own associated context P, and the system allocates a new thread M1 to take over the context P (Note: of course, M1 may also be created and idle).

The original thread M0 disconnected from the context, and M0 went to sleep because it had nothing to do, waiting to be awakened next time. As shown in the following figure:

Channel read and write operations, timer, network poll, etc., may be park goroutine.

Case 2: runtime gosched

Calling the runtime gosched function also causes the current goroutine to discard cpu, in which case the goroutine is set to runnable and placed in the global queue. Note: this is why there is a goroutine in the queue of the global variable.

What does 5.goroutine do when he is awakened?

If goroutine is in waiting state, after calling the runtime ·ready function, it will be awakened, and the awakened goroutine will be put back into the runqueue corresponding to the context of M, waiting to be scheduled.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report