C/Count free file descriptors

From Attie's Wiki
(Redirected from Count free file descriptors)
Jump to: navigation, search

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