Vivek Goel
Vivek Goel

Reputation: 24140

Getting count of current used file descriptors from C code

Is there a C API to get the:

  1. Current used file descriptors system wide
  2. Current used file descriptors of the current process

Upvotes: 18

Views: 17566

Answers (5)

FelipeFR
FelipeFR

Reputation: 149

To complement on Some programmer dude's answer, the most efficient (both in terms of space and time) way of getting the number of open fds for the current process is to walk /proc/self/fds where available.

This should do it:

#include <dirent.h>
#include <stddef.h>
#include <sys/types.h>

int count_open_fds(void) {
    DIR *dp = opendir("/proc/self/fd");
    struct dirent *de;
    int count = -3; // '.', '..', dp

    if (dp == NULL)
        return -1;

    while ((de = readdir(dp)) != NULL)
        count++;

    (void)closedir(dp);

    return count;
}

Upvotes: 2

MD XF
MD XF

Reputation: 8129

I'm not positive about file descriptors, but you can easily check <stdio.h> files.

In stdio.h, __sF, is a file array that stores every FILE. (googling __sF shows many stdios with a matching keyword).

If a FILE's flags are empty, the file is not in use. Therefore, we can simply walk through __sF, checking the flag of each FILE in the array.

#include <stdio.h>

int getOpenFileCount(void)
{
    int fileCount;

    for (fileCount = 0; __sF[fileCount]._flags != 0; fileCount++)
        continue;

    return fileCount;
}

Upvotes: -1

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215193

For the current process count, you can use getrlimit to get the file descriptor limit, then iterate over all integers from 0 to that limit and try calling fcntl with the F_GETFD command. 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 of struct 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 the events member set to 0. Call poll on the array with 0 timeout, and look for the POLLNVAL flag in the revents for 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.

Upvotes: 23

Polynomial
Polynomial

Reputation: 28316

You can read /proc/sys/fs/file-nr to find the total number of allocated and free file system handles as well as the maximum allowed.

[root@box proc]# cat /proc/sys/fs/file-nr
3853    908     53182
|       |       |
|       |       |
|       |       max: maximum open file descriptors
|       free: total free allocated file descriptors
allocated: total allocated file descriptors since boot

To calculate the number that are currently in use, just do allocated - free. You could also calculate a percentage of used descriptors by doing ((allocated - free) / max) * 100

As for per-process, I'm not sure of any programmatic way you can do it.

Here's a tutorial on how to do it with lsof anyway: http://linuxshellaccount.blogspot.com/2008/06/finding-number-of-open-file-descriptors.html

Upvotes: 13

Some programmer dude
Some programmer dude

Reputation: 409136

Since you say you are on Linux, you can open the folder /proc/self/fd/ which should contain symbolic links to all open file descriptors.

Upvotes: 15

Related Questions