Go’s G-M-P Scheduler Model
Go’s G-M-P scheduler is the runtime mechanism that runs many goroutines across a smaller or equal number of operating-system threads. G means goroutine, M means machine (an OS thread), and P means processor: a runtime resource that an M must hold to execute Go code.
The model lets Go keep goroutines cheap while still using multiple CPU cores. It also prevents one goroutine waiting in a blocking system call from stopping unrelated goroutines.
The problem it solves
A goroutine is not an OS thread. It starts with a small, growable stack and is managed by the Go runtime, so creating thousands of goroutines is practical. An OS thread, by contrast, is a more expensive kernel-managed resource.
A simple runtime could assign one OS thread to each goroutine, but that would waste memory and scheduling effort. It would also create a serious problem: if one thread blocked in a file read or other system call, every goroutine assigned to that thread would wait with it.
Go instead maintains a pool of goroutines that are ready to run and schedules them onto a smaller set of OS threads. The G-M-P model is the bookkeeping that makes that arrangement work.
What G, M, and P represent
- G (goroutine): The runtime’s representation of a goroutine, including its stack, instruction position, and state. A G can be running, ready to run, waiting, or blocked in a system call.
- M (machine): An operating-system thread. An M is what actually executes instructions, but it needs a P before it can run ordinary Go code.
- P (processor): A scheduling context owned by the runtime. It contains resources and a local queue of runnable goroutines. The number of Ps is controlled by
GOMAXPROCS; by default, it is generally based on the machine’s available CPU parallelism.
A useful way to picture execution is:
A G runs on an M, while the M must hold a P.
The P is not a CPU core and does not represent a thread. It is permission and runtime state for running Go code. If there are four Ps, at most four Ms can execute Go code simultaneously, even if the runtime has created more OS threads for other purposes.
Where runnable goroutines go
When a goroutine is ready to execute, the scheduler places it on a run queue, a queue of runnable goroutines waiting for an M. Each P normally has its own local run queue. Keeping queues local reduces contention: several Ps can add and remove goroutines without constantly modifying one shared data structure.
For example, when a running goroutine creates another goroutine with go, the new G is usually placed on the current P’s local run queue. When a goroutine becomes runnable after waiting on a channel or a timer, the runtime also makes it available to a P.
There is also a global run queue shared by all Ps. It acts as overflow and as a way to distribute work when local queues do not provide enough. The scheduler periodically checks the global queue rather than allowing a P to rely only on its local work.
A simplified program can create this situation:
for i := 0; i < 1000; i++ {
go process(i)
}
The loop creates Gs; it does not create 1,000 OS threads. Those Gs wait in scheduler queues until Ps and Ms can run them.
How work stealing keeps Ps busy
A P first looks for work in its own local run queue. If that queue is empty, the scheduler looks for work elsewhere, including the global queue. It can also use work stealing, in which an idle P takes some runnable goroutines from another P’s local queue.
Stealing matters because work is not always created evenly. One P might receive many new goroutines while another finishes its current work early. Without stealing, the second P could sit idle while the first’s queue grows.
The idle P selects another P as a victim and takes a portion of its queued work, commonly about half rather than just one goroutine. The exact details are runtime implementation details, but the goal is stable: spread ready work without putting every scheduling operation behind one global lock.
This is why a goroutine does not permanently belong to the thread that created it. A G may begin on one P and later run on another, depending on queue state and scheduling decisions.
Why blocking system calls do not freeze everything
A system call is a request from a program to the operating system, such as reading a file or opening a socket. Some system calls block: the calling OS thread waits until the operation can continue.
When a goroutine enters a blocking system call, its M may block too. The runtime does not leave the P attached to that unusable M. It detaches the P and lets another M acquire it. That replacement M can then run the P’s queued goroutines while the original M waits in the kernel.
When the system call returns, the old M tries to reacquire a P. If one is available, it resumes Go execution there. If not, its goroutine is made runnable and placed on a queue, while the M may pause or perform other runtime work. The important point is that the blocked M does not monopolize the P.
If there are no spare Ms, the runtime can create another OS thread when needed, subject to operating-system and runtime limits. This is why a file operation in one goroutine does not normally prevent unrelated goroutines from making progress.
Network operations often use Go’s network poller, which asks the operating system to report readiness instead of blocking an M for each connection. That is a related optimization, but the G-M-P handoff also covers genuinely blocking system calls.
The model explains runtime traces and diagnostics that mention G, M, or P, as well as observations such as “more goroutines than threads.” Goroutines are the work, Ms are the threads that execute it, and Ps are the runtime scheduling contexts that keep those threads supplied with work.