0

I am trying to create a performance display for my IOT device. The device uses Linux so , i want to get the number of file handles programmatically using C program. Currently i am using libprocps API to get all other data. Now i want to get the open file descriptors/handles for whole IOT device.

ls /proc/[pid]/fd |wc -l gives the number of files. But this data i need from any API in C Program. Which variable holds the fdinfo in proc

1
  • IMO this should go to stackoverflow.
    – Jiri B
    Mar 26, 2021 at 13:42

1 Answer 1

0

You can’t get this information from libprocps, but it can be obtained, per process, by counting the number of links in the directory (this is the equivalent of your ls /proc/[pid]/fd | wc -l command):

#include <dirent.h>

...

int fds = 0;
DIR * dirp;
struct dirent * entry;

dirp = opendir("/proc/.../fd"); /* You need to build the path */
while ((entry = readdir(dirp)) != NULL) {
    if (entry->d_type == DT_LNK) {
         fds++;
    }
}
closedir(dirp);

You’ll need to add error-handling to the above.

If you want to get the overall number of allocated file handles, look at /proc/sys/fs/file-nr: the first number there is the number of allocated file handles.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .