Home > Back-end >  why open file descriptors are not getting reused instead they are increasing in number value
why open file descriptors are not getting reused instead they are increasing in number value

Time:10-11

I have a simple C program that is http server. So I close file descriptor for disk files and new connection fd return of accept(...) but I noticed that I am getting new file descriptor numbers that are bigger than the previous numbers for example file descriptor from accept return starts with 4,then 5 then 4 again and so on until file descriptor reaches max open file descriptor on a system. I have set the value to 10,000 on my system but I am not sure why exactly file descriptor number jumps to max value And I am kind of sure than my program is closing the file descriptors. So I like to know if there are not thousands of connections then how come file descritor new number are increasing periodically like in around 24 hours I get message accept: to many open files what is this message. also does ulimit -n number value gets reset automatically without system reboot

as mentioned in the answer. The output of _2$ ps aux | grep lh is

dr-x------ 2 fawad fawad  0 Oct 11 11:15 .
dr-xr-xr-x 9 fawad fawad  0 Oct 11 11:15 ..
lrwx------ 1 fawad fawad 64 Oct 11 11:15 0 -> /dev/pts/3
lrwx------ 1 fawad fawad 64 Oct 11 11:15 1 -> /dev/pts/3
lrwx------ 1 fawad fawad 64 Oct 11 11:15 2 -> /dev/pts/3
lrwx------ 1 fawad fawad 64 Oct 11 11:25 255 -> /dev/pts/3

and the output of ls -la /proc/$$/fd is

root       49855  0.5  5.4 4930756 322328 ?      Sl   Oct09  15:58 /usr/share/atom/atom --executed-from=/home/fawad/Desktop/C  -work/lhparse --pid=49844 --no-sandbox
root       80901  0.0  0.0  25360  5952 pts/4    S    09:32   0:00 sudo ./lh
root       80902  0.0  0.0 1100852 2812 pts/4    S    09:32   0:00 ./lh
fawad      83419  0.0  0.0  19976   916 pts/3    S    11:27   0:00 grep --color=auto lh

I like to know what is pts/4 etc. column. is this the file descriptor number.

CodePudding user response:

It's likely that the socket that is represented by the file descriptor is in close_wait or time_wait state. Which means the OS holds the fd open for a bit longer. So you won't be able to reuse it immediately in this instance.

Once the socket is fully finished with, the file descriptor number will then available for reuse inside your program.

See: https://en.m.wikipedia.org/wiki/Transmission_Control_Protocol

Protocol Operation and specifically Wait States.

To see what files are still open you can run

ls -la /proc/$$/fd

The output of this will also be of help.

ss -tan | head -5
LISTEN     0  511             *:80              *:*
SYN-RECV   0  0     192.0.2.145:80    203.0.113.5:35449
SYN-RECV   0  0     192.0.2.145:80   203.0.113.27:53599
ESTAB      0  0     192.0.2.145:80   203.0.113.27:33605
TIME-WAIT  0  0     192.0.2.145:80   203.0.113.47:50685
  • Related