为什么 C-forkbombs 不像 bash 那样工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4244016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:57:32  来源:igfitidea点击:

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

c++clinuxbashfork

提问by peoro

If I run the classical bash forkbomb:

如果我运行经典的 bash forkbomb:

:(){ :&:&};:

my system hangsafter a few seconds.

我的系统在几秒钟后挂起

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

我试图用 C 写一个 forkbomb,这是代码:

#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.

当我运行它时,系统的响应会变慢,但我只需按 就可以终止该进程(即使在几分钟后)^C



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

上面的代码与我发布的原始 bash forkbomb 不同:它更像是:

:( )
{
    while true
    do
        :
    done
}

(I didn't test it; don't know if it'd hangthe 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 hangingmy 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?

bash forkbombs 有什么特别之处?是不是因为 bash 使用了更多的内存/CPU?因为 bash 进程调用的系统调用(例如访问文件系统)比我的多得多?

采纳答案by Arafangion

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.

那个 C 程序很小,非常小。此外,fork() 编写这样的程序非常非常有效。但是,解释器(例如 Bash)在 RAM 使用方面的成本要高得多,并且需要一直访问磁盘。

Try running it for much longer. :)

尝试运行更长时间。:)

回答by Shnatsel

The realcause 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 wholetree of ever-forking processes.

真正为这个原因是在BASH创建过程是从母体分离。如果父进程(您最初启动的那个)被杀死,其余的进程会继续存在。但是在你列出的 C 实现中,如果父进程被杀死,子进程就会死亡,所以它足以降低你开始降低整个不断分叉进程树的初始进程。

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.

我还没有想出一个 C forkbomb 实现来分离子进程,这样如果父进程死亡,它们就不会被杀死。链接到此类实现将不胜感激。

回答by ninjalj

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

在您的 bash forkbomb 中,您将新进程放入新的后台进程组中,因此您将无法使用^C它们。

回答by bashrc

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

那基本上是因为大小。当您运行 bash fork 炸弹时,它会将大型怪物程序加载到内存中(相对于您的 c 程序),它们中的每一个都开始占用您的 CPU 资源。当然,当大型怪物开始重现问题时,比蜜蜂开始做同样的事情来得更快. 所以计算机立即挂起。但是,如果您让 c 可执行文件长时间运行,它也会使系统挂起。只是时间会多得多。如果你想比较 bash 的大小和 c 程序的大小,请查看 /proc//status。首先是 bash 任何正在运行的实例的 pid,然后是 ac 程序的任何正在运行的实例的 pid