goroutine与内核和用户状态下的线程之间的关系是什么 [英] What is relationship between goroutine and thread in kernel and user state
问题描述
我对goroutine,用户线程和内核线程概念感到困惑
-
从有效开始引入goroutine ,那么
指论文中提到的哪一个?是用户线程还是内核线程? -
从 go-scheduler论文中,我了解了
M G P
以及为什么数字如此P
等于CPU的数量?如果所有的cpus都用于go程序,但是os系统中的其他程序没有cpu线程要执行? -
os系统生成了多少个内核线程?
让我们包括 go-scheduler 您链接到的页面.
并建立术语:
- M :操作系统线程,也可以称为内核线程
- P :处理器或调度上下文
- G :Goroutine
P 用于执行从许多goroutine到许多OS线程的映射.每个OS线程只有一个,该线程的数目由GOMAXPROCS
的值确定(默认情况下,由系统报告的CPU数目).
因此,按顺序回答您的问题:
- OS线程表示内核线程
-
GOMAXPROCS
默认为内核数,但是您可以更改.仅仅因为您可以在所有内核上运行并不意味着您没有为其他进程留出CPU时间.并发通常涉及大量等待IO.即使您正在疯狂地对事物进行哈希处理,内核调度程序也会引导您启动运行其他事物. - 所需要的OS线程数是多少.看
ps -eL
,我的系统当前有1434,其中一些是实际的内核作业,一些是我的go程序.
I am confused by goroutine, user thread, and kernel thread concepts
From effective go introduce goroutine, so what does
os threads
means which mentioned by the paper? Does it mean user thread or kernel thread?From go-scheduler paper, I learn about
M G P
, and why the numbers ofP
is equal to the numbers of CPU? If all the cpus serve for the go program, but other program in the os system have no cpu thread to execute?How many kernel thread generated by the os system?
Let's include the picture from the go-scheduler page you linked to.
And establish the terminology:
- M: OS thread, can also be called a kernel thread
- P: processor, or scheduling context
- G: Goroutine
goroutines are what we are most familiar with in Go, and could be considered user threads. A more technical name for those is Green Threads.
P is used to perform the mapping from many goroutines to many OS threads. There is one per OS thread, and the number is determined by the value of GOMAXPROCS
(by default, the number of CPUs as reported by your system).
So, to answer your questions in order:
- OS thread means kernel thread
GOMAXPROCS
defaults to the number of cores, but you can change that. Just because you can run on all cores does not mean you're not leaving CPU time for other processes. Concurrency usually involves a lot of waiting for IO. Even if you're going crazy hashing things, the kernel scheduler will boot you off to run other things.- there are as many OS threads as needed. Looking at
ps -eL
, my system currently has 1434, some of them actual kernel jobs, some for my go program.
You can find a really good explanation of OS vs Green threads in this answer
这篇关于goroutine与内核和用户状态下的线程之间的关系是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!