Batch fork bomb?

mowwwalker picture mowwwalker · Jan 21, 2012 · Viewed 29.1k times · Source

I was looking at the fork bomb on Wikipedia, and the batch examples were:

%0|%0

OR

:here
start ''your fork bomb name''.bat
goto here

OR

:here
start %0
goto here

I understand the second two, they start another instance of themselves and then repeat, but I don't understand the first. I read that the pipeline executes the file to the right with the output of the file to the left. Why can't the fork bomb just be:

%0

I would assume that this would call itself, but then terminate instantly, but why wouldn't %0|%0 also terminate? Even though the new instance is going to keep making new instances, isn't the first done after starting the second? What makes the first one continue?

edit: Does it loop because none of the instances can terminate until their recursive call is returned? Does this mean it only ever calls the first %0?

Answer

dbenham picture dbenham · Jan 21, 2012

%0 will never end, but it never creates more than one process because it instantly transfers control to the 2nd batch script (which happens to be itself).

But a Windows pipe creates a new process for each side of the pipe, in addition to the parent process. The parent process can't finish until each side of the pipe terminates. So the main program with a simple pipe will have 3 processes. You can see how the bomb quickly get's out of control if each side of the pipe recursively calls the parent batch!