How do you create zombie/defunct process in nodejs

2019-09-13 22:24发布

问题:

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>.

Thank you very much.

回答1:

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){};