How many file open ?

Hi !

How can I find out how many file descriptors a process has open ?  Either I can use a system call but preferrably I would like to put it in the program itself.  I am on Sun Solaris machine.

Marvin.
checkinAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
kejinConnect With a Mentor Commented:
This is the solaris code you need:

#include <dirent.h>

int total_open_fds(int pid)
{
        DIR*    pdd;
        int     count = 0;

        char path[128];
        sprintf(path, "/proc/%d/fd", pid);

        pdd = opendir(path);

        if( !pdd ) return -1; // indicate failure
       
        while(readdir(pdd)) {
                count++;
        }

        return count - 3; // ".", "..", and the pdd.dd_fd
}
0
 
ozoCommented:
You could try scaning the __iob[] structure.
0
 
kejinCommented:
Sorry, ozo, I didn't see you already post a comment.
0
 
kejinCommented:
Also, to ozo, my understanding is, __iob[]  only includes fds opened via fopen() , fdopen() and fdopen(). Other fds opened through open(), pipe(), dup(), socket(), accept(),  etc.. are not included.
0
 
kejinCommented:
code correction:

the last code line in the proposed answer:

     return count -3;

should change to:

     if( pid == getpid() )
        return count - 3;
     else
        return count -2;
0
All Courses

From novice to tech pro — start learning today.