file-max与ulimit的关系与差别

 http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/proc.txt?id=9cfe015aa424b3c003baba3841a60dd9b5ad319b

http://blog.yufeng.info/archives/1380

1. NR_OPEN is the maximum number of files that can be opened by process

    NR_OPEN是一个进程可以打开的最大文件数

    A process cannot use more than NR_OPEN file descriptors.

    一个进程不能使用超过NR_OPEN文件描述符

The kernel also enforces a dynamic bound on the maximum number of file descriptors in the signal->rlim[RLIMIT_NOFILE] structure of the process descriptor; this value is usually 1,024, but it can be raised if the process has root privileges.

2. NR_FILE is the limit on total number of files in the system at any given point in time

    NR_FILE 是系统在某一给定时刻,限制的文件总数

在linux kernel 2.6.25之前通过ulimit -n(setrlimit(RLIMIT_NOFILE))设置每个进程的最大打开文件句柄数不能超过NR_OPEN (1024*1024),也就是100多w(除非重新编译内核),而在25之后,内核导出了一个sys接口可以修改这个最大值(/proc/sys/fs /nr_open).具体的changelog在这里

1. file-max的含义

man proc,可得到file-max的描述:

/proc/sys/fs/file-max
              This  file defines a system-wide limit on the number of open files for all processes.  (See
              also setrlimit(2),  which  can  be  used  by  a  process  to  set  the  per-process  limit,
              RLIMIT_NOFILE,  on  the  number  of  files it may open.)  If you get lots of error messages
              about running out of file handles, try increasing this value:

即file-max是设置 系统所有进程一共可以打开的文件数量 。同时一些程序可以通过setrlimit调用,设置每个进程的限制。如果得到大量使用完文件句柄的错误信息,是应该增加这个值。

也就是说,这项参数是系统级别的。

echo  6553560 > /proc/sys/fs/file-max            //因为是在/proc文件系统中,重起失效

或修改 /etc/sysctl.conf, 加入                           // 永久生效

fs.file-max = 6553560 重启生效

2. ulimit的   //暂时生效

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

即设置当前shell以及由它启动的进程的资源限制。

显然,对服务器来说,file-max, ulimit都需要设置,否则就可能出现文件描述符用尽的问题,为了让机器在重启之后仍然有效,强烈建立作以下配置,以确保file-max, ulimit的值正确无误:

1. 修改/etc/sysctl.conf, 加入             

fs.file-max = 6553560

2.系统默认的ulimit对文件打开数量的限制是1024,修改/etc/security/limits.conf并加入以下配置,永久生效

* soft nofile 65535 
* hard nofile 65535

修改完之后,重启即可生效

3./proc/sys/fs /nr_open

提高系统整体上限

原文地址:https://www.cnblogs.com/zengkefu/p/5592117.html