title: How to increase the limit of "maximum open files" in C on Mac OS X
body: <p>The default limit for the max open files on Mac OS X is 256 (ulimit -n) and my application needs about 400 file handlers.</p>
  
  <p>I tried to change the limit with setrlimit() but even if the function executes correctly, i'm still limited to 256.</p>
  
  <p>Here is the test program I use:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/resource.h&gt;
  
  main()
  {
    struct rlimit rlp;
  
    FILE *fp[10000];
    int i;
  
    getrlimit(RLIMIT_NOFILE, &amp;rlp);
    printf("before %d %d\n", rlp.rlim_cur, rlp.rlim_max);
  
    rlp.rlim_cur = 10000;
    setrlimit(RLIMIT_NOFILE, &amp;rlp);
  
    getrlimit(RLIMIT_NOFILE, &amp;rlp);
    printf("after %d %d\n", rlp.rlim_cur, rlp.rlim_max);
  
    for(i=0;i&lt;10000;i++) {
      fp[i] = fopen("a.out", "r");
      if(fp[i]==0) { printf("failed after %d\n", i); break; }
    }
  
  }
  </code></pre>
  
  <p>and the output is:</p>
  
  <pre><code>before 256 -1
  after 10000 -1
  failed after 253
  </code></pre>
  
  <p>I cannot ask the people who use my application to poke inside a /etc file or something. I need the application to do it by itself.</p>