Home > Common Problem > Is golang multi-process?

Is golang multi-process?

百草
Release: 2023-07-07 10:18:15
Original
1560 people have browsed it

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.

Is golang multi-process?

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.

Is golang multi-process?

Is golang multi-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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template