setting low rlimit_nproc value doesn't allow even a single fork

◇◆丶佛笑我妖孽 提交于 2019-12-24 14:34:24

问题


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

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