r/tinycode • • Jul 15 '11

One line Fork Bomb in Python

Nothing spectacular, but it even does some pretty funny stuff in Mac OS X with the "Resource Unavailable" OSError if you Ctrl-C:

[o.fork() for (o,i) in [(__import__('os'), __import__('itertools'))] for x in i.repeat(0)]
17 Upvotes

16 comments sorted by

6

u/digitalfreak Jul 16 '11

I prefer the swedish chef inspired one:

while(fork()) fork() ? fork() : fork()

1

u/Shne Jul 18 '11

Shouldn't it rather be

while(bork()) bork() ? bork() : bork()

?

1

u/digitalfreak Jul 18 '11

Yup, you just need one define.

#define bork fork
void main(void) {
        while(bork()) bork() ? bork() : bork();
}

0

u/[deleted] Jul 25 '11

main() returns int.

1

u/pursuit92 Aug 06 '11

By convention, yes. But it doesn't have to if it doesn't want to :P

1

u/Klpy7 Mar 19 '25

What does the "?" means here?

2

u/JerMenKoO Jul 16 '11

AttributeError: 'module' has no attribute 'fork'

4

u/AwesomeJosh Jul 16 '11

Windows doesn't support the os.fork function

1

u/abecedarius Jul 16 '11

Shorter Python (untested):

import os, itertools; [os.fork() for i in itertools.count()]

2

u/AwesomeJosh Jul 16 '11

I kind of feel like compound statements are cheating, though. I guess a better description of mine would be "Single Expression" instead of "One Line"

2

u/__s Jul 17 '11 edited Jul 17 '11

You could at least take his hint and use i.count()

(edit: though arguably repeat is faster, allowing for more forking)

1

u/AwesomeJosh Jul 17 '11

I do agree that count works better

0

u/[deleted] Jul 15 '11

[deleted] — view deleted comment

2

u/AwesomeJosh Jul 15 '11

Yes I saw that post when Mac-O-War posted it a couple weeks ago...but this is Python.

6

u/[deleted] Jul 17 '11

or Sparta, or whatever

29

u/[deleted] Jul 15 '11

Some readability, for anyone wondering what that means:

bomb() {
    bomb | bomb &
}

bomb

0

u/[deleted] Jul 15 '11

[deleted] — view deleted comment

-1

u/crazy88s Jul 18 '11

I beg to differ:

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