Is a process the same as a Goroutine in Golang?

huangapple go评论83阅读模式
英文:

Is a process the same as a Goroutine in Golang?

问题

对于以下代码:

func main() {
    goRtns := runtime.NumGoroutine()
    fmt.Println("goroutines:", goRtns)
}

输出结果是 1。但这是在一个“进程”中,没有显式调用任何 goroutine:

“在计算机中,进程是正在执行的计算机程序的实例。它包含程序代码和当前的活动。根据操作系统(OS)的不同,一个进程可能由多个并发执行的线程组成。”

另外,根据 Krishna Sundarram 在他的博客文章“Goroutine 的工作原理”中所述:

“创建一个 goroutine 不需要太多的内存,只需要 2kB 的堆栈空间。它们通过根据需要分配和释放堆存储来增长。”

那么我的问题是:正在运行的代码实例(我的简单 main.go 函数)被运行时库视为一个 goroutine。我是否可以假设父进程被视为一个 goroutine,具有相同的内存分配、垃圾回收等规则?是否明智地假设读取有关 goroutine 执行的事实类似于运行它的总体 go 进程?根据上面关于 goroutine 的第二个引用,这听起来像是一个进程在程序执行时增长/缩小其堆栈空间,这是编程中的一个标准范例。

Go 进程和 goroutine 是否共享相同的规则?或者我对报告的 goroutine 数量有什么误解。

英文:

For the following code:

func main() {
	goRtns := runtime.NumGoroutine()
	fmt.Println("goroutines:", goRtns)
}

The output is 1. But this is within a "process," with no goroutines being explicitly called:

"In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently."

Also from the excellent "How goroutines work" blog post by Krishna Sundarram: http://blog.nindalf.com/how-goroutines-work/

"The creation of a goroutine does not require much memory - only 2kB of stack space. They grow by allocating and freeing heap storage as required."

My question is this, then: the instance of code that is running (my simple main.go function) is counted as a goroutine by the runtime library. Am I to assume that the parent process is treated as a go routine, with the same rules of memory allocation, garbage collection, etc? Would it be wise to assume reading a fact about a goroutine's execution is analogous to the overarching go process that runs it? With respect to the second quote on goroutines above, this sounds like a process growing/shrinking its stack space as a program executes which is a standard paradigm in programming.

Do go processes and routines share the same rules? Or am I just missing something about the reported number of goroutines.

答案1

得分: 5

在Golang中,进程(process)和Goroutine(协程)是不同的概念。在Go语言中,一切都是Goroutine,而不是进程。Goroutine是由Go运行时管理的轻量级线程。在你的代码中,只有一个Goroutine,因为只有main函数,没有在其中调用go关键字。它只是打印给定变量的内容。

如果在main函数中调用了go关键字,例如:

func main() {
    result := sq(sq(sq(gen(1, 2, 3, 4))))
    numGoroutines := runtime.NumGoroutine()
    fmt.Println("number goroutine =", numGoroutines)
    fmt.Println(<-result)
    fmt.Println(<-result)
    fmt.Println(<-result)
    fmt.Println(<-result)
}

你可以在这里找到sqgen函数的定义。现在,runtime.NumGoroutine()将返回5个Goroutine的数量。因为在gensq函数内部调用了go关键字,并将它们组合在一起,所以总共有4个Goroutine,再加上main函数本身,最终结果是5个Goroutine。

英文:

> Is a process the same as a Goroutine in Golang?

You are using the wrong term process here. In GO everything is a goroutine. as Volker said. and you can see gouroutine definition from here :

> A goroutine is a lightweight thread managed by the Go runtime.

for example in your code

func main() {
	goRtns := runtime.NumGoroutine()
	fmt.Println(&quot;goroutines:&quot;, goRtns)
}

this has only one goroutine because it has only main function and inside there are no go calling here. it just print the something from given variable.

another example if you have go called in your function main :

func main() {

	result := sq(sq(sq(gen(1, 2, 3, 4))))

	numGoroutines := runtime.NumGoroutine()
	fmt.Println(&quot;number goroutine = &quot;, numGoroutines)

	fmt.Println(&lt;-result)
	fmt.Println(&lt;-result)
	fmt.Println(&lt;-result)
	fmt.Println(&lt;-result)

}

you can find sq and gen function here. Now the runtime.NumGoroutine() will have 5 gorutine. Since inside function gen and sq we have go called and we combine theme here the total would be 4 + the main the final result is 5.

答案2

得分: 3

在Go语言中,对于术语"process"你需要小心。你引用了一个关于操作系统实体称为"processes"的定义,这个定义被广泛认可。很多人会理解这个用法。

但是这个术语有多重含义。C.A.R. Hoare的工作对我们也很重要:在他的"Communicating Sequential Processes"(CSP)代数中,术语"process"指的是一些很小的东西,更像是超轻量级的线程。他的代数属于一种叫做"process algebra"的数学范畴。

因此,可以认为"goroutine"是Go语言对CSP中的"process"的实现。

在这方面,Go语言与一个更早的语言Occam很相似。在Occam中,"process"意味着CSP中的一个进程。Occam广泛用于裸机(即没有操作系统)嵌入式编程,因此对于"process"这个术语从来没有任何歧义。

英文:

You have to be careful about the term process in Go. You quoted a definition about operating system entities called processes that will be recognised very widely. Many people will understand that usage.

But the term is overloaded. The work of C.A.R. Hoare also matters to us: in his Communicating Sequential Processes (CSP) algebra, the term process refers to something small - much more like a super-lightweight thread. His algebra is in a category of mathematics called process algebra.

So it is fair to assume that a goroutine is Go's implementation of a CSP process.

Go is like a much older language, Occam, in this respect. In Occam a process means a CSP process. Occam was widely used for bare-metal (I.e. no operating system) embedded programming and so there was never any ambiguity over the term process.

huangapple
  • 本文由 发表于 2017年2月12日 04:12:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/42180716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定