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.

In reply to Re: catching a pid by graff
in thread catching a pid by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.