我在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()

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

最佳答案

当某个进程退出时,无论正在运行什么其他进程,它始终会变成僵尸。这就是进程终止的方式。该进程将一直处于僵尸状态,直到其父级调用wait以获得退出状态,或者通过忽略SIGCHLD(可能在子级退出之前就对它表示不感兴趣)表明该进程对子级不感兴趣。在发生这种情况之前,它将一直是僵尸,以免退出状态迷路。

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

如果某个进程在仍然有子进程(正在运行或僵尸)的情况下退出,则这些子进程将重新成为退出进程的父进程的父进程,而该进程的父进程通常会忽略退出状态(清理僵尸进程)。

关于Golang : Child Processes become Zombies,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36050503/

10-16 16:07