A Simple Fork Bomb in C++


The following is a simple C++ source code that can be compiled into a command line program that can bomb your OS until the OS becomes unresponsive or crashes.

1
2
3
4
5
6
7
#include <thread>
 
int main() {
        std::thread t(main);
        t.join();
        return 0;
}
#include <thread>
 
int main() {
        std::thread t(main);
        t.join();
        return 0;
}

Name the above source code “bomb.cpp” and Compile the above using the following command (requires linking with pthread library):

1
$./g++ -pthread -o bomb.o bomb.cpp
$./g++ -pthread -o bomb.o bomb.cpp

What is behind the scene is that the program will fork a thread of itself – which will recursively fork another and another.. The thread.join will not happen as it is not possible to wait unitl all (continously growing and infinite number of) threads to reach the same barrier.

Use it at your own risk – as it will force your server/PC to reboot when the OS becomes unresponsive.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
208 words
Last Post: Teaching Kids Programming - Using Heap (Priority Queue) to Generate Nth Ugly Number
Next Post: Teaching Kids Programming - Maximum Product by Splitting Integer using Dynamic Programming or Greedy Algorithm

The Permanent URL is: A Simple Fork Bomb in C++

Leave a Reply