On May 12, 11:21 pm, [email protected] (robert Key) wrote:
> Hi,
> I want to capture UNBUFFERED output from a C programme and then
> control it depending on its output. Problem is nothing seems to
> unbufferd the output from the C programme. Only when the child has
> finished executing do I get all the output which is too late.
>
> The C programe is just like hello world in a loop of 20.
>
> Here is the code:
> #!/usr/bin/perl -w
> use IO::Handle;
> sub killGroup {
> # kill process group except self
> local $SIG{HUP} = 'IGNORE';
> kill (HUP, -$$);
> print "killed child exit status $?\n";
>
> }
>
> if ($pid = open(READER, "-|")) {
> # parent process
> print "parent: starting\n";
> while (<READER>) {
> print "parent: $_";
> killGroup if /3/;
> }
> close (READER);} else {
>
> # child process
> die "cannot fork: $!" unless defined $pid;
> $SIG{HUP} = sub {die "child: killed by parent\n"};
>
> # try and un buffer output
> STDOUT->autoflush(1);
> print STDOUT "child: starting\n";
>
> # start the C programme which has printf in a loop
> exec("/home/robert/try8 2>&1");
> exit 0;}
>
> I have tried all the recipes int the Perl cook book but none work.
Could you simplify the perl a bit and use Inline::C to get around
the pipe buffering...?
example:
#!/usr/bin/perl
use strict;
use warnings;
use Inline C => 'DATA';
run_c();
__DATA__
__C__
#include <stdio.h>
void run_c() {
int i;
char s[20] = "hello from c";
for (i = 0; i < 15; i++) {
sleep(1);
printf("%s %d\n", s, i);
}
}
--
Charles DeRykus
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/