如何在Node.js中创建僵尸/已终止进程? [英] How do you create zombie/defunct process in nodejs?
问题描述
这里有很多帖子(例如 https://unix.stackexchange.com/questions/217507/zombies-in-bash ),展示了如何在bash或c中创建僵尸进程.我想知道是否有一种方法可以在nodejs中创建它们,所以当我执行ps ax | grep node
时,有一行命令段为node <defunct>
.
There are many posts here (e.g. https://unix.stackexchange.com/questions/217507/zombies-in-bash) that shows how to create zombie processes in bash or c. I would like to know if there is a way to create them in nodejs so when I do ps ax | grep node
, there's a line whose command section is node <defunct>
.
非常感谢您.
推荐答案
回答我自己的问题,以防对任何人有帮助.事实证明,在节点中很容易做到这一点.运行以下脚本后,可以执行ps ax | grep node
,并且应该看到带有[node] <defunct>
的条目.
Answering my own questions in case it helps anyone. Turns out to be pretty easy to do it in node. After you run the following script, you can do a ps ax | grep node
and you should see an entry with [node] <defunct>
.
var cp = require('child_process');
if(process.send){ //this is a child process
process.exit();
}
//main process, so spawn a child process
cp.fork(__filename);
//this keeps the main process busy and unable to respond to child's exit, making child defunct
while(true){};
这篇关于如何在Node.js中创建僵尸/已终止进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!