develooper Front page | perl.perl5.porters | Postings from November 2011

Re: [perl #6847] strange behaviour when forking lots of times

Thread Previous | Thread Next
Aaron Crane
November 23, 2011 02:46
Re: [perl #6847] strange behaviour when forking lots of times
Message ID:
James E Keenan via RT <> wrote:
> On Sat Apr 21 01:36:46 2001, wrote:
>> $ perl -we 'fork || print for split //, "a" x 10'
>> when I run that on my computer, I get strange output.  Instead of
>> printing "a" 2**10 times, the output contains random "1"'s, like this:
>> aa1aaaaa1aaa1aaaaa1aaa1aaaa1aaa1aaaaaa1aaaaa1aa1aaaaaa1a
>> It also affects different computers differently; the "x 10" needs to
>> be replaced by a higher value to get the same effect sometimes (but
>> not too high, or you'll fork-bomb your computer).
> I have recently confirmed that this behavior still exists.  It taught me
> the meaning of 'fork-bomb'. :-(

A hypothesis: this is being run with an RLIMIT_NPROC in place (aka
`ulimit -u`).  In particular, if RLIMIT_NPROC is at or near 2**n, then
some of the forks are likely to fail with EAGAIN when the literal 10
in the code is replaced with an integer >= n; whereas choosing a lower
number will make things work.  That certainly matches the behaviour
I'm seeing.  This slightly modified code should emit "a" in every
child process, "1" if the fork failed with EAGAIN, or "0" if the fork
failed for some other reason:

$ ulimit -u
$ perl -MErrno=EAGAIN -we '(fork // print 0 + ($! == EAGAIN)) || print
for split //, "a" x 8'; sleep 0.2; echo

So when we can have just over 2**8 processes, and we try to start
(2**8)-1 in very quick succession, we get some EAGAIN failures, as
expected.  If we try to start only (2**7)-1 processes, they all

$ perl -MErrno=EAGAIN -we '(fork // print 0 + ($! == EAGAIN)) || print
for split //, "a" x 7'; sleep 0.2; echo

But if we then reduce the ulimit again, we see more errors:

$ ulimit -u 128
$ perl -MErrno=EAGAIN -we '(fork // print 0 + ($! == EAGAIN)) || print
for split //, "a" x 7'; sleep 0.2; echo

If I'm right about this, I don't think there's a bug here.

Aaron Crane **

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About