Is golang multi-process?
Golang is a multi-process, and its thread model is the MPG model. In general, Go processes and kernel threads correspond to many-to-many, so first of all, they must be multi-threaded. Golang has some so-called M ratio N model. N go routines can be created under M threads. Generally speaking, N is much larger than M. It is essentially a multi-threaded model. However, the scheduling of coroutines is determined by the runtime of Go. It is emphasized that developers should Use channels for synchronization between coroutines.
The operating environment of this article: Windows 10 system, go1.20 version, DELL G3 computer.
Golang is not a single process, but multi-threaded.
Golang has some so-called M ratio N model. N go routines can be created under M threads. Generally speaking, N is much larger than M. It is essentially a multi-threaded model, but the scheduling of coroutines is controlled by Go's runtime. The decision emphasizes that developers should use channels for synchronization between coroutines.
As for threads, since the language level is not open, you can understand it as a multi-coroutine model. Several go routines can be created on one thread. Generally speaking, the same number of threads as the number of CPU cores will be created. , of course, it is actually determined by the runtime.
Regarding the scheduling of goroutine, this is actually constantly evolving. I will only talk about the GMP model. Goroutine runs on M threads. Every time a task is executed, it will check the current P (processor). The executable queue contains executable goroutines. Once there is no executable goroutine on the current P, it will steal the goroutine in the executable queue of another P.
Theoretically, the creation of goroutine is only limited by memory. Generally speaking, the maximum is 2KB. For a thread with 2MB space, theoretically, it can easily reach 1,000. Of course, this is only an ideal situation, so the OS The number of threads will not increase as the number of goroutines created increases. Thread scheduling is relatively performance-intensive for Go. Frequent switching of schedules will only exist between goroutines, and threads will only maintain the same number of active threads as the number of CPUs.
About processes and threads
1. Process
A process is an execution process of a program in the operating system, and is the basic basis for resource allocation and scheduling by the system. Unit and process are a dynamic concept and are the basic unit for allocating and managing resources during program execution. Each process has its own address space. A process has at least 5 basic states: initial state, execution state, waiting state, ready state, and termination state.
In layman’s terms: a process is an executing program.
2. Thread
A thread is an execution instance of a process and the smallest unit of program execution. It is a basic unit that is smaller than a process and can run independently.
In layman's terms: A process can create multiple threads, and multiple threads in the same process can execute concurrently. If a program wants to run, there must be at least one process.
The above is the detailed content of Is golang multi-process?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics



Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

The FindStringSubmatch function finds the first substring matched by a regular expression: the function returns a slice containing the matching substring, with the first element being the entire matched string and subsequent elements being individual substrings. Code example: regexp.FindStringSubmatch(text,pattern) returns a slice of matching substrings. Practical case: It can be used to match the domain name in the email address, for example: email:="user@example.com", pattern:=@([^\s]+)$ to get the domain name match[1].

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Using predefined time zones in Go includes the following steps: Import the "time" package. Load a specific time zone through the LoadLocation function. Use the loaded time zone in operations such as creating Time objects, parsing time strings, and performing date and time conversions. Compare dates using different time zones to illustrate the application of the predefined time zone feature.

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.