Following this SO question and this answer in particular, it seems that calling setrlimit after a printf make it not working.
Here is the example code:
#include <stdio.h>
#include <sys/resource.h>
int main()
{
struct rlimit rlp;
FILE *fp[10000];
int i;
printf("Hello\n");
rlp.rlim_cur = 10000;
rlp.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_NOFILE, &rlp);
getrlimit(RLIMIT_NOFILE, &rlp);
printf("limit %lld %lld\n", rlp.rlim_cur, rlp.rlim_max);
for(i=0;i<10000;i++) {
fp[i] = fopen("a.out", "r");
if(fp[i]==0) { printf("failed after %d\n", i); break; }
}
}
Here is the console output:
Hello
limit 10000 9223372036854775807
failed after 4861
If I comment the first printf
, here is the console output:
limit 10000 9223372036854775807
failed after 9967
Is there any reason for that?
[Edit] I am running MAc OS X 10.7.5 with Xcode 4.6.2.