alarm($timeout) timing out only one iteration - Solaris9

Discussion in 'Perl Misc' started by peco, May 13, 2005.

  1. peco

    peco Guest

    Hello,
    I have the following code:
    eval
    {
    local $SIG{ALRM} = sub { die ;};
    alarm(3);
    while (<CMD>)
    {
    $oldvar= $_;
    }
    alarm(0);
    };

    CMD is an open pipe that ssh-es to a list of servers and retrieves a
    value. If ssh connection takes too long the intention is to kill it and
    proceed with the rest on the list. The problem is that alarm works only
    on the first slow connection. Subsequent iterations that take longer
    than 3 seconds do not time out. I have found several posts regarding
    this but nothing has worked so far. I understand this is architecture
    dependant implementation but so far I am unable to find the right
    incantation for Solaris 9.

    Thanks!
     
    peco, May 13, 2005
    #1
    1. Advertising

  2. In article <>,
    peco <> wrote:
    >Hello,
    >I have the following code:
    >eval
    >{
    > local $SIG{ALRM} = sub { die ;};
    > alarm(3);
    > while (<CMD>)
    > {
    > $oldvar= $_;
    > }
    > alarm(0);
    >};
    >
    >CMD is an open pipe that ssh-es to a list of servers and retrieves a
    >value. If ssh connection takes too long the intention is to kill it and
    >proceed with the rest on the list. The problem is that alarm works only
    >on the first slow connection. Subsequent iterations that take longer
    >than 3 seconds do not time out. I have found several posts regarding
    >this but nothing has worked so far. I understand this is architecture
    >dependant implementation but so far I am unable to find the right
    >incantation for Solaris 9.
    >


    More code needed to see what's going on... show how
    the pipe is actually opened for instance.

    Just a guess really but is the alarm(3) being reset
    somewhere after there's a timeout...

    --
    Charles DeRykus
     
    Charles DeRykus, May 13, 2005
    #2
    1. Advertising

  3. peco

    Anno Siegel Guest

    peco <> wrote in comp.lang.perl.misc:
    > Hello,
    > I have the following code:
    > eval
    > {
    > local $SIG{ALRM} = sub { die ;};
    > alarm(3);
    > while (<CMD>)
    > {
    > $oldvar= $_;
    > }
    > alarm(0);
    > };
    >
    > CMD is an open pipe that ssh-es to a list of servers and retrieves a
    > value. If ssh connection takes too long the intention is to kill it and
    > proceed with the rest on the list. The problem is that alarm works only
    > on the first slow connection. Subsequent iterations that take longer
    > than 3 seconds do not time out.


    The alarm you set times out exactly once, breaking the while loop.
    It can't time out more than one event.

    Your description of what CMD does is more than vague. It would have
    been your job to develop some demo job that everyone can run and watch
    the result.

    Assuming that it prints exactly one line for every "step", moving
    alarm( 3) into the loop would help. As in

    my $prog = <<'EOP';
    $| = 1;
    for ( 1 .. 10 ) {
    my $t = 1 + int rand( 5);
    print "see ya in $t secs\n";
    sleep $t;
    }
    EOP
    open CMD, '-|', "perl -e '$cmd'";

    eval
    {
    local $SIG{ALRM} = sub { die ;};
    while (<CMD>)
    {
    alarm(3);
    print;
    }
    alarm(0);
    };

    This times out the first time CMD fails to deliver the next line
    within 3 seconds. Is that what you want?

    Anno
     
    Anno Siegel, May 13, 2005
    #3
  4. peco

    Brian Wakem Guest

    Anno Siegel wrote:

    > peco <> wrote in comp.lang.perl.misc:
    >> Hello,
    >> I have the following code:
    >> eval
    >> {
    >> local $SIG{ALRM} = sub { die ;};
    >> alarm(3);
    >> while (<CMD>)
    >> {
    >> $oldvar= $_;
    >> }
    >> alarm(0);
    >> };
    >>
    >> CMD is an open pipe that ssh-es to a list of servers and retrieves a
    >> value. If ssh connection takes too long the intention is to kill it and
    >> proceed with the rest on the list. The problem is that alarm works only
    >> on the first slow connection. Subsequent iterations that take longer
    >> than 3 seconds do not time out.

    >
    > The alarm you set times out exactly once, breaking the while loop.
    > It can't time out more than one event.
    >
    > Your description of what CMD does is more than vague. It would have
    > been your job to develop some demo job that everyone can run and watch
    > the result.
    >
    > Assuming that it prints exactly one line for every "step", moving
    > alarm( 3) into the loop would help. As in
    >
    > my $prog = <<'EOP';
    > $| = 1;
    > for ( 1 .. 10 ) {
    > my $t = 1 + int rand( 5);
    > print "see ya in $t secs\n";
    > sleep $t;
    > }
    > EOP
    > open CMD, '-|', "perl -e '$cmd'";



    Global symbol "$cmd" requires explicit package name at....

    I pressume that should be $prog.


    --
    Brian Wakem
     
    Brian Wakem, May 13, 2005
    #4
  5. peco

    Anno Siegel Guest

    Brian Wakem <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:


    > > my $prog = <<'EOP';
    > > $| = 1;
    > > for ( 1 .. 10 ) {
    > > my $t = 1 + int rand( 5);
    > > print "see ya in $t secs\n";
    > > sleep $t;
    > > }
    > > EOP
    > > open CMD, '-|', "perl -e '$cmd'";

    >
    >
    > Global symbol "$cmd" requires explicit package name at....
    >
    > I pressume that should be $prog.


    Yes, thanks.

    Anno
     
    Anno Siegel, May 13, 2005
    #5
  6. peco

    peco Guest

    Thank you for your help. Here is the entire routine:

    foreach $host (list_hosts())
    {
    my $oldpass="";
    my $cmd = "su - user000 -c \".
    /user/home/.ssh-agent-serverXYZ;ssh $host cat /etc/passwd\"";
    my $pid = open(CMD, "$cmd |");
    print "Process ID of open pipe - $pid\n";
    eval
    {
    local $SIG{ALRM} = sub { print "TIMEOUT after $timeout
    seconds\n";die ;};
    alarm(3);
    while (<CMD>)
    {
    $oldpass= $_;
    }
    alarm(0);
    };
    kill 9,$pid;
    close(CMD);

    $oldpass =~ s/\n//;
    print $host."---".$oldpass."\n";

    }

    Thanks again
     
    peco, May 13, 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. Ishwar Rattan

    timeout using alarm signal question??

    Ishwar Rattan, Jul 22, 2004, in forum: Python
    Replies:
    0
    Views:
    376
    Ishwar Rattan
    Jul 22, 2004
  2. Ishwar Rattan

    timeout on on socket using alarm signal??

    Ishwar Rattan, Jul 22, 2004, in forum: Python
    Replies:
    0
    Views:
    638
    Ishwar Rattan
    Jul 22, 2004
  3. Micha³ Kurowski

    IO::Socket on Solaris9

    Micha³ Kurowski, Apr 1, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    138
    Micha³ Kurowski
    Apr 1, 2004
  4. Prabh
    Replies:
    2
    Views:
    109
    Charles DeRykus
    Apr 25, 2004
  5. David Combs
    Replies:
    2
    Views:
    104
    Ben Morrow
    Jul 20, 2004
Loading...

Share This Page