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

EDIT: I was mistaken, this has nothing to do with Perl, see my reply to Anonymous Monk below.

Here's a sample perl code:

system("rm -f x; ulimit -f 5; while [ 1 ]; do echo 1 >>x; done");

If I run this on Redhat EL6 (perl 5.10.1), I get a file that's 2560 bytes long. If I run it on Redhat EL5 (perl 5.8.8), I get a file that's 5120 bytes long.

If I run the command in the shell as opposed to via perl, I get a 5120 byte file on both platforms, on both sh and bash (I do know sh is a symbolic link to bash here, but figured it's worth a try since you can base behaviour on $0). This is consistent with bash ulimit documentation which states that blocks for -f are 1024 bytes long.

The really weird thing is I managed to call the 5.8 perl from RHEL5 on my RHEL6 system and it still made a 2560 byte file, so it's not dependent on the perl configuration.

So I am at a complete loss. I'd like to know:

  1. What causes the different behavior?
  2. How can I avoid it and have perl make a 5120 byte file in all cases?

Thank you very much.

Replies are listed 'Best First'.
Re: Call to ulimit in perl's system() causes unexplainable behavior
by Anonymous Monk on Mar 04, 2014 at 15:04 UTC

    Could this be an XY Problem? I.e. what is the underlying problem are you trying to solve? Why are you using ulimit instead of some other mechanism to limit the size of the file?

    Are you sure that /bin/sh is the same as bash on both systems? For example, try system("/bin/bash","-c", "..."); to make it a little more explicit.

    Have you checked if ulimit succeeds by looking at the exit code and running just ulimit -f?

      The explicit passing of shell did help, thank you. You are absolutely right. I obviously did some mistake with the shell scripting before - I apologize. I re-attempted #!/bin/sh \\ ulimit -f ... and it does write a 2560 byte file on RHEL6. So it's not perl at all, but bash doing different things depending on $0 (/bin/sh does point to bash on both). So I'll fix the code to pass the shell explicitly to system().

      Obvious now, but just to answer your question, ulimit does succeed on both.

      Thank you once again!

Re: Call to ulimit in perl's system() causes unexplainable behavior
by einhverfr (Friar) on Mar 04, 2014 at 15:26 UTC

    I don't think this has anything to do with Perl.

    All of your I/O comes through the shell. You are basically running a shell script to generate the file. There are some things I would look at for possible differences though:

    1. Which shell is invoked by default? Is it the same shell?
    2. Encoding differences. Is the same encoding happening? Do the files look the same in a hex editor?
    3. iconv might show something interesting.

    However what happens beyond within system call is OS dependent, not Perl dependent. There is really very little room for this to be solved as a Perl issue. What I would recommend doing is put the commands in a shell script and invoke that. If it works in the shell script but not from Perl, I would then start checking environment variables, etc.

      You are right, see my other reply above. Thank you!

        Though you marked this issue as resolved solved it might be worth taking a look at POSIX::1003::Limit.

        Update: fixed wrong adjective.

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: Call to ulimit in perl's system() causes unexplainable behavior
by Tux (Canon) on Mar 04, 2014 at 15:08 UTC

    Have a look at BSD::Resource, esp the getrlimit and setrlimit functions with the RLIMIT_FSIZE parameter.


    Enjoy, Have FUN! H.Merijn