getrlimit() returns wrong value?

走远了吗. 提交于 2019-12-24 03:35:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!