Problem in Executing system command in a forking server

Discussion in 'Perl Misc' started by vikrant, Sep 22, 2005.

  1. vikrant

    vikrant Guest

    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
    vikrant, Sep 22, 2005
    #1
    1. Advertising

  2. vikrant

    Anno Siegel Guest

    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.
    Anno Siegel, Sep 22, 2005
    #2
    1. Advertising

  3. vikrant

    Damian James Guest

    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
    Damian James, Sep 22, 2005
    #3
  4. vikrant

    Guest

    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
    , Sep 22, 2005
    #4
  5. vikrant

    Guest

    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
    , Sep 23, 2005
    #5
  6. vikrant

    Guest

    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
    , Sep 26, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SRam

    Forking server

    SRam, Aug 21, 2003, in forum: Perl
    Replies:
    1
    Views:
    2,992
  2. Irmen de Jong
    Replies:
    2
    Views:
    426
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Aug 12, 2003
  3. Dan M
    Replies:
    5
    Views:
    442
    Magnus Lycka
    Dec 14, 2005
  4. Robert La Ferla
    Replies:
    8
    Views:
    137
  5. CoralBanded

    Perl system command not executing

    CoralBanded, Jan 7, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    191
    CoralBanded
    Jan 8, 2004
Loading...

Share This Page