当前位置:网站首页>Golang collaboration scheduling (I): Collaboration Status
Golang collaboration scheduling (I): Collaboration Status
2022-06-12 15:29:00 【fish_ study_ csdn】
When explaining the part of operating system process scheduling , Almost all books will list a process state transition diagram first , Through the state diagram , It can clearly connect each link of process scheduling , Easy to understand .
Go The runtime scheduler can actually be seen as OS A somewhat simplified version of the scheduler Ben , One goroutine In its life cycle , It also includes the transformation of various states . The principles of these States and the switching between states are clarified , Yes, find out the whole Go The scheduler can be very helpful .
The following is my summary goroutine State transition diagram of , The circular box indicates the status , Arrows and text messages indicate the direction and conditions of switching :

Status details
Here's a brief analysis , And the State Gidle stay Go The scheduler code is not really used , So ignore . in fact , once runtime I've created a new one goroutine structure , Will set its state to Grunnable And add it to the task queue , So let's start with this state .
Grunnable
Golang in , A coroutine will be set to Grunnable state :
establish
Go In language , Including user entry functions main·main Implementation goroutine All tasks including , It's all through runtime·newproc -> runtime·newproc1 These two functions create , The former is actually a layer of encapsulation for the latter , Provide variable parameter support ,Go Linguistic go Keywords will eventually be mapped by the compiler to runtime·newproc Call to . When runtime·newproc1 After resource allocation and initialization , The status of the new task will be set to Grunnable, Then it is added to the current P In the private task queue of , Waiting for the schedule to execute .
Blocking task wakeup
When a blocking task ( Status as Gwaiting) When you are awakened when the waiting conditions are met — Like a task G#1 To someone channel Writing data will wake up before waiting to read the channel The task of data G#2——G#1 By calling runtime·ready take G#2 Reset the status to Grunnable And added to the task queue . About task blocking , It will be introduced in detail later .
other
Another path is from Grunning and Gsyscall The state changes to Grunnable, We have also merged them into the following introduction . All in all , be in Grunnable Your task must be in a task queue , Waiting for the scheduled execution at any time .
Grunning
All States are Grunnable All tasks can pass findrunnable Functions are scheduled (P&M) obtain , And then pass execute The function switches its state to Grunning, Last call runtime·gogo Load its context and execute .
I talked about it before. Go In essence, a cooperative scheduling scheme is adopted , A running task , You need to call yield To explicitly give up the processor ; stay Go1.2 after , The runtime also begins to support a certain degree of task preemption —— When the system thread sysmon It is found that a task takes too long to execute or runtime When judging that garbage collection is required , Will set the task to ” Can be preempted “ Of , When the task makes the next function call , Will give up the processor and switch back to Grunnable state . About Go1.2 Implementation details of preemption mechanism in , I will have another opportunity to introduce myself later .
Gsyscall
Go In order to ensure high concurrency performance at runtime , When the mission is performed OS Before system call , First call runtime·entersyscall The function sets its state to Gsyscall—— If the system call is blocked or takes too long , Will be the current M And P Separate —— When the system call returns , Execute thread call runtime·exitsyscall Try to retrieve P, If it succeeds and the current task is not preempted , Switch the status back to Grunning And continue ; Otherwise, set the status to Grunnable, Waiting to be scheduled again .
Gwaiting
When the resources or operating conditions required by a task cannot be met , Need to call runtime·park The function enters this state , After that, unless the conditions are met , Otherwise, the task will be in a waiting state and cannot be executed . Except for the ones mentioned before channel Except for the example of ,Go The timer of language 、 The Internet IO Operations may cause blocking of tasks .
untime·park Function contains 3 Parameters , The first is to unlock the function pointer , The second is a Lock The pointer , Finally, a string is used to describe the cause of the blocking . Obviously , The first two parameters are paired structures —— Because the task may get some before blocking Lock, these Lock You must save the task status before releasing , To avoid data competition . We know channel Must pass Lock Ensure mutually exclusive access , A blocked task G#1 You need to put yourself in channel Waiting in the queue , If it is released before the context is saved Lock, May lead to G#2 The unknown state of G#1 Set as Grunnable, So release Lock Must be in runtime·park Finish in . Due to blocking when the task holds Lock Different types —— Such as Select The lock of the operation is actually a set of Lock Set —— Therefore, it is necessary to point out Unlock In a specific way . The last parameter is mainly in gdb It is convenient to find the cause of task blocking during debugging . By the way , When all tasks are in Gwaiting In the state of , This means that the current program has entered a deadlock state , It is impossible to continue , that runtime This situation will be detected , And output all Gwaiting Mission backtrace Information .
Gdead
Last , When a task is finished , Would call runtime·goexit End your life —— I'm going to set the state to zero Gdead, And chain the structure to a part that belongs to the current P The free G In the list , For future use .
Go The concurrency model of the language basically follows CSP Model ,goroutine Completely depend on channel signal communication , Didn't like Unix Process wait or waitpid Waiting mechanism , There is no such thing “POSIX Thread” Medium pthread_join The convergence mechanism of , More like kill or signal This kind of interrupt mechanism . Every goroutine Exit and destroy by yourself after completion , Without a trace .
边栏推荐
- Servlet connects to database to realize user login function
- About layoffs in Internet companies
- Solve log4j2 vulnerability and be attacked by mining and zombie process viruses
- Particle filter learning record
- [LDA] LDA theme model notes - mainly Dirichlet
- [LDA] rough version notes of EM variational reasoning [to be improved
- [jvm learning] parental delegation mechanism and PC register (program counter)
- 如何写年终总结
- Xshell 7 official website free download
- Introduction to microservices
猜你喜欢

IMU的学习记录

Broadcast and multicast (tcp/ip details volume 1/2)

Learning records of new keywords, references & pointers

Wild pointer understanding

Increase the maximum number of MySQL connections

Particle filter learning record
![[LDA] basic knowledge notes - mainly AE and VAE](/img/1c/ccc073cac79b139becd5de0b9a91b1.png)
[LDA] basic knowledge notes - mainly AE and VAE

Scala download and idea installation of scala plug-ins (nanny level tutorial is super detailed)

Qiming Zhixian shares the application scheme of 2.8-inch handheld central control screen

POSTMAN-REST Client插件的应用
随机推荐
Is it safe to open an account for flush mobile stock trading
[untitled]
MySQL development considerations (Alibaba development manual)
频繁项集产生强关联规则的过程
Error 1105: message:\“raft entry is too large
What is reflection-- The soul of frame design
Solutions to some problems of scuacm22 retreat competition before summer training
File uploading and downloading in SSM
Two implementation methods of generic interface
Use of multithreading
Install RHEL 7/8 (red hat) virtual machine (Reprint)
IGMP message (tcp/ip details volume 1/ Volume 2)
[jvm learning] class loading subsystem
IMU的学习记录
C语言 分割bin文件程序
The difference between TCP and UDP, the three handshakes of TCP and the four waves of TCP
Village to village communication (and collective search)
PTA:自测-2 素数对猜想 (20分)
Job submission instructions upload jobs to network disk
Task output: dense snow ice city theme song 0612