• C

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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
You could try scaning the __iob[] structure.
0
kejinCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.