[comp.lang.c] how many file descriptors?

mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (09/10/88)

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?

I have a SunView program that uses many frames and I want to be able to
check that I won't exceed my per-process file descriptor limit (30 in
SunOS 3.x) before creating a new frame.

Thanks,
Mike Khaw
-- 
internet: mkhaw@teknowledge.arpa
uucp:	  {uunet|sun|ucbvax|decwrl|uw-beaver}!mkhaw%teknowledge.arpa
hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

bill@proxftl.UUCP (T. William Wells) (09/14/88)

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?

Here is one way:

Call getdtablesize to determine the number of fd's that are in
the per-process file table.  Then call fcntl for each fd to
retrieve some property of each file.  Each fcntl failure
represents an available fd.

---
Bill
novavax!proxftl!bill

walker@island.uu.net (Richard Walker) (09/15/88)

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);
}