develooper Front page | perl.perl5.porters | Postings from May 2008

Re: [perl #54644] Perl script hang at while statement

Thread Previous | Thread Next
From:
Yitzchak Scott-Thoennes
Date:
May 22, 2008 23:18
Subject:
Re: [perl #54644] Perl script hang at while statement
Message ID:
47256.71.37.21.153.1211523484.squirrel@webmail.efn.org
On Thu, May 22, 2008 6:11 am, Chaitanya Vutukuru wrote:
> And also please let me know, if these is not the correct correct/proper
> channel for support.

This is the proper place for perl bug reports, but not general support
of perl.  For help with perl programming, try http://perlmonks.org.

> Here is the  brief explanation  of the perl script and the problem
> we are facing:
> In the script we create pipe like:
> pipe(FROMDBREAD,FROMDBWRITE);
>
> we dup STDOUT and STDERR, to FROMDBWRITE pipe descriptor like:
> open(STDOUT,">&FROMDBWRITE") || die "$ME (child): can't redirect STDOUT\n";
> open(STDERR,">&STDOUT")  || die "$ME (child): can't redirect STDERR\n";
>
> Then we will wait for the pipe descriptor in the while loop:
> while(<FROMDBREAD>) {
> ..
> ..
> }
>
> We know that few messages are loggedin STDOUT and STDERR, but we  see
> that the script is hanging in the above while statement.
> We  are telling  it as  a hang  because its  not coming  out of  that
> while(for false) or even not  running the statements under that  while
> (for  true), it  is just  waiting at  "while(<FROMDBREAD>)", which is
> leading to the whole script hanging.
> We  and  customer   facing  this issue   in  one  particular  scenario
> and in  some machines.

I believe you are not understanding how pipes work; you may want to
unless you are in non-blocking mode, reading on a pipe will wait for
something to be written if there are any open write filehandles on
the pipe.

See http://www.opengroup.org/onlinepubs/009695399/functions/read.html
and/or try the following:

#!/usr/bin/perl
use strict;
use warnings;
my $ME = "me";

open OUT, ">&STDOUT" or die "couldn't dup\n";

pipe(FROMDBREAD,FROMDBWRITE);

#we dup STDOUT and STDERR, to FROMDBWRITE pipe descriptor like:
open(STDOUT,">&FROMDBWRITE") || die "$ME (child): can't redirect STDOUT\n";
open(STDERR,">&STDOUT")  || die "$ME (child): can't redirect STDERR\n";

print OUT "warning foo\n";
warn "foo";
print OUT "warned foo\n";

$SIG{ALRM} = sub { close FROMDBWRITE; close STDOUT; close STDERR; print
OUT "closed\n" };
alarm 5;

#Then we will wait for the pipe descriptor in the while loop:
while(<FROMDBREAD>) {
   print OUT "got :$_:\n";
}

print OUT "done\n";





Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About