Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem in Executing system command in a forking server

Reply
Thread Tools

Problem in Executing system command in a forking server

 
 
vikrant
Guest
Posts: n/a
 
      09-22-2005
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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      09-22-2005
vikrant <> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
 
Damian James
Guest
Posts: n/a
 
      09-22-2005
On Thu, 22 Sep 2005 11:38:23 +0530, vikrant said:
> 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
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      09-22-2005
vikrant <> wrote:
> 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 $!");

> exit;
> }
>
> -------------------------------------------------------------------------
> ----------------------------
>
> Thanks
>
> Vikrant


--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      09-23-2005

Anno Siegel wrote:
> vikrant <> wrote in comp.lang.perl.misc:
> > 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..."
>


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

--
Charles DeRykus

 
Reply With Quote
 
vikrant.kansal@gmail.com
Guest
Posts: n/a
 
      09-26-2005
hi

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

wrote:

> > How do you know that?


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

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

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".


> >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 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
executing a system command and stopping it after a specified duration? Robert La Ferla Ruby 8 04-16-2007 06:35 PM
Newbie: executing a system command from my Python app Dan M Python 5 12-14-2005 05:20 PM
Perl system command not executing CoralBanded Perl Misc 6 01-08-2004 03:02 AM
Forking server SRam Perl 1 08-21-2003 05:15 PM
os.system('cd dir1 ... and executing next os.system command in that directory (dir1) Irmen de Jong Python 2 08-12-2003 12:43 PM



Advertisments