r/ProgrammerHumor • • Aug 01 '22

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

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

Show parent comments

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!

3

u/GetJukedM8 Aug 02 '22

Yayyyyyy!

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!

2

u/boumans15 Aug 02 '22

Yaaaaaaaaaay

2

u/Fine-Impress8483 Aug 02 '22

Hooray!! It’s a pipe bomb

2

u/Triffinator Aug 02 '22

Thanks for triggering my Potter Puppet Pals flashbacks.

1

u/revyn Aug 01 '22

"I got a pipe bomb over here!"

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.

3

u/Kietzyboi Aug 01 '22

I’m not very familiar with bash - why does the expression on the right of the pipe operator even get evaluated? Wouldn’t the function just recurse into the first expression indefinitely? I’m assuming the expressions in the body get grouped as :|(:&) rather than (:|:)&

6

u/PrincessRTFM Aug 02 '22 edited Apr 10 '23

The pipe operator in shells is an output redirection of sorts. It creates a "pipe" that takes the output of the process on the left side and feeds it through to the input of the one on the right. Necessarily, in order for this to work, both processes must be started, which happens concurrently (or close enough to generally not matter).

What I think is tripping you up is the syntax grouping and the execution order. In bash, appending an & to a command line means the entire line is executed in the background, where it keeps running while the shell continues onwards as well.

As a result, the shell spawns a background job A, which executes : | :. Each of those two copies of : then spawns a background job (B and C), which execute : | : as well. B then spawns D and E, while C spawns F and G, and it continues on like that.

[EDIT] The fact that : produces no output and takes no input is irrelevant, in case anyone's wondering. The computer doesn't know that. It can't know that in all cases - or even a majority of them - and while it would probably be possible for a general analysis mechanism to determine that for this function, no such mechanism is or probably will be implemented given how few cases it could actually apply to. When piping output, the pipe remains open until it's no longer needed - as in, no longer functional. If the source (left side) terminates before the sink (right side), then the sink's input stream is closed. The processes involved are not terminated by the shell as a result of the pipe closing, because it can't be known what the process is doing or still needs to do.

→ More replies (3)

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

1

u/KingCoolFrauenarzt69 Aug 01 '22

Call the plumber

1

u/die-maus Aug 01 '22

Hint: the name of the function is :. See if that makes it easier to parse. 👍

→ More replies (3)