Layered design in software engineering

From Processes to Threads to Goroutines Part3

Now, what Go does, it takes process with one thread in it, one main thread and you can have multiple Goroutines that are all executing in that same thread.
So, from the operating system point of view, all it does is schedule the main thread but then within Go, you can have multiple Goroutines that are executing within that thread all alternating within that thread.
So, maybe Goroutine one executes, then two, then three, or the while from the operating system point of view, nothing is changing, it's got its main thread running.

But inside a Go, Go can start switching basically these Goroutines which are like threads inside the main thread. So, that process of doing the switching determining which Goroutine executing and what time, that scheduling process, that's done by what's called the Go Runtime Scheduler.

The Go Runtime Scheduler, it does scheduling within an operating system thread. So, you see how the scheduling task is separated between the operating system and the Go Runtime Scheduling. ‎

The operating system schedules which thread, which operating system thread runs at a time. Then once the main thread is running, your Go program is running within the main thread, an operating system thread. Once that's running, the Runtime Scheduler can choose different Goroutines to execute at different times underneath that main thread. Now, the Go Runtime Scheduler uses a logical processor.

This logical processor is mapped to a thread. So, typically like default, you're going to have one logical process are mapped to the main thread, and then the Goroutine is going to run on that logical processor which means it's running in that main thread. ‎

Then notice it since all these Goroutines are basically running in one thread, one main thread, there is not an opportunity for actual parallelism like this, it's all concurrent.‎

You can as a programmer, you can say look, I don't want one logical process I want to logical processors or three or four, and you would do this according to how many cores you had presumably that would be sort of the obvious way. You might say look I got four cores I'm going to have four logical processes. Then what will happen is the Go Runtime Scheduler.‎
it will take these goroutines and map them to different logical processes, each logical processor can be mapped to a different operating system thread and the operating system will map those threads to different cores.

Now still this task of hardware mapping, determining this Goroutine which core is at on, that task is still not done by the programmer, but the programmer can determine how many logical processors are going to be used. So, and by default, it'll be one in which case it's got to be concurrent behavior but you can increase that if you want to.‎



تعليقات