doujia1679 2017-02-11 20:12
浏览 73
已采纳

流程与Golang中的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.

  • 写回答

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)
    
        fmt.Println(<-result)
        fmt.Println(<-result)
        fmt.Println(<-result)
        fmt.Println(<-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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题