Resend: RESOLVED: Question about pipes and exit codes

Walter Pienciak walter at frii.com
Wed Nov 24 11:05:56 CST 1999


From: William Atkinson <CWA at DISC.com>
To: "'boulder-pm-list at happyfunball.pm.org'"
	 <boulder-pm-list at happyfunball.pm.org>
Subject: RESOLVED: Question about pipes and exit codes
Date: Wed, 17 Nov 1999 10:33:11 -0700
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.5.2650.10)
Content-Type: text/plain;
	charset="iso-8859-1"

Greetings,

I think I found a solution to the problem.  On page 591 and 592 of the Perl
Cookbook, there is information about installing a signal handler for signal
CHLD.  I get the pid from open2 and save it in a global variable.  When the
process dies, the signal handler is called.  Currently I'm using the handler
as described on p591:

sub REAPER {
   my $stiff;
   print "In reaper, PID: $pid\n";
   while (($stiff = waitpid ($pid, &WNOHANG)) > 0) {
      print "Returned value from REAPER: \$?: $?\n";
   }
   print "Returned value from REAPER: \$?: $?\n";

   $SIG{CHLD} = \&REAPER;
}

The only mods are the print statements.  The print within the while returns
the correct signal value.  The second print returns a bogus value, probably
reflecting the status of the first print.

Thanks for listening.

Chip

> -----Original Message-----
> From: William Atkinson [mailto:CWA at DISC.com]
> Sent: Wednesday, November 17, 1999 9:24 AM
> To: 'boulder-pm-list at happyfunball.pm.org'
> Subject: RE: Question about pipes and exit codes
> 
> 
> Jason,
> 
> Thanks for the suggestion and response.
> I originally tried open2 (haven't done open3 yet) but only 
> see sigpipe when
> the program at the "far" end of the pipe dies, regardless of 
> the cause of
> death.  I started on chapter 16 of the cookbook this morning 
> and am at p.
> 562, so I'd say we are definitely in the same ballpark.  I'd 
> also say I'm in
> a bit deep too.  
> 
> The next thing that I'm going to try is fussing with pipe and 
> fork to get a
> sort of pipe loop.  If I can do that successfully, I can 
> catch the signal
> that the child died from.
> 
> Chip
> 
> > -----Original Message-----
> > From: Jason Van Slyke [mailto:jvanslyk at matchlogic.com]
> > Sent: Wednesday, November 17, 1999 9:13 AM
> > To: 'boulder-pm-list at happyfunball.pm.org'
> > Subject: RE: Question about pipes and exit codes
> > 
> > 
> > Chip,
> > 
> > First up, you're way over my head.  But I found this 
> > interesting so I dug
> > out a couple, 3 or 4 books.
> > 
> > Do you have a Perl Cookbook available?  If so, see p567-571.
> > 
> > If not, this _might_ help:
> > 
> > eval {
> > 	open2($readme, $writeme, @program_and_arguments) ;
> > } ;
> > if ($@) {
> > 	if ($@ =~ /^open2/) {
> > 		warn "open2 failed: $!\n$@\n" ;
> > 		return ;
> > 	}
> > 	die ;		#reraise unforeseen exception
> > }
> > 
> > Let me know if this is playing in the same ball park as you are.
> > 
> > Jason
> > 
> > -----Original Message-----
> > From: William Atkinson [mailto:CWA at DISC.com]
> > Sent: Wednesday, November 17, 1999 8:46 AM
> > To: 'boulder-pm-list at pm.org'
> > Subject: Question about pipes and exit codes
> > 
> > 
> > Greetings,
> > 
> > Forgive and politely correct me if this is the wrong list 
> to post to.
> > 
> > I'm having trouble getting the exit status of a process at 
> > the end of a pipe
> > created by open2.  It seems that all I get is notification 
> > that the pipe
> > broke.  
> > 
> > One idea that we came up with was to have a shell script run 
> > the desired
> > program, catch its exit status and return it as the last 
> string.  The
> > problem with this is that I was hoping for a purely perl 
> > based solution due
> > to cross platform portability issues.
> > 
> > Any help would be appreciated.
> > 
> > Thanks.
> > 
> > Chip Atkinson
> > chip at disc.com
> > 
> 




More information about the Boulder-pm mailing list