


How Does the Go Scheduler Create New M and P When Goroutines Perform Blocking Operations?
When Go Scheduler Creates New M and P in the GMP Model
In Go's GMP (Goroutine, Machine, Processor) model, the scheduler manages the creation of M (Machine) and P (Processor) based on specific conditions.
M Creation
M is created in response to specific events, such as:
- Blocking Operations: When a goroutine performs a blocking operation (e.g., I/O, syscall), the M associated with that goroutine is blocked. To continue executing the goroutine, the scheduler creates a new M.
- Scheduling Pipelines: When the global G queue, which stores goroutines waiting for execution, has more goroutines than the number of available M, the scheduler creates new M to execute the goroutines.
P Creation
P is created at program startup based on the GOMAXPROCS environment variable, which specifies the maximum number of available P. The default value is the number of logical CPUs on the system.
Example Analysis
In your example code, you have two batches of goroutines running database operations. Each goroutine performs a blocking I/O operation.
- First Batch: The scheduler will create 8 M (since you have 8 virtual cores) and 1 P to run the first batch of goroutines. Each M will execute a goroutine from the local queue of P.
- Second Batch: Since the blocking operations cause the initial M to become blocked, the scheduler will create new M for the remaining goroutines in the second batch. The M will be associated with a new P, even though the number of P remains at 1.
Therefore, in your case, the scheduler will create more than 8 M for the second batch of goroutines because the operations are blocking. The P will be limited to 1 based on the GOMAXPROCS value, but M will be created dynamically as needed.
Additional Resources
For further understanding, refer to the following resources:
- https://www.programmersought.com/article/79557885527/
- https://blog.golang.org/go-goroutine-os-thread-and-cpu-management
The above is the detailed content of How Does the Go Scheduler Create New M and P When Goroutines Perform Blocking Operations?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

This article describes how to start an external editor (such as Vim or Nano) in a Go program and wait for the user to close the editor before the program continues to execute. By setting cmd.Stdin, cmd.Stdout, and cmd.Stderr, the editor can interact with the terminal to solve the problem of startup failure. At the same time, a complete code example is shown and precautions are provided to help developers implement this function smoothly.

Goprovidessimpleandefficientfilehandlingusingtheosandbufiopackages.Toreadasmallfileentirely,useos.ReadFile,whichloadsthecontentintomemorysafelyandautomaticallymanagesfileoperations.Forlargefilesorincrementalprocessing,bufio.Scannerallowsline-by-liner

struct{} is a fieldless structure in Go, which occupies zero bytes and is often used in scenarios where data is not required. It is used as a signal in the channel, such as goroutine synchronization; 2. Used as a collection of value types of maps to achieve key existence checks in efficient memory; 3. Definable stateless method receivers, suitable for dependency injection or organization functions. This type is widely used to express control flow and clear intentions.

This article aims to resolve EOF (End-of-File) errors encountered when developing WebSocket using Go. This error usually occurs when the server receives the client message and the connection is unexpectedly closed, resulting in the subsequent messages being unable to be delivered normally. This article will analyze the causes of the problem, provide code examples, and provide corresponding solutions to help developers build stable and reliable WebSocket applications.

MiddlewareinGowebserversarefunctionsthatinterceptHTTPrequestsbeforetheyreachthehandler,enablingreusablecross-cuttingfunctionality;theyworkbywrappinghandlerstoaddpre-andpost-processinglogicsuchaslogging,authentication,CORS,orerrorrecovery,andcanbechai

Use the encoding/json package of the standard library to read the JSON configuration file; 2. Use the gopkg.in/yaml.v3 library to read the YAML format configuration; 3. Use the os.Getenv or godotenv library to overwrite the file configuration; 4. Use the Viper library to support advanced functions such as multi-format configuration, environment variables, automatic reloading; it is necessary to define the structure to ensure type safety, properly handle file and parsing errors, correctly use the structure tag mapping fields, avoid hard-coded paths, and recommend using environment variables or safe configuration storage in the production environment. It can start with simple JSON and migrate to Viper when the requirements are complex.

This article aims to help developers understand and solve nil pointer exceptions caused by XORKeyStream function that may be encountered when using the CFB (Cipher Feedback) mode of Go language for AES encryption. Ensure the encryption process goes smoothly by analyzing common causes of errors and providing the correct code examples. The focus is on the correct use of initialization vectors (IVs) and the importance of understanding the AES block size.

To compile Go code for ARM architecture, simply set the environment variables and use the gobuild command. 1. Set GOOS=linux and GOARCH=arm (32-bit) or arm64 (64-bit) to specify the target platform. 2. Optionally, set GOARM=7 for 32-bit ARM to specify the ARMv7 instruction set. 3. If no CGO is required, set CGO_ENABLED=0 to ensure static linking. 4. Run the command such as GOOS=linuxGOARCH=arm64CGO_ENABLED=0gobuild-omyapp-arm64 to generate a binary file. 5. Copy the generated binary file to an ARM device (such as Raspber
