Linux对打开文件数量的限制

版权声明:欢迎分享转载 我可能会失败,但我不会一直失败 https://blog.csdn.net/u012637358/article/details/89639860

在高并发量多线程开发运维的时候我们常常会遇到类似“Socket/File: Can’t open so many files”,“无法打开更多进程”,或是coredump过大等问题,这些都可以设置资源限制来解决

句柄数限制类别:

  • 系统总限制
  • 单进程限制。

使用命令 ulimit -n 可以看到系统对于单个进程的限制,即open files。执行命令 ulimit -a 如下:

[root@node6 ~]# cat /proc/sys/fs/nr_open
1048576
[root@node6 ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 516123
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1048000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 2048000
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

open files 1048000表示我当前登录的用户(root),每个进程可以打开1048000个句柄,当然总和不能超过 file-max 限制。

修改open files的值两种方法

  • 临时的
    ulimit -HSn 65535 将open-files 修改为65535 ,退出当前shell后即失效。H和S选项表示硬限制和软限制,下面有解释,省略的话表示同时修改。
  • 永久的
    若希望永久生效的话就得修改配置文件,/etc/security/limits.conf,修改后需要重启系统,该文件内容为
[root@node6 ~]# cat /etc/security/limits.conf
# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open file descriptors  最大打开文件数
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes  最大进程数
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4

# End of file
#<domain>      <type>  <item>         <value>
* soft nofile 1048000  
* hard nofile 1048000  
* soft nproc 1048000  
* hard nproc 1048000

第一列:
表示对哪些用户进程进行限制,* 表示所有用户。
第二列:
   soft: 表示软限制,当进程打开的句柄数超过该限制后,只是进行警告。
   hard:表示硬限制,进程最多打开这么多句柄。
第三列:
nofile 表示能够打开的最大文件数
第四列:
代表具体的值,这个值也是有上限的,这个上限的值设置在 /proc/sys/fs/nr_open ,默认值为 1048576,完全够用了。


※注意※:当你实际给nofile设置成这个值unlimited或大于1048576值时,等你重启就会发现无法登录系统了。必须设置小于1048576它的数值才行


单个线程最多能分配的文件句柄数

nr_open 表示一个线程最多能分配的文件句柄数

[root@node2 sbin]# cat /proc/sys/fs/nr_open
1048576

系统总打开文件句柄限制

上面的 open files 是对单个进程的限制,属于线程级别的。
系统级的限制在这个文件中 /proc/sys/fs/file-max

[root@node2 sbin]# cat /proc/sys/fs/file-max
6554246

file-max指定了系统范围内所有进程可以打开的文件句柄限制。

同样,修改上面那个文件也是临时生效的,重启后会失效。
如果要永久生效,则要修改这个文件, /etc/sysctl.conf

fs.file-max  = 6815744

如果没有这一项,则新加这一项就行。运行 sysctl -p 或重启后才能生效。
lsof -p 进程pid 查看单个进程打开的文件句柄
/proc/sys/fs/file-nr记录当前系统打开的句柄数

[root@node2 sbin]# cat /proc/sys/fs/file-nr
6208	0	6554246

第一列表示已打开的句柄数
第二列表示已分配但是未使用的句柄数
第三列表示系统总的句柄数,即 file-max

总结

ulimit其实就是对单一程序的限制,进程级别的

file-max是所有时程最大的文件数

nr_open是单个进程可分配的最大文件数

猜你喜欢

转载自blog.csdn.net/u012637358/article/details/89639860
今日推荐