HNY, oh brothers of accumulated wisdom!

I need to control two external programs to accomplish a stream transcoding application. App 1 obtains its input from a URL and writes to STDOUT. It also forks 2-3 children. App 2 reads from STDIN and I read its output and send it on its way.

The good old open(FH,"app1 URL | app2 |"); performs the transcoding, but the catch is that I need to kill the children when the consumer quits drinking from my output. (recall that input is a stream... it doesn't end by itself) close(FH) doesn't seem to kill the kids (or even return, for that matter).

$pid = open2($rdhndl,$wrhndl,"app1 URL | app2"); also transcodes, but the PID returned is that of the sh process running the pipeline and killing that PID doesn't kill the children of App 1 (and indeed doesn't always kill App 1).

system("killall app1"); would work (sort of), but it's ugly, inefficient and doesn't allow for multiple clients of my transcoder.

Combining separate open2() calls hasn't worked. I can't seem to structure the calls so that App 2 reads from App 1's output without intermediation. (OK, I couldn't get it to work at all)

App 2 will die politely when its input disappears, so I really only need to reliably kill the first App 1 process.

What's a coder to do?


In reply to Taming multiple external processes by 87C751

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.