in reply to system command - OK on 32 bit, fails on 64 bit Linux - why?

In addition, when doing this type of tests, please always set in the beginning of your program

$|=1;
and of course use strict; use warnings;.

(Note: Typo in Perl code corrected)
-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re^2: system command - OK on 32 bit, fails on 64 bit Linux - why?
by ikegami (Patriarch) on Feb 17, 2010 at 13:41 UTC

    I don't see the point of initialising $!.

    • Functions are free to change $! even if no error occurred, and do so. Whatever value you set won't stay there.

      $ echo foo | perl -le'$!=1; print 0+$!; <>; print 0+$!' 1 9
    • $! is meaningless unless a function notified you that it set $!. You can't read $! and compare it against the value you previously gave it. You would think the readline in the above example had failed.

    • die will use $! if it's non-zero, but it will fall back to $?>>8 then to 255. If anything, that means $!=0; would be the best value with which to initialise $!.

Re^2: system command - OK on 32 bit, fails on 64 bit Linux - why?
by afoken (Chancellor) on Feb 17, 2010 at 13:19 UTC
    [...] please always set in the beginning of your program $!=1;

    Why? $|=1 would make sense, but $! is just the last error.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Hi guys and thanks,

      I've tried your suggestions and still have the problem. Am now using use strict; use warnings; $!=1;

      The return codes with $? >> 8, $? & 127 and $? & 128 are always 0.
      I get error messages running date via backticks and open(), which seems to indicate that there is no result from the command. (See below).

      BUT - if I reduce the array size then all runs OK!
      Reducing the array to about 5881 x 2562 then my demo script runs OK.
      2562 is not 100% repeatable - seems to depend on what other programs are running.
      Also - it isn't symmetrical. This means that 5881 x 2562 may work, but then 2562 x 5881 doesn't.

      Further suggestions welcomed.

      Cheers, Peter

      Bad results snippet:

      #Finished setup of two arrays 5761 x 2801 #After - using system("date");: #exited with value ($? >> 8) = 0 # #signal number ($? & 127) = 0 # #dumped core ($? & 128) = 0 Use of uninitialized value $cmd in print at ./xpt3.pl line 67. #After - using backticks: readline() on closed filehandle CMD at ./xpt +3.pl line 71. Use of uninitialized value $result in concatenation (.) or string at . +/xpt3.pl line 72. #After - using open: #End
      Good results snippet:
      #Finished setup of two arrays 5761 x 2001 #After - using system("date");: Wed Feb 17 13:13:24 GMT 2010 #exited with value ($? >> 8) = 0 # Wed Feb 17 13:13:24 GMT 2010 #signal number ($? & 127) = 0 # Wed Feb 17 13:13:24 GMT 2010 #dumped core ($? & 128) = 0 #After - using backticks: Wed Feb 17 13:13:24 GMT 2010 #After - using open: Wed Feb 17 13:13:24 GMT 2010 #End

      Code snippet:

      print "#Finished setup of two arrays $i x $j\n"; #Using system() print '#After - using system("date");: '; system("date"); if ( $? == -1 ) { print " #command failed: $!\n"; } else { print " #exited with value (\$? >> 8) = ",($? >> 8),"\n"; } print "# "; system("date"); if ( $? == -1 ) { print " #command failed: $!\n"; } else { print " #signal number (\$? & 127) = ",($? & 127),"\n"; } print "# "; system("date"); if ( $? == -1 ) { print " #command failed: $!\n"; } else { print " #dumped core (\$? & 128) = ",($? & 128),"\n"; } #Using backticks $cmd = `date 2>&1`; print "#After - using backticks: ",$cmd; #Using open() open(CMD, "date 2>&1 |"); $result = <CMD>; print "#After - using open: $result\n"; close(CMD); print "\n#End\n"; exit;
        The array sizes you have imply a total allocation of around 2Gb on a 32-bit system, and around 2.5Gb on a 64-bit system. This might be some magical number that is causing the fork to fail. How much virtual memory do the two servers have? Try running the code under strace, and see if there are any interesting system call errors:
        $ strace -f -o /tmp/strace.out perl foo.pl $ grep '= -1 E' /tmp/strace.out
        Also, for debugging purposes, please just call date the one time and print the full unadulterated value of $?, rather than three times with three separate snippets, e.g.
        system("date"); printf "\$?=0x%x \$!=%s\n", $?, $!;

        Dave.

      Sorry, a typo. I meant $|.

      -- 
      Ronald Fischer <ynnor@mm.st>