get number of open files in a process?

Mateusz Guzik mjguzik at gmail.com
Thu Jan 21 22:51:15 UTC 2016


On Thu, Jan 21, 2016 at 11:45:11PM +0100, Jilles Tjoelker wrote:
> On Thu, Jan 21, 2016 at 07:44:01PM +0000, Batutis, Ed wrote:
> > > However, the real question is what do you need this for.
> 
> > I would like to know when I am getting close to hitting the file
> > handle limit. This is useful for two reasons: 1) if I run out of
> > available file handles this will most likely cause my process to
> > crash. If I start logging when the count begins to get dangerously
> > high, then I might after the fact be able to determine what was going
> > on to cause that. 2) Some handles are used by a user interface. It
> > would be nice if the server could reject connections when the open
> > handle count hit a critical threshold so the user interface doesn't
> > become part of the problem - obscuring the actual underlying cause.
> 
> You can check the integer file descriptor that was just created, since
> if the descriptor is N, at least N other descriptors must be active at
> the same time. For example, if a new client's file descriptor is higher
> than the limit minus some safety margin, reject the connection.
> 

This cannot work. POSIX mandates the lowest free fd is returned, so the
N you see here can be extremely low, while the process is running out of
fds.

The sysctl I mentioned in my mail can be trivially backported to other
FreeBSD versions, if this kind of feature is really needed.

I'm still not convinced the approach here (of checking fds) is the right
one to take - you have to have a very big reserve to not suddenly run
out of fds, one would just limit amount of clients and, well, handle
errors as they can pop up for reasons other than running out of fd slots.

-- 
Mateusz Guzik <mjguzik gmail.com>


More information about the freebsd-hackers mailing list