7

On *NIX systems, is there a way to find out how many open filehandles are there in the current running process?

I am looking for an API or a formula for use in C, from within the running process in question.

ϹοδεMεδιϲ
  • 2,790
  • 3
  • 33
  • 54

4 Answers4

6

On certain systems (see below) you can count them in /proc/[pid]/fd. If not on one of those, see below for: wallyk's answer.

In c, you can list the dir and count the total, or list the dir contents:

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

 int
 main (void)
 {
   DIR *dp;
   struct dirent *ep;

   dp = opendir ("/proc/MYPID/fd/");
   if (dp != NULL)
     {
       while (ep = readdir (dp))
         puts (ep->d_name);
       (void) closedir (dp);
     }
   else
     perror ("Couldn't open the directory");

   return 0;
 }

In bash, something like:

ls -l /proc/[pid]/fd/ | wc -l

Operating systems that support the proc filesystem include, but are not limited to:
Solaris
IRIX
Tru64 UNIX
BSD
Linux (which extends it to non-process-related data)
IBM AIX (which bases its implementation on Linux to improve compatibility)
QNX
Plan 9 from Bell Labs

Community
  • 1
  • 1
chown
  • 51,908
  • 16
  • 134
  • 170
  • 2
    This is not portable to e.g. FreeBSD systems as they do not have the /proc/ filesystem. Also: This does not answer the OP's question. – arne Nov 17 '11 at 07:56
  • I liked wallyk's answer, it is more portable, and relies on very few facilities provided by the subsystem. But, in my case, I need to make sure the attempt to count file-handles has a high chance to succeed; i.e by not having to open and close more file-handles. In that sense, chown's solution is better, as it only uses one more file-handle in the effort. – ϹοδεMεδιϲ Nov 17 '11 at 12:22
  • 2
    @CodeMedic: wallyk solution requieres only one additional file handler at any time as it opens and closes it inside the loop. – salva Nov 17 '11 at 19:30
  • @salva Although, each fd has to be dup'd, which is probably fine, but *might* slow down for things like sockets or open fd's across mounts (nothing to back this statement up, it just seems reasonably possible).. There are also the 3.9k extra loop runs that method has to do (which is negligible anyway since they just continue when dup fails). But for non /proc filesystems, it seems like the best alternative. – chown Nov 17 '11 at 19:47
6

An idea which comes to mind which should work on any *nix system is:

int j, n = 0;

// count open file descriptors
for (j = 0;  j < FDMAX;  ++j)     // FDMAX should be retrieved from process limits,
                                  // but a constant value of >=4K should be
                                  // adequate for most systems
{
    int fd = dup (j);
    if (fd < 0)
        continue;
    ++n;
    close (fd);
}
printf ("%d file descriptors open\n", n);
wallyk
  • 56,922
  • 16
  • 83
  • 148
1

OpenSSH implements a closefrom function that does something very similar to what you need mixing the two approaches already proposed by wallyk and chown, and OpenSSH is very portable, at least between Unix/Linux/BSD/Cygwin systems.

salva
  • 9,943
  • 4
  • 29
  • 57
0

There is no portable way to get the number of open descriptors (regardless of type), unless you keep track of them yourself.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621