Linux中最大进程数和最大文件数

前言

Linux系统中可以设置关于资源的使用限制,比如:进程数量,文件句柄数,连接数等等。
在日常的工作中应该遇到过:

-bash: fork: retry: Resource temporarily unavailable

或者

too many open files

这些类似的操作错误,前者是由于当前用户的进程数超出限制,后者由于当前用户的文件打开数超出限制。

下面就简单的介绍下配置用户进程数和文件打开数。

一.ulimit命令

Provides control over the resources available to the shell and to processes started by it, on systems that allow such control.

Linux的man文档中是这样描述ulimit的:提供了当前shell控制系统资源的能力。从这里可以分析出ulimit的特点:

  • 可控系统资源:cpu、memory、process、file等等
  • 控制的 作用域在当前shell会话中

ulimit的语法:

ulimit [-HSTabcdefilmnpqrstuvx [limit]]

分为参数和值两部分,参数用于表示特定的资源类型,值表示在对特定的资源进行限制的量度。

命令参数 描述 例子
-H 设置硬资源限制,一旦设置不能增加 ulimit –Hs 64;限制硬资源,线程栈大小为 64K
-S 设置软资源限制,设置后可以增加,但是不能超过硬资源设置 ulimit –Sn 32;限制软资源,32 个文件描述符
-a 显示当前所有的 limit 信息 ulimit – a;显示当前所有的 limit 信息
-c 最大的 core 文件的大小, 以 blocks 为单位 ulimit –c unlimited; 对生成的 core 文件的大小不进行限制
-d 进程最大的数据段的大小,以 Kbytes 为单位 ulimit -d unlimited;对进程的数据段大小不进行限制
-f 进程可以创建文件的最大值,以 blocks 为单位 ulimit –f 2048;限制进程可以创建的最大文件大小为 2048 blocks
-l 最大可加锁内存大小,以 Kbytes 为单位 ulimit –l 32;限制最大可加锁内存大小为 32 Kbytes
-m 最大内存大小,以 Kbytes 为单位 ulimit – m unlimited;对最大内存不进行限制
-n 可以打开最大文件描述符的数量 ulimit –n 128;限制最大可以使用 128 个文件描述符
-p 管道缓冲区的大小,以 Kbytes 为单位 ulimit –p 512;限制管道缓冲区的大小为 512 Kbytes
-s 线程栈大小,以 Kbytes 为单位 ulimit – s 512;限制线程栈的大小为 512 Kbytes
-t 最大的 CPU 占用时间,以秒为单位 ulimit – t unlimited;对最大的 CPU 占用时间不进行限制
-u 用户最大可用的进程数 ulimit – u 64 限制用户最多可以使用 64 个进程
-v 进程最大可用的虚拟内存,以 Kbytes 为单位 ulimit – v 200000;限制最大可用的虚拟内存为 200000 Kbytes

根据以上表格所列,如果需要设置当前用户的最大进程数和文件打开数只要如下设置即可:

  • ulimit -u 8192 设置当前用户的最大进程数为8192,只在当前shell中生效
  • ulimit -n 8192 设置当前用户的最大文件打开数8192,只在当前shell中生效

同时可以通过参数-a查看当前用户的所有限制情况:

[post@PostOS ~]$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 127510
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 32768
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4096
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

可以通过max user processes和open files 看出当前post用户的最大进程数和最大打开文件数分别为32768和8192。

二.设置配置文件

前面介绍了ulimit的方式修改系统资源的限制,但是有局限性:ulimit只对当前shell会话生效。如果重新打开一个shell,配置又重新变成之前的限制了。如果需要永久生效需要修改文件让其配置持久化。

配置文件主要分为:

  • 将命令写至profile和bashrc中,相当于在登陆时自动动态修改限制
  • 在/etc/security/limits.conf中添加记录(需重启生效,并且在/etc/pam.d/中的seesion有使用到limit模块)

第一种方式很好理解,就是每次shell用户登录时,加载当前的用户的profile执行ulimit命令,以达到修改资源限制。
下面主要介绍下:如何修改/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
* soft nofile 32768
* hard nofile 65536
#use for oracle
postgres soft nproc 2047
postgres hard nproc 16384
postgres soft nofile 2048
postgres hard nofile 65536
postgres soft stack 10240

从文件的注释说明中可以看出该文件的配置含义,配置主要分为四个部分:

  • domain部分:用来表示用户名或者组名。@后接用户名或者组名表示用户名或者组名;*表示所有用户
  • type部分:表示资源层面,有两种值soft和hard。soft表示在软件层面,hard表示在硬件层面
  • item部分:表示可以限制的资源类型。core限制核心的文件大小;locks表示用户可以持有的最大文件锁数量;nproc表示最大的进程数;nofile表示最大的打开文件句柄数
  • value部分:限制的资源数量的值

如上面的配置: #@student hard nproc 20,表示student用户在硬件上最大的进程数只有20,* soft nofile 32768表示软件上所有用户的最大文件打开数是32768。

只修改了这个文件还无法完成配置,还需要修改 /etc/security/limits.d/90-nproc.conf配置文件,可以先查看下这个文件的内容:

# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.

*          soft    nproc     8192
root       soft    nproc     unlimited

从注释中可以看出,是针对用户进程的默认限制。从配置中可以看出root用户不做进程数量的限制,其他用户的最大进程数为8192。

关于Linux中为何既有/etc/security/limit.conf又还配置一个limits.d/90-nproc.conf,这个问题由于个人能力有限,无法回答。读者感兴趣可以自行研究,不吝赐教。

参考

ulimit限制之nproc问题

猜你喜欢

转载自www.cnblogs.com/lxyit/p/9204521.html