qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          并發時-修改Linux系統下的最大文件描述符限制

           通常我們通過終端連接到linux系統后執行ulimit -n 命令可以看到本次登錄的session其文件描述符的限制,如下:
            $ulimit -n
            1024
            當然可以通過ulimit -SHn 102400 命令來修改該限制,但這個變更只對當前的session有效,當斷開連接重新連接后更改就失效了。
            如果想永久變更需要修改/etc/security/limits.conf 文件,如下:
            vi /etc/security/limits.conf
            * hard nofile 102400
            * soft nofile 102400
            保存退出后重新登錄,其最大文件描述符已經被永久更改了。
            這只是修改用戶級的最大文件描述符限制,也就是說每一個用戶登錄后執行的程序占用文件描述符的總數不能超過這個限制。
            系統級的限制
            它是限制所有用戶打開文件描述符的總和,可以通過修改內核參數來更改該限制:
            sysctl -w fs.file-max=102400
            使用sysctl命令更改也是臨時的,如果想永久更改需要在/etc/sysctl.conf添加
            fs.file-max=102400
            保存退出后使用sysctl -p 命令使其生效。
            與file-max參數相對應的還有file-nr,這個參數是只讀的,可以查看當前文件描述符的使用情況。
            直接修改內核參數,無須重啟系統。
            sysctl -w fs.file-max 65536
            或者
            echo "65536" > /proc/sys/fs/file-max
            兩者作用是相同的,前者改內核參數,后者直接作用于內核參數在虛擬文件系統(procfs, psuedo file system)上對應的文件而已。
            可以用下面的命令查看新的限制
            sysctl -a | grep fs.file-max
            或者
            cat /proc/sys/fs/file-max
            修改內核參數
            /etc/sysctl.conf
            echo "fs.file-max=65536" >> /etc/sysctl.conf
            sysctl -p
            查看當前file handles使用情況:
            sysctl -a | grep fs.file-nr
            或者
            cat /proc/sys/fs/file-nr
            825 0 65536
            另外一個命令:
            lsof | wc -l
            下面是摘自kernel document中關于file-max和file-nr參數的說明
          file-max & file-nr:
          The kernel allocates file handles dynamically, but as yet it doesn't free them again.
          內核可以動態的分配文件句柄,但到目前為止是不會釋放它們的
          The value in file-max denotes the maximum number of file handles that the Linux kernel will allocate. When you get lots of error messages about running out of file handles, you might want to increase this limit.
          file-max的值是linux內核可以分配的最大文件句柄數。如果你看到了很多關于打開文件數已經達到了最大值的錯誤信息,你可以試著增加該值的限制
          Historically, the three values in file-nr denoted the number of allocated file handles, the number of allocated but unused file handles, and the maximum number of file handles. Linux 2.6 always reports 0 as the number of free file handles -- this is not an error, it just means that the number of allocated file handles exactly matches the number of used file handles.
          在kernel 2.6之前的版本中,file-nr 中的值由三部分組成,分別為:1.已經分配的文件句柄數,2.已經分配單沒有使用的文件句柄數,3.最大文件句柄數。但在kernel 2.6版本中第二項的值總為0,這并不是一個錯誤,它實際上意味著已經分配的文件句柄無一浪費的都已經被使用了

          posted on 2014-11-04 10:26 順其自然EVO 閱讀(475) 評論(0)  編輯  收藏 所屬分類: linux

          <2014年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 呼图壁县| 北票市| 丰台区| 图木舒克市| 新沂市| 邵武市| 吐鲁番市| 岳池县| 临西县| 富蕴县| 友谊县| 镇平县| 文成县| 大理市| 碌曲县| 呼伦贝尔市| 平果县| 揭东县| 宝兴县| 崇阳县| 凤山县| 梧州市| 连城县| 梨树县| 太谷县| 文安县| 呈贡县| 双柏县| 团风县| 南涧| 兴仁县| 卫辉市| 中西区| 石渠县| 延庆县| 广丰县| 泸水县| 苏州市| 潮安县| 沾益县| 外汇|