goroutine与内核和用户状态下的线程之间的关系是什么 [英] What is relationship between goroutine and thread in kernel and user state

查看:185
本文介绍了goroutine与内核和用户状态下的线程之间的关系是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对goroutine,用户线程和内核线程概念感到困惑

  1. 有效开始引入goroutine ,那么指论文中提到的哪一个?是用户线程还是内核线程?

  2. go-scheduler论文中,我了解了M G P以及为什么数字如此P等于CPU的数量?如果所有的cpus都用于go程序,但是os系统中的其他程序没有cpu线程要执行?

  3. os系统生成了多少个内核线程?

解决方案

让我们包括 go-scheduler 您链接到的页面.

并建立术语:

  • M :操作系统线程,也可以称为内核线程
  • P :处理器或调度上下文
  • G :Goroutine
goroutines是我们在Go中最熟悉的东西,可以被认为是用户线程.这些的更专业的名称是绿色线程.

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

  1. From effective go introduce goroutine, so what does os threads means which mentioned by the paper? Does it mean user thread or kernel thread?

  2. From go-scheduler paper, I learn about M G P, and why the numbers of P 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?

  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆