linux network open file descriptor
Anyone who working on network programming will often comes to face some errors reported on the limitation of the network connection. This issue will only comes into view when that application is deploy in an environment with heavy connections during the life testing. Developers will start scratching their head “where went wrong with their program”, while all the logic and technology are well implemented. The solution might be just the issue of the operating system open file descriptor settings.
A small info on open file descriptor. It governs the number of sockets that the system is allowed to create. There is a huge different between”Open File” and”Open File Descriptor”. I found an interesting explanation here.
In my experience of deploying a gateway I developed, the problem was solve with a tweak on the operating network file descriptor. Take note, this implementation is on Linux machine. The hero here is the command ulimit in Linux. ulimit provides control over the resources available to the shell and to processes started by it, on systems that allow such control, which is mostly Linux systems.
To view the current system resource limitation settings, simple type : ulimit -a. Sample Result will like below (might be different on different systems)
[GW ~]$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 49129
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
What we are interested here is the open files entry. We want to increase this limitation. Before that, we have to be aware that there is two limitation type which is hard and soft limits. A hard limit cannot be increased once it is set; a soft limit may be increased up to the value of the hard limit. Following are steps to increase the file descriptor limit:
1. Edit /etc/security/limits.conf by adding the following lines:
* soft nofile 1024
* hard nofile 65535
2. Edit /etc/pam.d/login, add the following line:
session required /lib/security/pam_limits.so
3. Next we increase the system file descriptor limit in the file-max file with following command :
echo 65535 > /proc/sys/fs/file-max
4. We can then tell the system to increase the file descriptor as has been set in the configuration with the following command:
ulimit -n unlimited
*** Take note that we need to relogin before the changes take effect.
After the changes, if we like to view the number of open file descriptors being used, we can view the file /proc/sys/fs/file-nr. Some description of the content :
2660 10 309651
| | |_____maximum open file descriptors
| |__________total free allocated file descriptors
|_______________ total allocated file descriptors
We have to do a little math to get the total currently used open file descriptor. We need to subtract the total free allocated file descriptors from the total allocated file descriptors. In this particular case, we have 2650 open files descriptors(sockets).