C/Count free file descriptors

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "This could be improved to detect the number of file descriptors set by ulimit, but most systems are set to ~1024 by default. <source lang="c"> { int i, fd[2048]; for (i = 0...")
 
 

Latest revision as of 17:08, 14 February 2013

This could be improved to detect the number of file descriptors set by ulimit, but most systems are set to ~1024 by default.

{
	int i, fd[2048];
	for (i = 0; i < sizeof(fd) / sizeof(int); i++) {
		if ((fd[i] = open("/dev/null", O_RDONLY)) == -1) break;
	}
	printf("free fds: %d\n", i);
	for (i-- ; i >= 0; i--) {
		close(fd[i]);
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox