By the time the program is ready to start executing, the runtime has machines and processors set up. The runtime would request the OS to start an ample number of Machines (M), GOMAXPROCS number of Processors to execute goroutines (G). It is important to understand that M is the actual unit of execution and G is the logical unit of execution. However, they require P to actually execute G against the M. Let's look at a possible scenario to better explain the scheduling process. First let's look at the components we shall be using for the scenario:
- We have a set of M ready to run: M1...Mn
- We also have two Ps: P1 and P2 with runqueues—runq1 and runq2 respectively
- Last but not least, we also have 20 goroutines, G1...G20, ...