Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!exodus!appserv!angel.Eng.Sun.COM!henry From: henry@angel.Eng.Sun.COM (Henry McGilton) Newsgroups: comp.unix.questions Subject: Re: File descriptors open info Message-ID: <471@appserv.Eng.Sun.COM> Date: 27 Feb 91 23:41:16 GMT References: <1991Feb26.213145.26540@batcomputer.tn.cornell.edu> <15346@smoke.brl.mil> Sender: news@appserv.Eng.Sun.COM Lines: 35 In article <15346@smoke.brl.mil>, gwyn@smoke.brl.mil (Doug Gwyn) writes: > In article <1991Feb26.213145.26540@batcomputer.tn.cornell.edu> hurf@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