Skip to content

Instantly share code, notes, and snippets.

@sahib
Created February 24, 2015 19:30
Show Gist options
  • Save sahib/a2a27a58616fce6366b1 to your computer and use it in GitHub Desktop.
Save sahib/a2a27a58616fce6366b1 to your computer and use it in GitHub Desktop.
Find number of file descriptor per process (initially 3 usually)
int rm_fd_cache_get_fds_per_process(RmFdCache *self) {
g_assert(self);
int n = -1;
struct rlimit rl;
if(getrlimit(RLIMIT_NOFILE, &rl) == -1) {
return -1;
}
struct pollfd *fds = g_malloc0(rl.rlim_cur * sizeof(struct pollfd));
for(rlim_t i = 0; i < rl.rlim_cur; ++i) {
fds[i].fd = i;
fds[i].events = 0;
}
if(poll(fds, rl.rlim_cur, 0) == -1) {
goto cleanup;
}
n = rl.rlim_cur;
for(rlim_t i = 0; i < rl.rlim_cur; ++i) {
n -= !!(fds[i].revents & POLLNVAL);
}
cleanup:
g_free(fds);
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment