当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Binder机制(下篇)
PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
STM32F1和F4的区别
5. Use RecyclerView to elegantly achieve waterfall effect
Win11没有本地用户和组怎么解决
使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
轻量化AlphaPose
【系统设计与实现】基于flink的分心驾驶预测与数据分析系统
Binder机制(中篇)
CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
DP1332E刷卡芯片支持NFC内置mcu智能楼宇/终端poss机/智能门锁
FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?
基于深度学习的配准框架
LORA芯片ASR6505无线远距离传输8位MCU
2021-10-14
发布模块到npm应该怎么操作?及错误问题解决方案
DP4056电源保护芯片锂电池pin对pinTP4056