Tom Lane wrote:
> Kevin Brown <[EMAIL PROTECTED]> writes:
> > I originally thought that each shared library that was loaded would eat
> > a file descriptor (since I thought it would be implemented via mmap())
> > but that doesn't seem to be the case, at least under Linux
> 
> Hmm.  This may be OS-specific.  The shlibs certainly show up in the
> output of lsof in every variant I've checked, but do they count against
> your open-file limit?

It seems not, for both shared libraries that are linked in at startup
time by the dynamic linker and shared libraries that are explicitly
opened via dlopen().  This seems to be true for Linux and Solaris (I
wasn't able to test on HP-UX, and AIX yields a strange "bad file number"
error that I've yet to track down).

Attached is the test program I used.  It takes as its arguments a list
of files to hand to dlopen(), and will show how many files it was able
to open before and after running a batch of dlopen() commands.


-- 
Kevin Brown                                           [EMAIL PROTECTED]
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <dlfcn.h>

int *fd;
int size = 1024;

int eatallfds(void) {
	int i = 0;
	int j, myfd;

	while (1) {
		myfd = dup(0);
		if (myfd < 0) {
			fprintf (stderr, "dup() failed: %s\n", strerror(errno));
			break;
		}
		fd[i++] = myfd;
		if (i >= size) {
			size *= 2;
			fd = realloc(fd, size);
			if (fd == NULL) {
				fprintf (stderr, "Can't allocate: %s\n",
						strerror(errno));
				fprintf (stderr, "Had used %d descriptors\n",
						i);
				exit(1);
			}
		}
	}
	for (j = 0 ; j < i ; ++j) {
		close(fd[j]);
	}
	return i;
}


int main (int argc, char *argv[]) {
	int n, na;
	int i;
	void *addr;

	size = 1024;
	fd = malloc(size * sizeof(*fd));
	if (fd == NULL) {
		fprintf (stderr, "Can't allocate: %s\n", strerror(errno));
		return 1;
	}
	n = eatallfds();
	printf ("Was able to use %d file descriptors\n", n);

	na = 0;
	for (i = 1 ; i < argc ; ++i) {
		addr = dlopen(argv[i], RTLD_LAZY);
		if (addr != NULL) na++;
	}
	n = eatallfds();
	printf ("Was able to use %d file descriptors after opening %d shared libs\n", n, na);
	return 0;
}

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Reply via email to