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

提高系統整體上限

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM