【问题标题】:How do I write an async child reaper in Go?如何在 Go 中编写异步子收割机?
【发布时间】:2014-06-11 01:18:43
【问题描述】:

我有一些这样的 go 代码:

cmd = exec.Command(command)
//...
cmd.Run()
func reapChild(cmd) {
    sigc := make(chan os.Signal, 1)
    signal.Notify(sigc, syscall.SIGCHLD)
    go func() {
        my_signal := <- sigc
        log.Infof("appstore: reapChildren: got a SIGCHLD signal")
        cmd.Wait()
        signal.Stop(sigc)
    }()
}

这会为特定生成的孩子收获过程,但我正在寻找 更通用的东西。

有没有办法让 PID 脱离 my_signal?我在找东西 像 pid_t wait(int *status) -- golang 提供了一个函数 称为 Wait4,它采用特定的 PID。

【问题讨论】:

  • 你的代码有什么问题? goroutines 非常便宜。
  • 什么是儿童收割者,有什么用途?

标签: go signals zombie-process


【解决方案1】:

如你所说,Go 的 syscall 包有这个功能:

func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error)

这似乎对你有用。来自the BSD man page for wait(2)(请记住,Go 部分是在 Mac 上开发的!):

wait4() 调用为程序提供了一个更通用的接口, 需要等待某些需要资源的子进程 子进程累积的利用率统计信息,或者 需要选项。 其他的等待功能是使用 wait4().

通过传递正确的参数,您可以使用Wait4 实现您想要的。例如,如果您不想等待特定的孩子:

如果 pid 为 -1,则调用等待任何子进程。

您可以在手册页中找到所需的其余信息。

【讨论】:

  • 请注意 wait4() 在 syscall 包中。使用部分 syscall 包的程序是不可移植的,因为 syscall 包在每个操作系统上都有不同的内容。
  • 感谢 fuzxxl 的回复,但是“系统调用包的一部分”是什么意思?
猜你喜欢
  • 2013-08-13
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多