in reply to Ulimit makes program hang

Try putting a sleep in before shelling out - that way the scheduler should get time... e.g.
#!/usr/bin/perl -w print STDERR "$$\n"; sleep 1; `./my_program`;
HTH ,

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: Ulimit makes program hang
by memo2005 (Initiate) on Aug 13, 2008 at 14:29 UTC
    Unfortunatelly sleeping doesn't solve the problem.

    I have tested all more throughly and modified program:
    #!/usr/bin/perl -w my $x = $ARGV[0] + 1; print STDERR "$x\n"; `./my_program $x`;

    Now I ran commands:
    (ulimit -u100; ./my_program) (ulimit -u50; ./my_program) (ulimit -u20; ./my_program)

    The first one hang on 95-th iteration. The second hang on 45-th iteration. The third one hang on 15-th iteration. So wen you set maximum number or processes to N (ulimit -u<N>), then program hangs on N-5 iteration.
    I want that program to fail (return nonzero code) when no processes are available...

      ulimit sets the maximum number of process that your user can run. Since you are likely to be running some other processes (for instance, your shell), it comes as no surprise that there are fewer "slots" available for other processes.

      --
      David Serrano

        Yes -- you're right. My question is why perl hangs instead of giving up and telling user "unable to fork".
      Hi ,

      I strongly suspect that, if you re-insert the sleep before shelling out, your results will be modified since the perl process won't otherwise have enough time to complete the print.

      Have you tried the fork/exec approach ?

      Have you tried an even lower limit and running the program using truss (or is it strace on linux) ?

      As an afterthought, I do wonder if you've uncovered a bug in perl...

      A user level that continues to overstate my experience :-))
        After insterting sleep before shelling out I get exactly same results.

        I'm not a system programmer to analyse strace output...sorry.

        I have tried fork and exec this way:
        #!/usr/bin/perl -w my $x = $ARGV[0] + 1; print STDERR "$x\n"; if (!defined($child_pid = fork())) { die "cannot fork: $!"; } elsif ($child_pid) { waitpid($child_pid, 0); } else { exec "$0 $x"; }
        This program never hangs -- always says cannot fork: Resource temporarily unavailable at ./fork_wait line 7.
        But I must grab output of command so I can't use this solution.