这个 bash fork 炸弹是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/991142/
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-09 18:16:30  来源:igfitidea点击:

How does this bash fork bomb work?

bashunix

提问by Lajos Nagy

According to Wikipedia, the following is a very elegant bash fork bomb:

根据维基百科,以下是一个非常优雅的 bash fork 炸弹:

:(){ :|:& };:

How does it work?

它是如何工作的?

回答by John Feminella

Breaking it down, there are three big pieces:

将其分解为三大块:

:()      # Defines a function, ":". It takes no arguments.
{ ... }; # The body of the function.
:        # Invoke the function ":" that was just defined.

Inside the body, the function is invoked twice and the pipeline is backgrounded; each successive invocation on the processes spawns even more calls to ":". This leads rapidly to an explosive consumption in system resources, grinding things to a halt.

在主体内部,函数被调用两次,管道被后台处理;对进程的每次连续调用都会产生更多对“:”的调用。这会迅速导致系统资源的爆炸性消耗,使事情陷入停顿。

Note that invoking it once, infinitely recursing, wouldn't be good enough, since that would just lead to a stack overflow on the original process, which is messy but can be dealt with.

请注意,调用它一次,无限递归,还不够好,因为这只会导致原始进程的堆栈溢出,这很混乱但可以处理。

A more human-friendly version looks like this:

更人性化的版本如下所示:

kablammo() {             # Declaration
  kablammo | kablammo&   # The problematic body.
}; kablammo              # End function definition; invoke function.

Edit:William's comment below was a better wording of what I said above, so I've edited to incorporate that suggestion.

编辑:威廉在下面的评论是对我上面所说的更好的措辞,因此我进行了编辑以纳入该建议。

回答by Talljoe

Short answer:

简短的回答:

The colon (":") becomes a function, so you are running the function piped to the function and putting it in the backgroun which means for every invocation of the function 2 copies of the function are invoked. Recursion takes hold.

冒号 (":") 成为一个函数,因此您正在运行通过管道传输到该函数的函数并将其放入后台,这意味着每次调用该函数都会调用该函数的 2 个副本。递归成立。