Explain the Go scheduler and the GMP model.
Answer
Go's runtime uses a GMP model — G (goroutine), M (OS thread / machine), P (logical processor / scheduler context). Each P has a local run queue of goroutines and is bound to one M at a time. The number of Ps is set by GOMAXPROCS (defaults to the number of CPU cores), limiting true parallelism. The scheduler is cooperative/preemptive: goroutines yield at function calls and since Go 1.14 can be asynchronously preempted via signals, preventing a CPU-bound loop from monopolizing a thread. When a goroutine blocks on a syscall, its M detaches from the P so the P can pick up another M and continue running other goroutines — this is how Go achieves high concurrency even with blocking I/O.
Previous
What is fmt.Errorf with %w and how do errors.Is / errors.As work?
Next
How does Go's garbage collector work?