supman
V2EX  ›  问与答

fork()可以把 linux 系统资源占完么?

  •  
  •   supman · Aug 8, 2015 · 3413 views
    This topic created in 3981 days ago, the information mentioned may be changed or developed.
    大概在自己服务器上循环了10分钟, 但是没什么反应啊。。
    while(2==0)
    {
    fork();
    }
    14 replies    2015-08-09 15:31:22 +08:00
    echo1937
        1
    echo1937  
       Aug 8, 2015
    你试试fork炸弹就知道了。
    Strikeactor
        2
    Strikeactor  
       Aug 8, 2015
    while false什么鬼
    wy315700
        3
    wy315700  
       Aug 8, 2015
    while(false) 你跑10年都不会有效果
    rrfeng
        4
    rrfeng  
       Aug 8, 2015
    你敢贴上这个么
    :(){:|:&};:
    jasontse
        5
    jasontse  
       Aug 8, 2015 via Android
    会把 PID 号码吃完
    supman
        6
    supman  
    OP
       Aug 8, 2015
    @echo1937
    @Strikeactor
    @wy315700
    @rrfeng
    @jasontse


    错了错了。。。是while true。。。。 但是服务器还是好好的。。
    ihciah
        7
    ihciah  
       Aug 8, 2015
    上次出去玩,逛到一家苹果专卖店,扔了一个炸弹也是没反应,差评
    echo1937
        8
    echo1937  
       Aug 8, 2015
    @supman 把你的脚本写成递归调用再说。
    xierch
        9
    xierch  
       Aug 8, 2015
    最坑的是 fork() 失败后还要 kill()
    xl2tpd 里就有这个 bug,kill(-1, TERM),呵呵呵,不知道修好了没有
    mind3x
        10
    mind3x  
       Aug 9, 2015
    fork()是copy on write的好吗...你单纯跑fork,能占的资源很有限。

    Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.
    sleeperqp
        11
    sleeperqp  
       Aug 9, 2015
    fork会占完linux的进程管理资源 而不是整个内存的资源 (进程管理资源在内存中)
    因为linux管理进程是用一个结构体(task_struct)数组来管理进程的 这个数组是固定的 也就说系统所管理的进程数一定 因此不断fork是会耗尽这个结构体的
    pid是一个unsiged int数 是循环使用的 因此首先耗尽的应该是结构体数组 而不是pid
    上面说的copy on write是对的 当你不对新fork的进程进行写操作时 这些进程所管理的内存页实际是一样的 新增的进程只需要让内存页的计数加一 然后管理的页表项指向内存也即可 因此内存消耗较小
    tracyone
        12
    tracyone  
       Aug 9, 2015 via Android
    似乎有限制一个程序里面进程数的…
    q5we66fg
        13
    q5we66fg  
       Aug 9, 2015
    每个进程好像又现在最大内存,不知道会不会swap
    binyuJ
        14
    binyuJ  
       Aug 9, 2015
    我在.bashrc 里加了个diff() { diff -u "$@" }
    结果只要一打diff直接就不响应了,只能按电源
    看kenerl log 里面就是出了无限循环内存耗尽
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1428 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 6b34f497 · 82ms · UTC 16:59 · PVG 00:59 · LAX 09:59 · JFK 12:59
    ♥ Do have faith in what you're doing.