Too many open files? Increasing the limit in C.
I ran into error 24 today: "Too many open files". Ooops. The solution that I
eventually went for is not among the first Google hits for
"increase limit open files"
so I felt like blogging about it here. There are two functions getrlimit
and
setrlimit
that allow querying and modifying certain resource limits, e.g.
the maximum number of open files for a single process (and they do have a
shared, detailed man page). For each of the resource limit types, there is a
hard and a soft limit: the soft limit, is the one you run into. In my case the
soft is 1024, the hard one is 4096. The cool things is: Any non-root process
can increase the soft limit to the hard one. That's my rescue. Simplified,
this is what I do:
#include <sys/time.h> #include <sys/resource.h> ... struct rlimit open_file_limit; /* Query current soft/hard value */ getrlimit(RLIMIT_NOFILE, &open;_file_limit); /* Set soft limit to hard limit */ open_file_limit.rlim_cur = open_file_limit.rlim_max; setrlimit(RLIMIT_NOFILE, &open;_file_limit);
Works like a charm.