进程和Golang中的Goroutine一样吗?
Is a process the same as a Goroutine in Golang?
对于以下代码:
func main() {
goRtns := runtime.NumGoroutine()
fmt.Println("goroutines:", goRtns)
}
输出为1
。但这是在 "process," 中,没有明确调用 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."
也来自 Krishna Sundarram 的优秀 "How goroutines work" 博客 post: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."
我的问题是这样的: 运行 的代码实例(我的简单 main.go 函数)被运行时库计为 goroutine。我是否假设父进程被视为一个 go 例程,具有相同的内存分配、垃圾收集等规则?假设阅读有关 goroutine 执行的事实类似于运行它的总体 go 进程是否明智?关于上面 goroutines 的第二个引用,这听起来像是一个进程 growing/shrinking 它的堆栈 space 作为程序执行这是编程中的标准范例。
go 进程和例程共享相同的规则吗?或者我只是遗漏了一些关于报告的 goroutines 数量的信息。
Is a process the same as a Goroutine in Golang?
您在这里使用了错误的术语 process
。在 GO 中,一切都是 goroutine。作为 Volker said. and you can see gouroutine definition from here :
A goroutine is a lightweight thread managed by the Go runtime.
例如在您的代码中
func main() {
goRtns := runtime.NumGoroutine()
fmt.Println("goroutines:", goRtns)
}
这只有一个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)
}
你可以找到 sq 和 gen 函数 here。现在 runtime.NumGoroutine()
将有 5 个协程。由于在函数 gen
和 sq
中我们调用了 go
并且我们在这里组合主题总数将是 4 + main
最终结果是 5。
你必须小心 Go 中的术语 process。您引用了一个关于名为 processes 的操作系统实体的定义,该定义将得到广泛认可。很多人会理解这种用法。
但是这个词已经过载了。 C.A.R 的作品。 Hoare 对我们也很重要:在他的 Communicating Sequential Processes (CSP) 代数中,术语 process 指的是一些小东西 - 更像是 super-lightweight线程。他的代数属于称为 过程代数.
的数学类别
因此可以公平地假设 goroutine 是 Go 对 CSP process.
的实现
在这方面,Go 就像一门古老得多的语言 Occam。在 Occam 中,进程意味着 CSP 进程。 Occam 被广泛用于 bare-metal(即无操作系统)嵌入式编程,因此术语 process.
从来没有任何歧义
对于以下代码:
func main() {
goRtns := runtime.NumGoroutine()
fmt.Println("goroutines:", goRtns)
}
输出为1
。但这是在 "process," 中,没有明确调用 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."
也来自 Krishna Sundarram 的优秀 "How goroutines work" 博客 post: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."
我的问题是这样的: 运行 的代码实例(我的简单 main.go 函数)被运行时库计为 goroutine。我是否假设父进程被视为一个 go 例程,具有相同的内存分配、垃圾收集等规则?假设阅读有关 goroutine 执行的事实类似于运行它的总体 go 进程是否明智?关于上面 goroutines 的第二个引用,这听起来像是一个进程 growing/shrinking 它的堆栈 space 作为程序执行这是编程中的标准范例。
go 进程和例程共享相同的规则吗?或者我只是遗漏了一些关于报告的 goroutines 数量的信息。
Is a process the same as a Goroutine in Golang?
您在这里使用了错误的术语 process
。在 GO 中,一切都是 goroutine。作为 Volker said. and you can see gouroutine definition from here :
A goroutine is a lightweight thread managed by the Go runtime.
例如在您的代码中
func main() {
goRtns := runtime.NumGoroutine()
fmt.Println("goroutines:", goRtns)
}
这只有一个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)
}
你可以找到 sq 和 gen 函数 here。现在 runtime.NumGoroutine()
将有 5 个协程。由于在函数 gen
和 sq
中我们调用了 go
并且我们在这里组合主题总数将是 4 + main
最终结果是 5。
你必须小心 Go 中的术语 process。您引用了一个关于名为 processes 的操作系统实体的定义,该定义将得到广泛认可。很多人会理解这种用法。
但是这个词已经过载了。 C.A.R 的作品。 Hoare 对我们也很重要:在他的 Communicating Sequential Processes (CSP) 代数中,术语 process 指的是一些小东西 - 更像是 super-lightweight线程。他的代数属于称为 过程代数.
的数学类别因此可以公平地假设 goroutine 是 Go 对 CSP process.
的实现在这方面,Go 就像一门古老得多的语言 Occam。在 Occam 中,进程意味着 CSP 进程。 Occam 被广泛用于 bare-metal(即无操作系统)嵌入式编程,因此术语 process.
从来没有任何歧义