Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

system function is returning status -1 when running a unix command

by elf_firein (Acolyte)
on Apr 08, 2009 at 05:07 UTC ( [id://756229]=perlquestion: print w/replies, xml ) Need Help??

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

I am facing a strange problem with perl code. I have a perl package abc.pm which has following subroutine:
sub DB_xxx { my ($dbUser, $dbPass, $db, $autoCommit) = @_; my $dbh; # logon to DB unless( $dbh = DBI->connect('dbi:Oracle:' . ($$db || ''), $$dbUser +, $$dbPass, {AutoCommit => $$autoCommit}) ) { croak "Error connecting to database $DBI::errstr \n"; } return $dbh; }

This method is for connecting to database

Inside my own .pl file I am using this subroutine in following manner:
use abc qw (DB_xxx); my $dbh; my $stat1 = system ("gzip -t xyz.gz"); print "The stat checking:$stat1\n"; my $userid = xxx; my $passwd = xxx; my $database = xyz; my $autocommit = 0; $dbh=DB_xxx(\$userid, \$passwd, \$database, \$autocommit); my $stat2 = system ("gzip -t xyz.gz"); print "The stat checking:$stat2\n";

The problem is:the value of $stat1 is 0 but value of $stat2 is -1. though the .gz file is same in same location. In Solaris 8 box but in $stat1 & $stat2 have values 0 for both. But in solaris 10 box giving to two differnt output:$stat1=0 & $stat2=-1

I have also printed $!: For $stat2 it is showing error message: / No child process/

Can anybody please help me? Please it is urgent.

Replies are listed 'Best First'.
Re: system function is returning status -1 when running a unix command
by ikegami (Patriarch) on Apr 08, 2009 at 06:01 UTC
    Sounds like your DB stuff is messing with SIGCHLD.
    $ perl -wle'$SIG{CHLD}="IGNORE"; print system("ls -d ."); print $!' . -1 No child processes

    system is basically fork + exec (in the child) + waitpid (in the parent). In the above example, children are auto-reaped, so system has no child to wait for, so it returns an error.

    strace (or truss or whatever it's called on Solaris) would confirm this diagnosis.

    Update: Added last line.

      ikegami, could you please explain what is going on here:
      > perl -wle ' $SIG{CHLD}="IGNORE"; $! = 33; print system("ls -d ."); print -+-$!; print $!; print -+-$!' . -1 33 No child processes 10

      It seems that string-ifying "$!" changes its value.

        print can result in system calls. $! is meaningless after the first print.

        $ perl -wle' $SIG{CHLD}="IGNORE"; $! = 0; my $rv = system("ls -d ."); my $errno = $!; print $rv; print 0+$errno; print $errno; print 0+$errno; ' . -1 10 No child processes 10

        Note that 0+ is much easier to understand than -+-, and it conveys the intent much better.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://756229]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found