Golang:Child Processes成为Zombies

Anf*_*nee 8 stdin stdout process go zombie-process

我在Go中有一个应用程序重新路由二进制文件的STDIN和STDOUT,然后运行它们.简而言之,我正在做:

- create command object with the binary path (lets call the object command A) - create command object with the binary path (calling it command B) - set the stdout of command B to the stdin of Command A - start command A - start command B

我注意到,当命令A运行时,只要命令B的进程退出,它就会变成进程表中的僵尸进程.

这是一个例子:

commandA := exec.Command("samplebin")
commandB := exec.Command("sample2bin")

cmdAStdin := commandA.StdinPipe()

commandB.Stdout = cmdAStdin

commandA.Start()
commandB.Start()
Run Code Online (Sandbox Code Playgroud)

如果commandB仍在运行时,为什么commandB会退出僵尸?我在Ubuntu 14上运行Go 1.5.

Chr*_*odd 16

当一个进程退出时,它总是变成一个僵尸,无论其他进程是在运行什么.这就是流程终止的工作方式.该过程将保持僵尸,直到其父母要求wait获得其退出状态,或通过忽略SIGCHLD(可能在孩子退出之前)表明它对孩子不感兴趣.它将保持僵尸,直到发生这种情况,以免退出状态丢失.

在您的示例中,您的进程(创建进程的进程)似乎是父进程,因此A和B都将保留为僵尸,直到您的进程收集它们.

如果一个进程在仍有子进程(运行或僵尸)时退出,那么这些子进程将被重新分配给退出进程的父进程,这通常会忽略退出状态(清除僵尸).