Xref: utzoo comp.lang.c:12592 comp.unix.questions:9205 Path: utzoo!attcan!uunet!island!walker From: walker@island.uu.net (Richard Walker) Newsgroups: comp.lang.c,comp.unix.questions Subject: Re: how many file descriptors? Message-ID: <360@island.uu.net> Date: 14 Sep 88 18:21:45 GMT References: <24889@teknowledge-vaxc.ARPA> Reply-To: walker@island.uu.net (Richard Walker) Organization: Island Graphics, Marin County, California Lines: 43 In article <24889@teknowledge-vaxc.ARPA> mkhaw@teknowledge-vaxc.UUCP (Mike Khaw) writes: >How can a SunView program tell how many file descriptors it has in use, >especially if it opens&closes /dev/fb several times and creates&destroys >frames several times while it's running? >Thanks, >Mike Khaw This is one of the things I hate about the Unix/Sunview combination. Sunview uses file descriptors for its user interface objects yet provides no control over allocation and release. (They do get released *eventually* after the destruction). Here are functions to determine the number of free file descriptors. free_file_descriptors() { int i, n; struct stat statb; extern int errno; /* There is a slop factor of 2 in the fd calculations becuse the window system uses fd's transiently */ n = getdtablesize(); for (i = n-1; i >= 0; i--) if (!(fstat(i, &statb) < 0 && errno == EBADF)) n--; /* RJW reserve SunView slop of 2 and 2 for prompt dialog */ return(n-4); } /* * Return a flag indicating that there are 'n' free descriptors. */ enough_fds(n) int n; { if(n > free_file_descriptors()) { err_prompt("There are too many panels open.\n\ Please close some panels and try again."); return(0); } return(1); }