问题描述
我在Go中有一个应用程序,用于重新路由二进制文件的STDIN和STDOUT,然后运行它们。简而言之,我在做:
$ b
- 用二进制路径创建命令对象(让我们调用对象命令A)
- 使用二进制路径创建命令对象(调用它命令B)
- 将命令B的标准输出设置为命令的标准输入A
- 启动命令A
- 启动命令B
我注意到,当命令A正在运行时,命令B的进程退出,它将在进程表中成为僵尸进程。
下面是一个例子:
commandA:= exec.Command(samplebin )
commandB:= exec.Command(sample2bin)
cmdAStdin:= commandA.StdinPipe()
commandB.Stdout = cmdAStdin
commandA.Start()
commandB.Start()
为什么commandB变成僵尸如果在commandA仍在运行时退出?我在Ubuntu 14上运行Go 1.5。
当进程退出时,它始终 成为一个僵尸,无论其他进程在运行。这只是过程终止的方式。该过程将保持僵尸状态,直到它的父母调用 wait
来获得其退出状态,或者通过忽略SIGCHLD来指示它对孩子不感兴趣(可能在孩子退出之前)。在这种情况下它将保持僵尸状态,以免退出状态丢失。在您的示例中,您的进程(创建进程的进程)似乎是父母,所以A和B都会保持僵尸状态,直到你的程序收集它们。
如果一个进程在仍然有孩子(跑步或僵尸)时退出,那些孩子将被重新设置到退出程序的父母身上,该父母通常会忽略退出状态(清理僵尸)。
I have an application in Go that reroutes the STDIN and STDOUT of binaries and then runs them. In a nutshell I'm doing:
- 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
I noticed whenever the process for command B exits while command A is running, it becomes a zombie process in the process table.
Here's an example:
commandA := exec.Command("samplebin")
commandB := exec.Command("sample2bin")
cmdAStdin := commandA.StdinPipe()
commandB.Stdout = cmdAStdin
commandA.Start()
commandB.Start()
Why does commandB become a Zombie if it exits while commandA is still running? I'm running Go 1.5 on Ubuntu 14.
When a process exits, it ALWAYS becomes a zombie, regardless of what other processes are running. That's just the way process termination works. The process will remain a zombie until its parent calls wait
to get its exit status, or indicates that it is uninterested in children by ignoring SIGCHLD (which may have been before the child exited). It will remain a zombie until that happens, lest the exit status get lost.
In your example, it would seem that your process (the one creating the processes) is the parent, so both A and B will remain as zombies until your process collects them.
If a process exits while it still has children (either running or zombies), those children will be reparented to the exiting process's parent, which will generally ignore the exit status (clearing up the zombies).
这篇关于Golang:儿童进程成为僵尸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!