Why don't C-forkbombs work like bash?

If I run the classic bash forkbomb:

:(){ :&:&};: 

my system freezes in a few seconds.

I tried writing forkbomb in C, here is the code:

 #include <unistd.h> int main( ) { while(1) { fork(); } return 0; } 

When I start it, the system becomes less responsive, but I can kill this process (even after a few minutes) just by pressing ^C




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

 :( ) { while true do : done } 

(I did not test it, I do not know if it hung the system).

Therefore, I also tried to implement the original version; here is 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. This is not my system hanging.




Why?

What is so special about bash forkbombs? Is it because bash uses a lot more memory / processor? Since bash processes handle far more system calls (for example, to access the file system) than mine?

+51
c ++ c linux bash fork
Nov 22 2018-10-10T00:
source share
4 answers

This C program is tiny, very tiny. In addition, fork () is such a program very, very effective. However, an interpreter such as Bash is much more expensive in terms of RAM usage and must constantly access the disk.

Try to run it much longer. :)

+44
Nov 22 '10 at 9:47
source share

In your forkbomb bash, you add new processes to new background process groups, so you cannot ^C .

+3
Nov 13 '11 at 11:58
source share

The real reason for this is because in BASH, the process you are creating is separate from the parent. If the parent process (the one you originally started) was killed, the rest of the processes continue. But in C implementations, you indicated that child processes die if the parent is killed, so enough to knock down the initial process, you started destroying the entire process tree that ever occurs.

I have not yet come up with an implementation of C forkbomb that separates child processes so that they are not killed if the parent dies. References to such implementations will be appreciated.

+3
Oct 11 '13 at 15:41
source share

This is mainly due to size. When you launch a bomb with a bash fork, it loads large monster programs (in relation to your c program) into memory, each of which starts to root for your processor resources. Of course, when big monsters begin to reproduce problems, this happens faster than if the bees begin to do the same. So the computer freezes immediately. However, if you continue to execute the executable c, it will also hang the system. It’s just that time will be much longer. If you want to compare the size of bash with the size c of the check / proc // status program. first with pid of any running instance of bash, and then with pid of any running instance of program c

0
Dec 6 '11 at 12:21
source share



All Articles