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.
1. About processes and threads
1. Process
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.
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 be executed concurrently. If a program wants to run, there must be at least one process.
2. About concurrency and parallelism
1. Concurrency
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.
2. Parallel
Multiple threads can be executed at the same time. In each time period, multiple threads can be executed at the same time.
3. In layman’s terms
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.
3. Goroutine coroutine and main thread
1. The main thread
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.
2. Coroutines
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.
3. Multi-coroutine and multi-threading
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.
4. Practical operation of go keywords
1. Sequential execution
package main import "fmt" func test() { for i := 0; i <h3 id="strong-Join-go-strong"><strong>2. Join go</strong></h3><pre class="brush:php;toolbar:false">package main import "fmt" func test() { for i := 0; i <h3 id="strong-Joining-time-strong"><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="strong-When-the-main-thread-executes-quickly-strong"><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="strong-sync-WaitGroup-solves-the-problem-of-not-waiting-strong"><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="strong-Concurrent-execution-of-multiple-coroutines-strong"><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="strong-Set-the-number-of-cpu-cores-occupied-by-golang-when-running-Not-very-important-strong"><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!

Go's "strings" package provides rich features to make string operation efficient and simple. 1) Use strings.Contains() to check substrings. 2) strings.Split() can be used to parse data, but it should be used with caution to avoid performance problems. 3) strings.Join() is suitable for formatting strings, but for small datasets, looping = is more efficient. 4) For large strings, it is more efficient to build strings using strings.Builder.

Go uses the "strings" package for string operations. 1) Use strings.Join function to splice strings. 2) Use the strings.Contains function to find substrings. 3) Use the strings.Replace function to replace strings. These functions are efficient and easy to use and are suitable for various string processing tasks.

ThebytespackageinGoisessentialforefficientbyteslicemanipulation,offeringfunctionslikeContains,Index,andReplaceforsearchingandmodifyingbinarydata.Itenhancesperformanceandcodereadability,makingitavitaltoolforhandlingbinarydata,networkprotocols,andfileI

Go uses the "encoding/binary" package for binary encoding and decoding. 1) This package provides binary.Write and binary.Read functions for writing and reading data. 2) Pay attention to choosing the correct endian (such as BigEndian or LittleEndian). 3) Data alignment and error handling are also key to ensure the correctness and performance of the data.

The"bytes"packageinGooffersefficientfunctionsformanipulatingbyteslices.1)Usebytes.Joinforconcatenatingslices,2)bytes.Bufferforincrementalwriting,3)bytes.Indexorbytes.IndexByteforsearching,4)bytes.Readerforreadinginchunks,and5)bytes.SplitNor

Theencoding/binarypackageinGoiseffectiveforoptimizingbinaryoperationsduetoitssupportforendiannessandefficientdatahandling.Toenhanceperformance:1)Usebinary.NativeEndianfornativeendiannesstoavoidbyteswapping.2)BatchReadandWriteoperationstoreduceI/Oover

Go's bytes package is mainly used to efficiently process byte slices. 1) Using bytes.Buffer can efficiently perform string splicing to avoid unnecessary memory allocation. 2) The bytes.Equal function is used to quickly compare byte slices. 3) The bytes.Index, bytes.Split and bytes.ReplaceAll functions can be used to search and manipulate byte slices, but performance issues need to be paid attention to.

The byte package provides a variety of functions to efficiently process byte slices. 1) Use bytes.Contains to check the byte sequence. 2) Use bytes.Split to split byte slices. 3) Replace the byte sequence bytes.Replace. 4) Use bytes.Join to connect multiple byte slices. 5) Use bytes.Buffer to build data. 6) Combined bytes.Map for error processing and data verification.


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

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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

Zend Studio 13.0.1
Powerful PHP integrated development environment
