Home >Backend Development >Golang >Does golang have a process?
Golang has processes. A process is an execution process of a program in the operating system, and is the basic unit for resource allocation and scheduling by the system; a process is a dynamic concept, and is the basic unit for allocating and managing resources during the execution of a program. Each process has a own address space. The go language supports multiple processes, and its thread model is the MPG model. In general, there is a many-to-many correspondence between Go processes and kernel threads.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
The process is the program in the operating system An execution process is the basic unit for resource allocation and scheduling by the system. Process is a dynamic concept and 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.
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 be executed concurrently. If a program wants to run, there must be at least one process.
Multiple threads compete for a position at the same time, and only the ones that compete can be executed. Only one thread is executed in each time period.
Multiple threads can be executed at the same time. In each time period, multiple threads can be executed at the same time.
Multi-threaded programs running on a single-core CPU are concurrency, and on multi-core CPUs Running is parallel. If the number of threads is greater than the number of CPU cores, a multi-threaded program will be both concurrent and parallel on multiple CPUs.
can be understood as a thread or Process, multiple coroutines can be enabled on the main thread of a golang program. Multiple coroutines in golang can achieve concurrency or parallelism.
can be understood as user-level threads, which are transparent to the kernel, that is, the system does not know the existence of coroutines. It is completely scheduled by the user's own program. A major feature of golang is that it supports coroutines natively from the language perspective. You can create a coroutine by adding the go keyword in front of a function or method. It can be said that the coroutine in golang is goroutine.
# Multi-coroutine in Golang is somewhat similar to multi-threading in other languages.
Each goroutine (coroutine) in Golang occupies much less memory by default than Java and C threads. OS threads (operating system threads) generally have a fixed stack memory (usually about 2MB). A goroutine (coroutine) occupies very small memory, only about 2KB. The scheduling overhead of multi-goroutine goroutine switching is much less than that of threads. This is one of the reasons why more and more large companies are using Golang.
package main import "fmt" func test() { for i := 0; i <h3 id="2.%20%E5%8A%A0%E5%85%A5go"><strong>2. Join go</strong></h3><pre class="brush:php;toolbar:false">package main import "fmt" func test() { for i := 0; i <h3 id="3.%20%E5%8A%A0%E5%85%A5%E6%97%B6%E9%97%B4"><strong>3. Joining time</strong></h3><pre class="brush:php;toolbar:false">package main import ( "fmt" "time" ) // 加入时间 func test1() { for i := 0; i <h3 id="4.%20%E4%B8%BB%E7%BA%BF%E7%A8%8B%E6%89%A7%E8%A1%8C%E5%BF%AB%E7%9A%84%E6%83%85%E5%86%B5"><strong>4. When the main thread executes quickly</strong></h3><pre class="brush:php;toolbar:false">package main import ( "fmt" "time" ) func test1() { for i := 0; i <h3 id="5.%20sync.WaitGroup%E8%A7%A3%E5%86%B3%E4%B8%8D%E7%AD%89%E5%BE%85"><strong>5. sync .WaitGroup solves the problem of not waiting</strong></h3><pre class="brush:php;toolbar:false">package main import ( "fmt" "time" "sync" ) var wg sync.WiatGroup func test2() { for i := 0; i <h3 id="6.%20%E5%A4%9A%E5%8D%8F%E7%A8%8B%E5%B9%B6%E5%8F%91%E5%B9%B6%E8%A1%8C%E6%89%A7%E8%A1%8C"><strong>6. Concurrent execution of multiple coroutines</strong></h3><pre class="brush:php;toolbar:false">package main import ( "fmt" "time" "sync" ) func hello(num int) { defer wg.Done() for i := 0; i <h2 id="%E4%BA%94%E3%80%81%E8%AE%BE%E7%BD%AEgolang%E8%BF%90%E8%A1%8C%E6%97%B6%E5%8D%A0%E7%94%A8%E7%9A%84cpu%E6%A0%B8%E6%95%B0%E9%87%8F%EF%BC%88%E4%B8%8D%E6%98%AF%E5%BE%88%E9%87%8D%E8%A6%81%EF%BC%89"><strong>5. Set the number of cpu cores occupied by golang when running (Not very important) </strong></h2><pre class="brush:php;toolbar:false">package main import ( "fmt" "runtime" ) func main() { // 设置程序占用几个cpu进行执行,默认是全部 // 获取计算机cpu个数 cpuNum := runtime.NumCPU() fmt.Println(cpuNum) // 6 我本机电脑是6核cpu // 设置占用cpu个数 runtime.GOMAXPROCS(2) fmt.Println("ok") }
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of Does golang have a process?. For more information, please follow other related articles on the PHP Chinese website!