Thursday, October 11, 2012

How to change the per process open file limit in Linux


While ulimit -n can be used for changing the process open file descriptor limit, in the current shell. This limit is NOT set for system wide processes. The way to set a system wide total open file descriptor limit is to add an entry to /etc/sysctl.conf as:
fs.file-max = FILE_FD_NUM 

The sysctl settings can be loaded using sysctl -p
User specific fd limits can be set in /etc/security/limits.conf

But this does not increase per process open fd limit. One  way of doing it is by the use of set the limit using the system call setrlimit(). The accessor function is getrlimit(). 

These system calls modify the rlimit struct for a given process resource. A few example would be core size (RLIMIT_CORE) limit, or open fd (RLIMIT_NOFILE) limits or  data segment  (RLIMIT_DATA) limit. Quite a few resources can be modified likewise ( do man setrlimit for more information).

It would be interesting to note do processes spawned from the current process inherit the limits values or not. 


(P.S. The diary of a geek @ http://blog.andrew.net.au/2011/06/25#gdb_rlimits does have much better information on the same topic)