make recyle zombie more simple

master
codeskyblue 7 years ago
parent 43f15bfdc3
commit be857aaa20

@ -3,44 +3,26 @@
package main package main
import ( import (
"log"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
) )
func init() { func init() {
go reapChildren() go watchChildSignal()
} }
func childSignal(notify chan bool) { func watchChildSignal() {
var sigs = make(chan os.Signal, 3) var sigs = make(chan os.Signal, 3)
signal.Notify(sigs, syscall.SIGCHLD) signal.Notify(sigs, syscall.SIGCHLD)
for { for {
<-sigs <-sigs
select { reapChildren()
case notify <- true:
default:
// Channel full, does not matter as we wait for all children.
}
} }
} }
func reapChildren() { func reapChildren() {
var wstatus syscall.WaitStatus var wstatus syscall.WaitStatus
notify := make(chan bool, 1) syscall.Wait4(-1, &wstatus, syscall.WNOHANG, nil)
go childSignal(notify)
for {
pid, err := syscall.Wait4(-1, &wstatus, 0, nil)
for err == syscall.EINTR {
pid, err = syscall.Wait4(-1, &wstatus, 0, nil)
}
if err == syscall.ECHILD {
break
}
log.Printf("pid %d, finished, wstatus: %+v", pid, wstatus)
}
} }

Loading…
Cancel
Save