当前位置:网站首页>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
边栏推荐
- PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
- Fast advanced TypeScript
- FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
- 日常-笔记
- win10无法直接用照片查看器打开图片怎么办
- 为android系统添加产品的过程
- 刷卡芯片CI520可直接PIN对PIN替换CV520支持SPI通讯接口
- FP7195芯片PWM转模拟调光至0.1%低亮度时恒流一致性的控制原理
- 实战美团Nuxt +Vue全家桶,服务端渲染,邮箱验证,passport鉴权服务,地图API引用,mongodb,redis等技术点
- What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
猜你喜欢
随机推荐
General syntax and usage instructions of SQL (picture and text)
TCP三次握手、四次挥手
arm ldr系列指令
推开机电的大门《电路》(二):功率计算与判断
vscode镜像
Win10电脑需要安装杀毒软件吗?
LORA芯片ASR6505无线远距离传输8位MCU
二叉树遍历之后序遍历(非递归、递归)入门详解
如何用硬币模拟1/3的概率,以及任意概率?
Win10无法连接打印机怎么办?不能使用打印机的解决方法
Win11 system cannot find dll file how to fix
CS4398音频解码替代芯片DP4398完全兼容DAC解码
Win11没有本地用户和组怎么解决
Binder机制(中篇)
PyTorch②---transforms结构及用法
The SSE instructions into ARM NEON
Win7怎么干净启动?如何只加载基本服务启动Win7系统
Detailed explanation of RecyclerView series article directory
Golang 垃圾回收机制详解
PyTorch③---torchvision中数据集的使用










