Last active
August 29, 2015 14:02
-
-
Save burke/1c105378ac0629b39485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func startReapingChildren() { | |
sigchld := make(chan os.Signal, 1) | |
signal.Notify(sigchld, syscall.SIGCHLD) | |
log.Println("starting to reap children") | |
go func() { | |
for _ = range sigchld { | |
reapChildren() | |
} | |
}() | |
} | |
func reapChildren() { | |
var stat syscall.WaitStatus | |
var rusage syscall.Rusage | |
for { | |
// {_,ECHILD} means that there are no children. | |
// {0,nil} means that there are children, but none have changed state (died) | |
// {_,any other error} means something kinda serious happened. | |
pid, err := syscall.Wait4(0, &stat, syscall.WNOHANG, &rusage) | |
if err != nil { | |
if err == syscall.ECHILD { | |
break | |
} | |
reportErrorAndDie("Unexpected error reaping children: ", err) // EINVAL | |
} | |
if pid == 0 { | |
break | |
} | |
if pid == AppPid { | |
fmt.Printf("App rusage: %#v\n", rusage) | |
log.Println("App died") | |
AppDead <- stat.ExitStatus() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment