Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is relationship between goroutine and thread in kernel and user state

Tags:

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?

like image 500
Jim Green Avatar asked Feb 06 '18 08:02

Jim Green


People also ask

What is the relationship between user thread and kernel thread?

There is one-to-one relationship of user-level thread to the kernel-level thread. This model provides more concurrency than the many-to-one model. It also allows another thread to run when a thread makes a blocking system call. It supports multiple threads to execute in parallel on microprocessors.

Is Goroutine same as thread?

Threads are hardware dependent. Goroutines have easy communication medium known as channel. Thread does not have easy communication medium. Due to the presence of channel one goroutine can communicate with other goroutine with low latency.

What exactly is a Goroutine?

A goroutine is a lightweight execution thread in the Go programming language and a function that executes concurrently with the rest of the program. Goroutines are incredibly cheap when compared to traditional threads as the overhead of creating a goroutine is very low.

What are threads discuss common ways of establishing relationship between user and kernel thread?

There exist a strong a relationship between user level threads and kernel level threads. Dependencies between ULT and KLT : Use of Thread Library : Thread library acts as an interface for the application developer to create number of threads (according to the number of subtasks) and to manage those threads.


1 Answers

Let's include the picture from the go-scheduler page you linked to.

enter image description here

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

like image 121
Marc Avatar answered Oct 28 '22 18:10

Marc