Why C-forkbombs don't work like bash ones?

C++CLinuxBashFork

C++ Problem Overview


If I run the classical bash forkbomb:

:(){ :&:&};:

my system hangs after a few seconds.

I tried to write a forkbomb in C, here is the code:

#include <unistd.h>

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

When I run it the system gets less responsive, but I can kill that process (even after minutes) just pressing ^C.


The above code is different from the original bash forkbomb I posted: it's something more like:

:( )
{
	while true
	do
		:
	done
}

(I didn't test it; don't know if it'd hang the system).

So I also tried to implement the original version; here the code:

#include <unistd.h>

inline void colon( const char *path )
{
	pid_t pid = fork( );
	if( pid == 0 ) {
		execl( path, path, 0 );
	}
}

int main( int argc, char **argv )
{
	colon( argv[0] );
	colon( argv[0] );
	return 0;
}

But still nothing: I can run it and then easily kill it. It's not hanging my system.


Why?

What's so special about bash forkbombs? Is it because bash uses a lot more memory/CPU? Because bash processes call a lot more system calls (eg, to access filesystem) than mine?

C++ Solutions


Solution 1 - C++

That C program is tiny, seriously tiny. In addition, fork()'ing a program like that is very, very efficient. An interpreter, such as Bash, however, is much more expensive in terms of RAM usage, and needs to access the disk all the time.

Try running it for much longer. :)

Solution 2 - C++

The real cause for this is that in BASH the process you create is detached from the parent. If the parent process (the one you initially started) is killed, the rest of the processes live on. But in the C implementations you listed the child processes die if the parent is killed, so it's enough to bring down the initial process you started to bring down the whole tree of ever-forking processes.

I have not yet come up with a C forkbomb implementation that detaches child processes so that they're not killed if the parent dies. Links to such implementations would be appreciated.

Solution 3 - C++

In your bash forkbomb, you are putting new processes in new background process groups, so you are not going to be able to ^C them.

Solution 4 - C++

Thats basically because of the size. When you are running the bash fork bomb it loads big monster programs into memory (with respect to your c program) each of them start hogging on your cpu resources.Definitely when big monsters start reproducing trouble comes more quickly than if bees start doing the same. So the computer hangs immediately.However if you keep your c executable running for long it will hang the system too.Just that the time will be much much more. If you want to compare the size of bash with size of c program check out /proc//status. first with pid of any running instance of bash,and then with pid of any running instance of a c program

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpeoroView Question on Stackoverflow
Solution 1 - C++ArafangionView Answer on Stackoverflow
Solution 2 - C++ShnatselView Answer on Stackoverflow
Solution 3 - C++ninjaljView Answer on Stackoverflow
Solution 4 - C++bashrcView Answer on Stackoverflow