2017-02-11 20:12
浏览 73


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:

"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.

图片转代码服务由CSDN问答提供 功能建议


  func main(){  
 goRtns:= runtime.NumGoroutine()
 fmt.Println(“ goroutines:”,goRtns)

输出为 1 。 但这是在“进程”内,没有明确调用goroutine:

”在计算中,进程是正在执行的计算机程序的实例。它包含程序代码 及其当前的活动。取决于操作系统(OS),一个进程可能由同时执行指令的多个执行线程组成。”

“优秀的goroutines如何工作” “克里希纳·桑达拉姆(Krishna Sundarram)撰写的博客文章: /


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

go进程和例程共享相同的规则吗? 还是我只是缺少有关报告的goroutine数量的信息。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongle19863 2017-02-11 22:48

    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("goroutines:", 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("number goroutine = ", numGoroutines)

    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.

    点赞 评论
  • dqwp81696 2017-02-12 09:40

    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.

    点赞 评论

相关推荐 更多相似问题