Go程序启动多个进程

huangapple go评论109阅读模式
英文:

Go program launching several processes

问题

我正在使用Go来了解它的特性和语法。我已经用并发的go函数做了一个简单的生产者-消费者程序,并在中间加入了一个优先级缓冲区。一个单独的生产者根据特定的优先级产生任务,并使用通道将它们发送到缓冲区。一组消费者在空闲时会请求一个任务,接收并消费它。中间缓冲区将在优先级队列缓冲区中存储一组任务,因此最高优先级的任务将首先被处理。该程序还会打印垃圾收集器的活动情况(它被调用了多少次以及收集垃圾所花费的时间)。

我正在使用Go 1.1在树莓派上运行它。

软件似乎工作正常,但我发现在SO级别上,htop显示有4个正在运行的进程,内存使用相同,并且CPU使用率的总和超过100%(树莓派只有一个核心,所以我想这可能与线程/进程有关)。此外,系统负载约为CPU的7%,我想这可能是因为操作系统层面的不断上下文切换。GOMAXPROCS环境变量设置为1或未设置。

你知道为什么Go会使用多个操作系统进程吗?

代码可以在这里找到:http://pastebin.com/HJUq6sab

谢谢!

编辑:

似乎htop显示的是系统的轻量级进程。Go程序运行多个这些轻量级进程(它们与goroutine线程不同),因此使用htop会显示多个进程,而pstop只会显示一个进程,这是应该的。

英文:

I'm playing with Go to understand its features and syntax. I've done a simple producer-consumer program with concurrent go functions, and a priority buffer in the middle. A single producer produces tasks with a certain priority and send them to a buffer using a channel. A set of consumers will ask for a task when idle, received it and consume it. The intermediate buffer will store a set of tasks in a priority queue buffer, so highest priority tasks are served first. The program also prints the Garbage collector activity (how many times it was invoked and how many time it took to collect the garbage).

I'm running it on a Raspberry Pi using Go 1.1.

The software seems to work fine but I found that at SO level, htop shows that there are 4 processes running, with the same memory use, and the sum of the CPU use is over 100% (the Raspberry Pi only has one core so I suppose it has something to do with threads/processes). Also the system load is about 7% of the CPU, I suppose because of a constant context switching at OS level. The GOMAXPROCS environment variable is set to 1 or unset.

Do you know why Go is using more than one OS process?

Code can be found here: http://pastebin.com/HJUq6sab

Thank you!

<b>EDIT:</b>

It seems that htop shows the lightweight processes of the system. Go programs run several of these lightweight processes (they are different from the goroutines threads) so using htop shows several processes, while psor top will show just one, as it should be.

答案1

得分: 5

请尝试终止所有可疑进程,然后再运行一次,只运行一次。此外,暂时不要使用go run,因为它会最小化运行进程的数量。

我怀疑其他实例只是你之前开发尝试的残留物(可能是通过go run调用的,并且没有正确地[间接地]在SIGINT上终止),特别是因为在'main'的末尾有一个1小时的“超时”(而不是适当的同步或select{})。Go二进制文件可以生成新的线程,但除非明确要求,否则不应该创建新的进程。而你的代码并没有导入“os/exec”或“syscall”。

如果我对go run和使用长时间超时的组合的猜测真的是罪魁祸首,那么RP内核可能与开发人员用于测试的内核存在一些差异。

英文:

Please try to kill all of the suspect processes and try again running it only once. Also, do not use go run, at least for now - it blurs the number of running processes at minimum.

I suspect the other instances are simply leftovers from your previous development attempts (probably invoked through go run and not properly [indirectly] killed on SIGINT [hypothesis only]), especially because there's a 1 hour "timeout" at the end of 'main' (instead of a proper synchronization or select{}). A Go binary can spawn new threads, but it should never create new processes unless explicitly asked for that. Which is not the case of your code - it doesn't even import "os/exec" or "syscall" in the first place.

If my guess about the combination of go run and using a long timeout is really the culprit, then there's possibly some difference in the RP kernel wrt what the dev guys are using for testing.

huangapple
  • 本文由 发表于 2013年5月26日 16:35:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/16757527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定