In a version prior to the release of go 1.5 of the Tour of Go website, there's a piece of code that looks like this.
package main
import (
"fmt"
"runtime"
)
func say(s string) {
for i := 0; i < 5; i++ {
runtime.Gosched()
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
Run Code Online (Sandbox Code Playgroud)
The output looks like this:
hello
world
hello
world
hello
world
hello
world
hello
Run Code Online (Sandbox Code Playgroud)
What is bothering me is that when runtime.Gosched()
is …
当没有设置相同名称的环境变量时,是否可以保证GOMAXPROCS设置为1?
此代码显示值:
package main
import (
"runtime"
"fmt"
)
func getGOMAXPROCS() int {
return runtime.GOMAXPROCS(0)
}
func main() {
fmt.Printf("GOMAXPROCS is %d\n", getGOMAXPROCS())
}
Run Code Online (Sandbox Code Playgroud)
并像这样运行:
$ GOMAXPROCS= go run max.go
GOMAXPROCS is 1
Run Code Online (Sandbox Code Playgroud)
显示在这种情况下它是1,但我在这里寻找一些确认.
我正在玩Go以了解它的功能和语法.我已经完成了一个带有并发go函数的简单生产者 - 消费者程序,以及中间的优先缓冲区.单个生产者生成具有特定优先级的任务,并使用通道将它们发送到缓冲区.一组消费者在闲置,接收并消费它时会要求任务.中间缓冲区将一组任务存储在优先级队列缓冲区中,因此首先提供最高优先级的任务.该程序还会打印垃圾收集器活动(调用它的次数以及收集垃圾所花费的时间).
我正在使用Go 1.1在Raspberry Pi上运行它.
该软件似乎工作正常,但我发现在SO级别,htop显示有4个进程在运行,内存使用相同,CPU使用总和超过100%(Raspberry Pi只有一个核心,所以我假设它与线程/进程有关).系统负载大约是CPU的7%,我想因为OS级别的上下文切换是恒定的.GOMAXPROCS环境变量设置为1或取消设置.
你知道为什么Go使用多个操作系统进程吗?
代码可以在这里找到:http://pastebin.com/HJUq6sab
谢谢!
编辑:
它似乎htop
显示了系统的轻量级过程.Go程序运行其中几个轻量级进程(它们与goroutines线程不同),因此使用htop
显示多个进程,同时ps
或top
将只显示一个进程.