6

I must run the command "ulimit -n 400" to raise number of allowed open files before I start my program written in C, but is there a way to do the equivalent from within the C program?

That is, increase the number of allowed open file descriptors for that process. (I am not interested in per thread limits.)

Will it involve setting ulimits, then forking a child which will be allowed to have more open files?

Of course, I could write a shell wrapper which runs ulimit, then start my C program, but it feels less elegant. I could also grep through the source code of bash or sh to see how it is done there - maybe I will if I get no answer here.

Also related, if you want to select on a lot of file descriptors, look here.

Community
  • 1
  • 1
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
  • I am afraid that you would be able to lower the limit but not to raise it from the same process. – Pascal Cuoq Nov 02 '10 at 10:43
  • @Pascal, I figured that might be the case, but there must be a way to raise for a child, or the ulimit command would have no effect. – Prof. Falken Nov 02 '10 at 10:45
  • Couldn't you call as shell doing that from the C program ? – Raveline Nov 02 '10 at 10:46
  • @Raveline, not really. When you run "ulimit" in sh or bash, it doesn't really run a new command. It changes how many files its child will be allowed to have open. (Check with "which ulimit", it does not exist.) Running ulimit would change nothing, when the called shell exits from the C program, all ulimit changes would instantly disappear. No, it must be something done from within the first process itself. – Prof. Falken Nov 02 '10 at 10:48
  • @AmigableClarkKent: I'm pretty sure that you need to be a _privileged_ user to raise resource limits. – D.Shawley Nov 02 '10 at 10:50
  • 1
    You can raise your own limit from the soft limit to the hard limit. – Douglas Leeder Nov 02 '10 at 10:53

2 Answers2

11

I think that you are looking for setrlimit(2).

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
9
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main (int argc, char *argv[])
{
  struct rlimit limit;
  
  limit.rlim_cur = 65535;
  limit.rlim_max = 65535;
  if (setrlimit(RLIMIT_NOFILE, &limit) != 0) {
    printf("setrlimit() failed with errno=%d\n", errno);
    return 1;
  }

  /* Get max number of files. */
  if (getrlimit(RLIMIT_NOFILE, &limit) != 0) {
    printf("getrlimit() failed with errno=%d\n", errno);
    return 1;
  }

  printf("The soft limit is %lu\n", limit.rlim_cur);
  printf("The hard limit is %lu\n", limit.rlim_max);

  /* Also children will be affected: */
  system("bash -c 'ulimit -a'");

  return 0;
}
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
  • Adding my own answer with a simple example and the correct define (RLIMIT_NOFILE) for setting number of open files. – Prof. Falken Nov 02 '10 at 11:05
  • Good, but according to Clang it should be %lu, not %llu. Well, the latter also gave me wrong numbers (are you're on x86_64?). As usual, GCC just ignored that bug. – asdf Jul 06 '11 at 10:10