Writing a simple, intelligible fork bomb in bash?

2019-08-15 02:50发布

问题:

How can I do this?

I just wish to write something like

while(true) {
fork()
}

Is this possible in bash ? I don't want it for religious reasons, just to explain to a friend!

回答1:

You cannot do it by bash alone since there isn't a fork() system call directly accessible in bash (at least as far as I can tell). You could do a 1 liner in Perl though.

perl -e 'while(1) { fork(); sleep(1); }'

This should spawn 2xnumber of processes per second. I.E. first spawn will give you 2, next run gives you 4, next run gives you 8 etc...



回答2:

Try this:

#!/bin/bash
$0 &
$0 &
wait

Optionally insert echo $$ before the first $0



标签: bash fork