C vs BASH Fork bomb

Pre-requisites :

Bash fork bomb :

:(){:&:&};:

Working in Unix :
In Unix-like operating systems, fork bombs are generally written to use the fork system call. As forked processes are also copies of the first program, once they resume execution from the next address at the frame pointer, they also seek to create a copy of themselves. This has the effect of causing an exponential growth in processes.

C program for fork bomb :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Modified fork bomb
#include <unistd.h>
#include <malloc.h>
  
int main()
{
    //Infinite loop
    while (1)
    {
        // Generating child fork processes
        fork();
    }
}

chevron_right


Working in Windows :
Microsoft Windows operating systems do not have an equivalent functionality to the Unix fork system call. A fork bomb on such an operating system must therefore create a new process instead of forking from an existing one.

Which is more powerful between Bash and C fork() bomb

This is clear that the BASH fork bomb is much more powerful than its version of C program. The reason is that in BASH the process we create is detached from the parent. If the parent process (the one we initially started) is killed, the rest of the processes live on. But in the C implementation, the listed child processes die if the parent is killed, so it’s enough to bring down the initial process we started to bring down the whole tree of ever-forking processes. A script communicates with the system directly.

The fork bomb program in C can be modified. We can allocate memory in the program at the time of creating the fork processes.
Below is the implementation of modified C fork bomb:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Modified fork bomb
#include <unistd.h>
#include <malloc.h>
  
int main()
{
    // Infinite loop
    while (1)
    {
        // Generating child fork processes
        fork();
  
        // Allocating memory in RAM
        int *p = (int *) malloc (sizeof (int) * 100000);
    }
}

chevron_right


Different fork bombs in Windows :

  • Terminal script :

    %0|%0

    Save it as a bat extension(Example fork.bat).
    The process of every symbol is somehow similar to the BASH script. The given line will call the same file again and pipe the output to another instance of the same batch file. It is not deadly enough to crash the system. But it will surely make the CPU unresponsive enough to leave reboot as only option.

  • Batch file :

    :runthis
    start %0
    goto runthis
    

    Save it as a bat extension(Example fork.bat).
    Working :

    :runthis is a label which determines a point 
    where the execution can be sent using the goto command.
    
    start %0 asks the command prompt to launch 
    another instance of the same batch file into another process.
    
    goto runthis command tells the execution to 
    go to the runthis label which will then call the start %0 command.
    

    It will go into a self recursive call and end up creating a lot of processes.
    The system will hang up due to resource over-usage! A lot of command prompt windows will open up and the entire system will come to a halt within few seconds.

This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.