问题
Can someone explain to me why the following program creates 7185 threads instead of 7455?
void *thr_crt(void *arg)
{
sleep(64);
return 0;
}
int main(void)
{
struct rlimit lim;
int err;
int i;
pthread_t tid;
if(getrlimit(RLIMIT_NPROC, &lim) < 0)
perror("getrlimit error"), exit(1);
i = 1;
while(pthread_create(&tid, NULL, thr_crt, NULL) == 0)
i++;
printf("soft limit: %d\n", lim.rlim_cur);
printf("hard limit: %d\n", lim.rlim_max);
printf("threads %d\n", i-1);
return 0;
}
output:
soft limit: 7455
hard limit: 7455
threads 7185
It should not create 7455 threads?
回答1:
The maximum thread number RLIMIT_NPROC is (IIRC) per "real" user id and not per process, so it makes sense that it is somewhat lower per process either because of other already running processes with the same "real" user id or as some sort of protection against rapid resource exhaustion.
回答2:
Check /proc/sys/kernel/threads-max
, as it might be lower than the allowed processes.
来源:https://stackoverflow.com/questions/32204824/getrlimit-returns-wrong-value