File descriptors open info

Henry McGilton henry at angel.Eng.Sun.COM
Thu Feb 28 10:41:16 AEST 1991


In article <15346 at smoke.brl.mil>, gwyn at smoke.brl.mil (Doug Gwyn) writes:
> In article <1991Feb26.213145.26540 at batcomputer.tn.cornell.edu> hurf at theory.tn.cornell.edu (Hurf Sheldon) writes:
	**  is there a Unix system call (or some other method) for
	**  finding out how many file descriptors a process has open?

    *  This is another one of the "why would you possibly care?" category
    *  of questions.  There is no direct support for this, but something
    *  of possible use would be to see what value dup(0) returns (then
    *  close() the duplicate FD).  You will get the lowest available FD
    *  number, which may be sufficient.  By elaborating on this idea you
    *  could also count the FDs initially in use, but why would you want to?

Back in the bad old days of SunView, window applications used two
fd's per subwindow (plus many more fd's for other things.  Because of
the limit of 32 open files per process (later upped to 64 in SunOS
4.0), SunView window applications with lots of subwindows could
potentially run out of fd's.  What we had to do was some kind of
window garbage collection, plus checking that there were enough fd's
available before trying to create a new window and crashing if we
could not.  So we wrote this little bit of code to count the number
of available fd's:

{
    struct stat     statb;
    register int    i, n = 0;
    extern int      errno;

    for (i = getdtablesize() - 1; i >= 0; i--)
	if (fstat(i, &statb) < 0 && errno == EBADF)
	    n++;
}

Mind you, it only works on systems supporting getdtablesize.

	........  Henry



More information about the Comp.unix.questions mailing list