当前位置:网站首页>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
边栏推荐
- Tensorflow常用函数
- 日常-笔记
- IPV4和IPV6是什么?
- FP5207电池升压 5V9V12V24V36V42V大功率方案
- FP7195转模拟调光技术解决智能家居调光频闪和电感噪音的原理
- Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
- PyTorch⑤---卷积神经网络_卷积层
- Bash shell位置参数
- How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?
- 小T成长记-网络篇-1-什么是网络?
猜你喜欢
随机推荐
BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
Binder机制(中篇)
Fast advanced TypeScript
FP7195转模拟恒流调光芯片在机器视觉光源的应用优势
golang之GMP调度模型
实战美团Nuxt +Vue全家桶,服务端渲染,邮箱验证,passport鉴权服务,地图API引用,mongodb,redis等技术点
如何用硬币模拟1/3的概率,以及任意概率?
Win10安装了固态硬盘还是有明显卡顿怎么办?
Win10无法连接打印机怎么办?不能使用打印机的解决方法
LeetCode2 电话号码的字母组合
PyTorch③---torchvision中数据集的使用
FP6195耐压60V电流降压3.3V5V模块供电方案
基于51单片机和物联网的智能家居系统(ESP8266物联网模块)
casbin模型
蓝牙温度检测系统(基于BT08-B蓝牙模块)
PyTorch②---transforms结构及用法、常见的Transforms
STM32LL库——USART中断接收不定长信息
Win11 system cannot find dll file how to fix
DP4344兼容CS4344-DA转换器










