Table of Contents
1. About processes and threads
1. Process
2. Thread
2. About concurrency and parallelism
1. Concurrency
2. Parallel
3. In layman’s terms
3. Goroutine coroutine and main thread
1. The main thread
2. Coroutines
3. Multi-coroutine and multi-threading
4. Practical operation of go keywords
1. Sequential execution
2. Join go
3. Joining time
4. When the main thread executes quickly
5. sync .WaitGroup solves the problem of not waiting
6. Concurrent execution of multiple coroutines
5. Set the number of cpu cores occupied by golang when running (Not very important)
Home Backend Development Golang Does golang have a process?

Does golang have a process?

Dec 26, 2022 pm 05:38 PM
golang go language 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.

Does golang have a process?

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.

Does golang have a process?

Does golang have a 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.

Does golang have a process?

2. Parallel

Multiple threads can be executed at the same time. In each time period, multiple threads can be executed at the same time.

Does golang have a process?

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.

Does golang have a process?

# 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")
}
Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Golang's Purpose: Building Efficient and Scalable Systems Golang's Purpose: Building Efficient and Scalable Systems Apr 09, 2025 pm 05:17 PM

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

In Go programming, how to correctly manage the connection and release resources between Mysql and Redis? In Go programming, how to correctly manage the connection and release resources between Mysql and Redis? Apr 02, 2025 pm 05:03 PM

Resource management in Go programming: Mysql and Redis connect and release in learning how to correctly manage resources, especially with databases and caches...

How to ensure concurrency is safe and efficient when writing multi-process logs? How to ensure concurrency is safe and efficient when writing multi-process logs? Apr 02, 2025 pm 03:51 PM

Efficiently handle concurrency security issues in multi-process log writing. Multiple processes write the same log file at the same time. How to ensure concurrency is safe and efficient? This is a...

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Do I need to install an Oracle client when connecting to an Oracle database using Go? Do I need to install an Oracle client when connecting to an Oracle database using Go? Apr 02, 2025 pm 03:48 PM

Do I need to install an Oracle client when connecting to an Oracle database using Go? When developing in Go, connecting to Oracle databases is a common requirement...

See all articles