问题
I am trying to use setrlimit to limit the number of processes a program can create. Here is my code:
struct rlimit limiter;
getrlimit( RLIMIT_NPROC, &limiter );
limiter.rlim_max = limiter.rlim_cur = 10;
setrlimit( RLIMIT_NPROC, &limiter );
int val = fork();
printf( "Error number %d\n", errno ); //gives 11
if( val == -1 ) {
printf( "Fork failed\n" );
} else if( val ) {
printf( "parent\n" );
} else {
printf("child\n" );
}
return 0;
Since the value of rlim_max
and rlim_cur
is 10, my program should be allowed to fork 10 processes. But right now it is failing even for a single fork call. Though it works if I set the values to around 250. I don't understand why is that. I want a way to limit the number of forks to n
.
来源:https://stackoverflow.com/questions/32649829/setting-low-rlimit-nproc-value-doesnt-allow-even-a-single-fork