I haven't made an intensive search, but I don't think what you're looking for exists on Linux. Opening a file descriptor doesn't take any global lock, only a per-process lock, so on a multicore machine whatever you'd be using to count the number of open file descriptors could be running literally at the same time that other threads is opening or closing files on other cores.
Linux doesn't have a global limit on the total number of open files. There's no explicit per-user limit either. There's a per-user limit on processes, and a per-process limit on file descriptor numbers, which indirectly imposes a limit on open files per user, but that isn't explicitly tracked.
Exploring /proc
(which is what lsof
does under the hood) is as good as it gets. /proc
is the Linux API to get information about processes.
it would refuse to create FD if max limit for user is exhausted
? The setrlimit/getrlimit system calls work aper-process
base. diskquota works on aper filesystem
base. AFAIK, there is no API that works on aper user
base. – andcoz Apr 25 '16 at 14:46lsof
(with non atomic constraint) or read kernel sources and build your own utility. A good question anyway. – Archemar Apr 25 '16 at 15:03RLIMIT_NOFILE
set to 100, she'll can have two processes with 99 open files (198 in total). – andcoz Apr 25 '16 at 15:20