forbomb

来源:互联网 发布:重庆时时彩源码 编辑:程序博客网 时间:2024/06/05 01:10
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <unistd.h>int main(){    pid_t pid = 0;    //进程ID    printf("Start to begin fork.\n");    for (;;)    {        pid = fork();        if (pid < 0)        {            printf("Failed to fork child process.\n");        }        else if (pid = 0)        {            printf("Succeed to fork child[%d] process.\n", getpid());        }       }    return 0;}

代码如上,这个函数一开始会去fork一个子进程,子进程如果fork成功会打印一句话,然后。。。。。。
然后子进程也会重新从for函数重新循环,继续去fork,这样子子孙孙无穷尽了,就会造成资源被消耗尽了。
可以在shell中将编译出来的进程试运行一下./进程名,就可以看见运行的状态的,这个时候如果想退出,可以直接ctrl+c来退出,但是如果是./进程名&,这个时候进程在后台运行,你就只能重启了。

百度百科里有fork炸弹的说明,有一个最简单的;(){ :|:& };:的fork炸弹,挺有意思的,有兴趣的可以了解一下。

0 0
原创粉丝点击