当前位置:网站首页>GMP scheduling model of golang
GMP scheduling model of golang
2022-08-02 15:26:00 【The stars have a meal】
Directory
I. Briefly describe the GMP scheduling model of go language
G: A G represents a goroutine. The essence of a coroutine is a user-mode thread, and the user has control over it. It occupies less memory and has low switching costs.
M: Kernel state thread, an M represents a kernel thread, which is equivalent to a system thread. All Gs must be placed on M to run.
P: A logical processor, which can carry several Gs, and make these Gs dock with M in a timely manner. A P represents the context required by M.The number of P depends on the set GOMAXPROCS. The new version of go uses the maximum number of cores by default. For example, if you have an 8-core processor, then the number of P is 8
The number of M does not necessarily match the P. You can set a lot of M, and the M and P can be bound to run, and the excess M is in the dormant state.
The system will find G from the global queue and assign it to an idle M through the scheduler. P will select an M to run. The number of M and G varies. P will have a local queue to represent the unprocessed G and M itself.There is a G being processed, and each time M finishes processing a G, it takes a G from the local queue and updates the schedtick field of P. If there is no G in the local queue, the number of G/P is taken from the global queue at one time.G, if there is nothing in the global queue, take half of it from other P's local queues.
Second, the goroutine of golang: if the Goroutine blocks, will the corresponding M also block?
In the following cases:
Goroutine is blocked due to atomic, mutex or channel operation calls
G blocking in this case will not block kernel thread M, so it will not cause context switching of M to involve G switching
Goroutine is blocked due to network request file IO, time.sleep, and ticker timer operations
The G blocking in this case will not block the kernel thread M, so it will not cause the context switch of M to involve the switch of G
Goroutine blocking due to calling the blocked system
This situation will cause M to block, the kernel will switch M, and the P associated with M will not wait for the blocking of M, which means that the current under PAll G cannot be executed, so at this time, P will be disassociated from the current M, and will be associated with another kernel thread M2. M2 may be a newly created kernel thread, or a previously idle kernel thread may be awakened to execute P'sG.
When M's system call blocking ends, the G will try to obtain an idle P for execution and put it into the P's local queue.If P cannot be obtained, then the thread M becomes dormant, joins the idle thread, and then the G is put into the global queue.
Third, how to block a Goroutine
Method 1: Receive data from a channel that does not send data
Method 2: Send data to a channel that does not receive data
Method 3: Receive data from an empty channel
Method 4: Send data toSend data in an empty channel
Method 5: Use select
边栏推荐
- Golang 垃圾回收机制详解
- FP7195芯片PWM转模拟调光至0.1%低亮度时恒流一致性的控制原理
- TypeScript 快速进阶
- PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
- Win10 can't start WampServer icon is orange solution
- 深入理解Golang之Map
- PyTorch③---torchvision中数据集的使用
- Win11系统找不到dll文件怎么修复
- Publish module to NPM should be how to operate?Solutions to problems and mistake
- 【使用Pytorch实现ResNet网络模型:ResNet50、ResNet101和ResNet152】
猜你喜欢
随机推荐
DP4344兼容CS4344-DA转换器
小T成长记-网络篇-1-什么是网络?
CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
推开机电的大门《电路》(二):功率计算与判断
TypeScript 快速进阶
The overlapping effect of the two surfaceviews is similar to the video and handout practice in the live effect
一篇文章彻底理解Redis的持久化:RDB、AOF
机器学习---监督学习、无监督学习
PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
STM32LL库使用——SPI通信
Win11 computer off for a period of time without operating network how to solve
How to add a one-key shutdown option to the right-click menu in Windows 11
pygame绘制弧线
How to solve Win11 without local users and groups
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
How to set the win10 taskbar does not merge icons
蓝牙温度检测系统(基于BT08-B蓝牙模块)
PyTorch⑤---卷积神经网络_卷积层
Letter combination of LeetCode2 phone number
win10系统更新错误代码0x80244022怎么办











