r/ProgrammerHumor • • Aug 01 '22

>>>print(“Hello, World!”)

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

Show parent comments

2.5k

u/sanchez2673 Aug 01 '22 edited Aug 02 '22

It's called a fork bomb. It defines a function with the name : that takes no parameters () (not that you can pass parameters to a bash function like this but anyway). The body of the function {} contains a call to itself : and the output of itself is piped | into another call to itself :, both of which are started as a background process &. The ; terminates the statement and the final : calls the function, executing it. The function will keep multiplying exponentially until your PC cannot handle it anymore.

722

u/OSSlayer2153 Aug 01 '22 edited Aug 02 '22

So it would look like

:() {
    :|:&
};
:()

Its just made very confusing, you could write it like this:

fork() {
    fork|fork&
};
fork()

Correct?

Edit: reddit syntax is struggling

Edit2: fixed, you also dont need to call the function with empty params so it would be

fork() {
    fork|fork&
};
fork

254

u/sanchez2673 Aug 01 '22

thats right, but as i was also reminded, you dont need the parentheses at the final function call, simply the function name is sufficient

13

u/CopEatingDonut Aug 01 '22

How does a dead computer log its own death after its dead?

8

u/ElectronPie171 Aug 02 '22

Pretty much the same way humans do

7

u/[deleted] Aug 02 '22

: is a shell builtin in bash, so it might not be able to be overwritten. The parentheses might force it to use the function instead.

This is all conjecture bc i'm too lazy to open up my laptop to check

→ More replies (1)

10

u/TerrorBite Aug 01 '22

To fix the formatting, add four spaces indent to every line and only quote the first one, like

fork() {
    fork | fork &
};
fork()

Also, you are correct, except the final line; bash doesn't use parentheses to call functions (they take arguments like commands)

→ More replies (1)

4

u/auders_karaoke_bar69 Aug 02 '22

I eat ass, then regret eating ass, then regret my regret for eating ass, so I eat ass again.

→ More replies (10)

225

u/BlueBananaBaconBurp Aug 01 '22

What's the pipe operator doin in the body?

599

u/Xtrouble_yt Aug 01 '22

It’s a pipe bomb

123

u/[deleted] Aug 01 '22

[removed] — view removed comment

→ More replies (6)

10

u/hampshirebrony Aug 01 '22

I found the source of the mysterious ticking noise!

→ More replies (1)

8

u/[deleted] Aug 01 '22

I spat my goddamn coffee

5

u/Void1702 Aug 01 '22

Who added demoman to my code?

3

u/JoonasD6 Aug 01 '22

Yeaaaaay!

→ More replies (5)

13

u/spatofdoom Aug 01 '22

It's what causes it to grow so rapidly. This way the function body is to call itself piping its output to itself, so for every function call it calls 2 more of this function. Also, by using pipe, the OS will have to call the function ready to receive the output of the previous function causing parallel processing to take place.

→ More replies (5)

5

u/pdpi Aug 01 '22

The pipe makes it so that the : on the right consumes the output of the : on the left. It’s what makes the growth exponential by forcing both copies to be alive at the same time

→ More replies (6)

5

u/Lazypassword Aug 01 '22

I've seen this used to reboot a system

6

u/qcon99 Aug 01 '22

So OP was basically right

3

u/Spork_the_dork Aug 01 '22

If a stroke is an emotion, I guess.

3

u/daynthelife Aug 02 '22

When I first saw this, I was confused by how the pipe would ever resolve, since the first function call never returns. The answer that the & operator applies to the entire function (not just the part after the pipe) and essentially means “immediately return exit code 0, and run the function as a background process”. Thus, each pipe resolves in a single cycle.

Also, the reason this is likely to bring down a system (as well as why it is so hard to clean up) is not that it overloads the memory or CPU, but rather that due to the PID structure, the OS has a hard limit of 65536 processes that can be running at any given time. So, once this process table is filled, it becomes impossible to start a new process. Moreover, even if you can kill one instance, the PID slot will immediately be refilled by another copy.

If you have a working shell, you may be able to run kill -9 -1 to kill all processes owned by the user (as long as kill is a builtin for the running shell). If not, then a reboot is likely the only solution.

→ More replies (4)
→ More replies (32)