in reply to Re: Ulimit makes program hang (bug)
in thread Ulimit makes program hang

I don't think it's a bug. The fork() doesn't fail - the fork() is blocked. (Or rather, fork(1) fails, setting errno to EAGAIN).

You can easily test it. Copy the program, so you have foo calling itself and bar calling itself. Now do from on shell:

(ulimit -u 50 ./foo)
This will print a bunch of pids, and the program "hangs". Now from another shell do:
(ulimit -u 60 ./bar)
This will print 10 or less pids, and it hangs. Go back to the first window, kill the program, and issue a killall foo (or whatever is the equivalent on your system). Watch the output of the bars.

When reaching the maximum amount of processes set by ulimit, fork() blocks till another slot becomes available.

Replies are listed 'Best First'.
Re^3: Ulimit makes program hang (bug?)
by tye (Sage) on Aug 13, 2008 at 20:11 UTC

    So it isn't a bug in Perl but a change in behavior based on operating system (or some other environmental detail)? My prior experience shows fork failing due to ulimit and other examples in the thread show this behavior as well. Thanks for the insight.

    - tye        

      I don't think it's on the system level, but it's perl that retries. I don't know exactly which functions Perl executes on backticks, but it's probably going to open some pipe. And here's a snippet from the function Perl_my_popen in util.c:
      while ((pid = PerlProc_fork()) < 0) { if (errno != EAGAIN) { PerlLIO_close(p[This]); PerlLIO_close(p[that]); if (did_pipes) { PerlLIO_close(pp[0]); PerlLIO_close(pp[1]); } if (!doexec) Perl_croak(aTHX_ "Can't fork"); return NULL; } sleep(5); }
      As you can see, if the fork fails with an EAGAIN, perl sleeps for 5 seconds and tries again.

      A similar loop is found in Perl_my_popen_list in the same file.

        I also think this is perl who retries to fork. I think perl should return control back to user saying "Hey! I wasn't able to fork now -- you decide what to do next...".

        If admin set ulimit to prevent users from wasting system resources, then perl should fail if restrictions are broken. Perl should not wait until administrator makes limits bigger.

        --
        memo2005
        http://tornado.99k.org/telekinetics.htm