• 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.

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.

You could try scaning the __iob[] structure.
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)) {

        return count - 3; // ".", "..", and the pdd.dd_fd

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
Sorry, ozo, I didn't see you already post a comment.
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.
code correction:

the last code line in the proposed answer:

     return count -3;

should change to:

     if( pid == getpid() )
        return count - 3;
        return count -2;
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

From novice to tech pro — start learning today.