dgaramond2 has asked for the wisdom of the Perl Monks concerning the following question:

Anyone familiar with BSD::Resource and how setrlimit(RLIMIT_NPROC) works in Linux?

I'm confused by how RLIMIT_NPROC works. What is it limiting, exactly? It seems that when I do setrlimit(RLIMIT_NPROC, $soft, $hard) and $soft<=$hard, then I cannot do any double fork (like system("echo 123")) at all, no matter whether $soft is 1 or 100.

perl -MBSD::Resource -le'setrlimit(RLIMIT_NPROC,20,50); system qq(echo 1);' # hangs, fork fails with EAGAIN perl -MBSD::Resource -le'setrlimit(RLIMIT_NPROC,20,20); system qq(echo 1);' # also hangs

But when $soft >= ($hard+1) there doesn't seem to be any limit that I'm hitting, I can do multiple forks even when $soft=2 and $hard=1.

perl -MBSD::Resource -le'setrlimit(RLIMIT_NPROC,20,10); system qq(echo 1);' # succeeds perl -MBSD::Resource -le'setrlimit(RLIMIT_NPROC,2,1); system qq(echo 1);' # succeeds perl -MBSD::Resource -le'setrlimit(RLIMIT_NPROC,2,1); for(1..10){if(fork==0){print"child $_";sleep 1;exit}}; waitpid(-1,0)' # succeeds, prints "child 1".."child 10"

I'm using 2.4 kernel on Debian Sarge/i386.

Replies are listed 'Best First'.
Re: BSD::Resource and RLIMIT_NPROC on Linux
by sgifford (Prior) on Apr 01, 2006 at 16:07 UTC
    RLIMIT_NPROC controls the total number of processes that uid can have running at once. So for example if you're doing this as the Apache user and you already have 100 processes running, setting a limit of 100 will stop you from creating any new processes.

    At least, that's how it's supposed to work.

      OK, got it. So the total number of processes is counted for all existing processes, not just the new/child processes. My test script failed because even when I set $soft and $hard to 100, I have already more than 100 processes lying around at that time, so I cannot make new processes. And when I set $soft larger than $hard (e.g. $soft=2 and $hard=1), the behaviour becomes... well, let's say undefined, since it's not described in the manpages.