简体   繁体   English

一个过程和Golang中的Goroutine一样吗?

[英]Is a process the same as a Goroutine in Golang?

For the following code: 对于以下代码:

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

The output is 1 . 输出为1 But this is within a "process," with no goroutines being explicitly called: 但这是在一个“过程”中,没有明确调用goroutines:

"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." “在计算中,进程是正在执行的计算机程序的实例。它包含程序代码及其当前活动。根据操作系统(OS),进程可以由执行的多个执行线程组成同时说明。“

Also from the excellent "How goroutines work" blog post by Krishna Sundarram: http://blog.nindalf.com/how-goroutines-work/ 还有来自Krishna Sundarram的优秀“How goroutines work”博客文章: 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." “创建一个goroutine不需要太多内存 - 只有2kB的堆栈空间。它们通过根据需要分配和释放堆存储来增长。”

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. 我的问题是,然后:正在运行的代码实例(我的简单main.go函数)被运行时库计为goroutine。 Am I to assume that the parent process is treated as a go routine, with the same rules of memory allocation, garbage collection, etc? 我是否假设父进程被视为go例程,具有相同的内存分配,垃圾收集等规则? Would it be wise to assume reading a fact about a goroutine's execution is analogous to the overarching go process that runs it? 假设阅读一个关于goroutine执行的事实类似于运行它的总体进程是否明智? 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. 关于上面关于goroutines的第二个引用,这听起来像一个程序在程序执行时增加/缩小其堆栈空间的过程,这是编程中的标准范例。

Do go processes and routines share the same rules? 流程和例程是否共享相同的规则? Or am I just missing something about the reported number of goroutines. 或者我只是遗漏了报道的goroutines数量。

Is a process the same as a Goroutine in Golang? 一个过程和Golang中的Goroutine一样吗?

You are using the wrong term process here. 您在这里使用了错误的术语process In GO everything is a goroutine. 在GO中,一切都是goroutine。 as Volker said. 正如沃尔克所说。 and you can see gouroutine definition from here : 你可以从这里看到gouroutine的定义:

A goroutine is a lightweight thread managed by the Go runtime. goroutine是由Go运行时管理的轻量级线程。

for example in your code 例如在你的代码中

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

this has only one goroutine because it has only main function and inside there are no go calling here. 这只有一个goroutine,因为它只有main功能,里面没有go调用这里。 it just print the something from given variable. 它只是打印给定变量的东西。

another example if you have go called in your function main : 如果您有另一个例子go在你的函数调用main

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)

}

you can find sq and gen function here . 你可以在这里找到sq和gen功能。 Now the runtime.NumGoroutine() will have 5 gorutine. 现在runtime.NumGoroutine()将有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. 由于内部函数gensq ,我们go打来电话,我们结合主题这里的总数是4 +的main最后的结果是5。

You have to be careful about the term process in Go. 你必须小心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 CAR 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. CAR Hoare的工作对我们也很重要:在他的通信顺序过程 (CSP)代数中,术语过程指的是一些小东西 - 更像是一个超轻量级的线程。 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 . 因此,假设goroutine是Go的CSP 流程实现是公平的。

Go is like a much older language, Occam, in this respect. 在这方面,围棋就像是一部陈旧的语言,奥卡姆。 In Occam a process means a CSP process. 在Occam中,流程意味着CSP流程。 Occam was widely used for bare-metal (Ie no operating system) embedded programming and so there was never any ambiguity over the term process . Occam被广泛用于裸机(即无操作系统)嵌入式编程,因此在术语过程中从不存在任何歧义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM