You didn't say what sort of OS your working with, or
what sort of process "program_a" is... Did you look into
something like this:
open( PROC, "| program_a" );
sleep $n;
close PROC;
Or maybe program_a isn't a pipeline sort of process -- you
could try something like the following -- note that
open( PROC, "| command" )
returns the process-id of the
command that is launched. It's not pretty, and not
portable outside of *n*x, but it worked for me (maybe you
need better time resolution on the wait; if so, good luck):
$spid = open( SH, "| /bin/sh" );
select SH; $|++;
print "program_a &\n";
$slept = 0;
until ( $ps =~ m{\b$spid\s+$$\s+/bin/sh\b} &&
$ps =~ m{\b\d+\s+$spid\s+program_a\b} ) {
sleep 1; $slept++;
$ps = `/bin/ps -o pid -o ppid -o comm`;
}
if ($ps =~ m{\b(\d+)\s+$spid\s+sleep\s}) {
$cpid = $1;
sleep ($n-$slept) if ( $n>$slept );
print SH "kill -9 $cpid\n";
} else {
print "Damn! Lost the kid.\n";
}
close SH;
If I put parens around the target
pid in the "until ()" match, $1 shows up empty outside
that block, so I have to do the match again.
|