Is there a C API to get the:
- Current used file descriptors system wide
- Current used file descriptors of the current process
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For the current process count, you can use
getrlimitto get the file descriptor limit, then iterate over all integers from 0 to that limit and try callingfcntlwith theF_GETFDcommand. It will succeed only on the file descriptors which are actually open, letting you count them.Edit: I now have a better way to do it. After getting the
rlimit, make a large array ofstruct pollfd(as large as the limit if possible; otherwise you can break it down into multiple runs/calls) with each fd in the range and theeventsmember set to 0. Callpollon the array with 0 timeout, and look for thePOLLNVALflag in thereventsfor each member. This will tell you which among a potentially-huge set of fds are invalid with a single syscall, rather than one syscall per fd.