What? Why did my shell just crash? How did I accomplish that? I am astonished that this is even possible!

It seems that I have executed a fork bomb. “A what now?”, you might ask now. Computer scientists love a great metaphor, in this case, I really did something explosive to my PC.

Fork Bombs

What looks like gibberish is actually one of the simplest functions of your operating system, but abused to stop your OS from working at all. :(){ :|:& };: is nothing more than a declaration of a function, followed by its invocation. The function’s name is :. Let’s rename it crash, so we can make sense of this:

crash(){
  crash | crash&
}
crash

The crash function really just invokes itself, pipes (|) its own output into another invocation of itself that is then sent to work in the background with the &. After this declaration, this is function is called once.

You might have already realized, that this function is calls itself twice, and each of these calls will result in two more calls and so on:

0 1 2 3 : : : : 8 4 9 2 1 0 5 1 1 1 1 2 6 1 3 3 1 4 7 1 5

Exponential growth

Every generation of the fork bomb has double the siblings of the previous generation.

Our operating system has a maximum amount of processes. On Linux you can use the following command to get your maximum process ID:

# sysctl kernel.pid_max
kernel.pid_max = 4194304

That’s exactly 2^22 processes allowed (a gigantic amount!). But since our fork bomb uses the power of exponential growth, this also means that after just 22 generations, we are reaching the limit of all process ids available on our system!

Learnings

Don’t do fork bombs, kids! They look like fun, but your PC probably will not appreciate your experiment and you might have to force a reboot (although I would never know from experience).