2

For the classical bash fork bomb:

:(){ :|:& };:

I modify it as following:

:(){ :& };:

Execute and it exits immediately. Per my understanding, : function should recursively create child process in background. Why doesn't it work?

Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
  • This might help: [How does this bash fork bomb work?](https://stackoverflow.com/q/991142/3776858) – Cyrus Dec 10 '17 at 11:18
  • *sarnold*'s answer to [How does this other version of the bash fork bomb work?](https://stackoverflow.com/a/8394535/6136214) is also good, it has some pseudo process tree pictures of both classes of forkbomb that may help explain what the difference is between having the pipe and not having it. – agc Dec 11 '17 at 01:24

1 Answers1

0

Because there's always only one recursion path of the function :()

The original fork bomb invokes two more instances simultaneously every invocation, using up system resources quickly. However your modified version only invokes itself once, making a simple recursion. It'll probably end up overflowing the stack and doing no more harm.

iBug
  • 35,554
  • 7
  • 89
  • 134
  • `:() { :; }` would overflow the call stack, not the one that forks a background processes. – chepner Dec 10 '17 at 14:31
  • Your first sentence ("only one running instance of `:`") contradicts your last. – chepner Dec 10 '17 at 15:16
  • @chepner It halts after invoking another instance. Can you suggest a better wording? – iBug Dec 10 '17 at 15:24
  • Just drop the last sentence. – chepner Dec 10 '17 at 15:35
  • @chepner Drop "*It'll probably end up overflowing the stack and doing no more harm*"? – iBug Dec 10 '17 at 15:38
  • @chepner, I'm trying to reproduce the idea that a backgrounded function call from within a function consumes no stack frame. My current test case is `count=0; recurTest() { if (( ++count < 10 )); then echo "$count ${#FUNCNAME[@]}"; recurTest & fi; }; recurTest`, but as you can see, the depth of the `FUNCNAME` array increases with each recursion level. Can you tell me where I'm going wrong? – Charles Duffy Dec 10 '17 at 20:50