C vs BASH Fork bomb Last Updated : 07 Feb, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Pre-requisites : Fork System Call Fork bomb 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 : C // 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: C // 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 : 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. Comment More infoAdvertise with us Next Article C vs BASH Fork bomb K kartik Improve Article Tags : C Language system-programming Similar Reads Fork() Bomb Prerequisite : fork() in CFork Bomb is a program that harms a system by making it run out of memory. It forks processes infinitely to fill memory. The fork bomb is a form of denial-of-service (DoS) attack against a Linux based system.Once a successful fork bomb has been activated in a system it may 3 min read fork() in C The Fork system call is used for creating a new process in Linux, and Unix systems, which is called the child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction followin 6 min read fork() and Binary Tree Given a program on fork() system call. c #include <stdio.h> #include <unistd.h> int main() { fork(); fork() && fork() || fork(); fork(); printf("forked\n"); return 0; } How many processes will be spawned after executing the above program? A fork() system call spawn proc 4 min read Difference between fork() and exec() Every application(program) comes into execution through means of process, process is a running instance of a program. Processes are created through different system calls, most popular are fork() and exec() fork() pid_t pid = fork(); fork() creates a new process by duplicating the calling process, T 4 min read Output of C Programs | Set 2 Predict the output of below programs. Question 1 c #include<stdio.h> char *getString() { char str[] = "Will I be printed?"; return str; } int main() { printf("%s", getString()); getchar(); } Output: Some garbage value The above program doesn't work because array variables a 2 min read Like