问题描述
根据维基百科,以下是一个非常优雅的 bash fork 炸弹:
According to Wikipedia, the following is a very elegant bash fork bomb:
:(){ :|:& };:
它是如何工作的?
推荐答案
分解,分为三大块:
:() # Defines a function, ":". It takes no arguments.
{ ... }; # The body of the function.
: # Invoke the function ":" that was just defined.
在body内部,函数被调用两次,管道后台运行;对进程的每次连续调用都会产生对:"的更多调用.这会迅速导致系统资源的爆炸性消耗,使事情陷入停顿.
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.
威廉在下面的评论是对我上面所说的更好的措辞,因此我进行了编辑以纳入该建议.
William's comment below was a better wording of what I said above, so I've edited to incorporate that suggestion.
这篇关于这个 bash fork 炸弹是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!