C vs BASH Fork bomb in C/C++?

A fork bomb is a type of denial-of-service attack that creates an exponential number of processes, consuming system resources. While BASH fork bombs are more persistent because created processes detach from the parent, C fork bombs have their own characteristics and can be modified to increase their impact.

Syntax

#include <unistd.h>

int main() {
    while(1) {
        fork();
    }
    return 0;
}

BASH vs C Fork Bomb Differences

The key differences between BASH and C fork bombs are −

  • Process Independence: In BASH, created processes detach from the parent and continue running even if the parent is killed
  • Parent Dependency: In C, child processes automatically die when the parent process is destroyed
  • System Communication: BASH scripts communicate directly with the system, making them more persistent

Example: Basic C Fork Bomb

Warning: Do not run fork bomb programs on production systems as they can make the system unresponsive by exhausting process limits and memory.
#include <stdio.h>
#include <unistd.h>

int main() {
    printf("Fork bomb demonstration (limited iterations)\n");
    
    /* Limited version for demonstration */
    for(int i = 0; i < 3; i++) {
        pid_t pid = fork();
        if(pid == 0) {
            printf("Child process created: PID %d\n", getpid());
            break; /* Child exits loop */
        } else if(pid > 0) {
            printf("Parent created child: PID %d\n", pid);
        }
    }
    
    return 0;
}
Fork bomb demonstration (limited iterations)
Parent created child: PID 1234
Child process created: PID 1234
Parent created child: PID 1235
Child process created: PID 1235
Parent created child: PID 1236
Child process created: PID 1236

Example: Modified Fork Bomb with Memory Allocation

The fork bomb can be enhanced by allocating memory during process creation, which exhausts both process and memory resources −

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main() {
    printf("Modified fork bomb with memory allocation\n");
    
    /* Demonstration with limited iterations */
    for(int i = 0; i < 2; i++) {
        /* Allocate memory before forking */
        int *memory = (int*)malloc(sizeof(int) * 1000);
        
        if(memory != NULL) {
            printf("Allocated memory block %d\n", i + 1);
        }
        
        pid_t pid = fork();
        if(pid == 0) {
            printf("Child %d: Allocated additional memory\n", i + 1);
            /* Child allocates more memory */
            int *child_mem = (int*)malloc(sizeof(int) * 500);
            free(child_mem);
            break;
        }
        
        free(memory);
    }
    
    return 0;
}
Modified fork bomb with memory allocation
Allocated memory block 1
Child 1: Allocated additional memory
Allocated memory block 2
Child 2: Allocated additional memory

Key Differences Summary

Aspect BASH Fork Bomb C Fork Bomb
Process Persistence Processes survive parent death Child processes die with parent
Resource Impact Higher system impact Moderate system impact
Implementation Direct system communication System call wrapper

Conclusion

While BASH fork bombs are more persistent due to process detachment, C fork bombs can be modified to increase impact through memory allocation. Understanding these differences helps in system security and resource management.

Updated on: 2026-03-15T12:40:37+05:30

516 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements