Problem in Executing system command in a forking server

V

vikrant

hi

I am trying to store the return value of system command after execution in a forking server.

The command executes successfully but returns "-1". In case of failure, it returns the same value
"-1", like when i am replacing "date" with "data" in system command. From my understanding, it
should return -1 only in case of failure. How do go about distinguishing between a success and
failure call while using the system commnad?

Code:-
-----------------------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use IO::Select;

$SIG{CHLD} = 'IGNORE';

my $sock = new IO::Socket::INET(
LocalHost => '10.0.0.23',
LocalPort => '36545',
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket :$!";

REQUEST:
while (my $sNew_Socket = $sock->accept())
{
my $kid=fork();
if ($kid)
{
close $sNew_Socket;
next REQUEST;
}

close $sock;
my $obSelected_Socket = IO::Select->new($sNew_Socket);
my $sData_Recevied;
while( $obSelected_Socket->can_read(20))
{
my $sBuffer;
sysread($sNew_Socket,$sBuffer,1<<10);
$sData_Recevied.=$sBuffer;
if($sBuffer =~/\/END>/)
{
last;
}
}
my $sDatecmd=system("date");
$sNew_Socket->send($sDatecmd);
exit;
}

-----------------------------------------------------------------------------------------------------


Thanks

Vikrant
 
A

Anno Siegel

vikrant said:
hi

I am trying to store the return value of system command after execution
in a forking server.

The command executes successfully

How do you know that?
but returns "-1". In case of failure,
it returns the same value
"-1", like when i am replacing "date" with "data" in system command.

Is "date" on the search path of the process that's trying to execute
it?
From my understanding, it
should return -1 only in case of failure. How do go about distinguishing
between a success and
failure call while using the system commnad?

See perldoc -f system. Note the third paragraph beginning with "The
return value is..."

Anno
 
D

Damian James

I am trying to store the return value of system command after execution in a forking server.

The command executes successfully but returns "-1". In case of failure, it returns the same value
"-1", like when i am replacing "date" with "data" in system command. From my understanding, it
should return -1 only in case of failure. How do go about distinguishing between a success and
failure call while using the system commnad?

Well, do you know what it returns on success?

perl -e 'printf "[%d]\n", system "date"'
...
my $sDatecmd=system("date");
$sNew_Socket->send($sDatecmd);

How about:

$sNew_Socket->send($sDatecmd? "Failed" : "Succeded");

Though perhaps you actually wanted the *output* of the date
command? See perldoc -q "output of a command".

You might also care to try the builtins instead:

perl -le 'print scalar localtime'

So the above would be

my $date = scalar localtime;
$sNew_Socket->send($date);

--Damian
 
X

xhoster

vikrant said:
hi

I am trying to store the return value of system command after execution
in a forking server.

The command executes successfully but returns "-1".

How do you know?
In case of failure,
it returns the same value "-1", like when i am replacing "date" with
"data" in system command. From my understanding, it should return -1 only
in case of failure. How do go about distinguishing between a success and
failure call while using the system commnad?

Maybe, if Perl is telling you that it fails, you should ask Perl why it
fails.

Code:-
-------------------------------------------------------------------------
---------- #!/usr/bin/perl -w
use strict;
use IO::Socket;
use IO::Select;

$SIG{CHLD} = 'IGNORE';

After asking Perl why it fails, I've decided this is your problem.

....
my $sDatecmd=system("date");
$sNew_Socket->send($sDatecmd);

$sNew_Socket->send("$sDatecmd $!");
 
C

ced

Anno said:
How do you know that?


Is "date" on the search path of the process that's trying to execute
it?


See perldoc -f system. Note the third paragraph beginning with "The
return value is..."

I suspect the most likely scenario is that "date" succeeded but
that wait returned -1 because of the auto-reaping of "IGNORE".
From perlipc:
...
Setting "$SIG{CHLD}" to "'IGNORE'" on such a platform has
the effect of not creating zombie processes when the parent
process fails to "wait()" on its child processes (i.e. child
processes are automatically reaped). Calling "wait()" with
"$SIG{CHLD}" set to "'IGNORE'" usually returns "-1" ...

perl -le '$SIG{CHLD}="IGNORE";print system "date"'
Thu Sep 22 20:06:29 PDT 2005
-1
 
V

vikrant.kansal

hi

Due to some problem i could not connect to news group thats why i am
replying using google news group.


when i am running my script on terminal
perl server.pl
It shows the following output on the terminal after accepting the
client request.
Mon Sep 26 11:49:16 IST 2005
as u told i go through it and able to understand what exactly
happened,same as Charles DeRykus said to me .
Calling "wait()" with "$SIG{CHLD}" set to "'IGNORE'" usually returns
"-1" that's what i got through send function
$sNew_Socket->send($sDatecmd);
but if i comment out the $SIG{CHLD}='IGNORE' then zombies are created.
Also, date is not just i what to run . i am using it here just to test
the system command output in forking server.
I suspect the most likely scenario is that "date" succeeded but
that wait returned -1 because of the auto-reaping of "IGNORE".
...
Setting "$SIG{CHLD}" to "'IGNORE'" on such a platform has
the effect of not creating zombie processes when the parent
process fails to "wait()" on its child processes (i.e. child
processes are automaticaly reaped).

perl -le '$SIG{CHLD}="IGNORE";print system "date"'
Thu Sep 22 20:06:29 PDT 2005
-1
so,please suggest me how do i get the system command output in forking
server.

Thanks
vikrant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top