From 43f15bfdc316458949e70f3d6084c6bc01b0c9a0 Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Tue, 11 Jul 2017 17:15:52 +0800 Subject: [PATCH] catch SIGCHLD signals --- sigchld_unix.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/sigchld_unix.go b/sigchld_unix.go index 24ef95e..e577e5b 100644 --- a/sigchld_unix.go +++ b/sigchld_unix.go @@ -3,17 +3,35 @@ package main import ( + "log" + "os" + "os/signal" "syscall" - - "github.com/qiniu/log" ) func init() { go reapChildren() } +func childSignal(notify chan bool) { + var sigs = make(chan os.Signal, 3) + signal.Notify(sigs, syscall.SIGCHLD) + + for { + <-sigs + select { + case notify <- true: + default: + // Channel full, does not matter as we wait for all children. + } + } +} + func reapChildren() { var wstatus syscall.WaitStatus + notify := make(chan bool, 1) + + go childSignal(notify) for { pid, err := syscall.Wait4(-1, &wstatus, 0, nil)