I have a process that I would like to fork at least three times. Twice to launch little helper apps on my PC and another to do some tiddly stuff with another running application.
I have zero problems creating three seperate scripts to do what I want them to do. But I want to merge the code from all three into one single script. So I figured a fork would work nicely. Problem is, all the examples I've seen out there show how to Fork only once. For example:
FORK: { if ($pid = fork) { #Parent here. #Child process pid is available in $pid } elsif (defined $pid) { #pid is zero here if defined #child here #parent process pid is available with getppid (Not available wit +h Windows). } elsif ($! == EAGAIN) { #EAGAIN is supposedly recoverable fork error sleep 5; redo FORK; } else { #Weird fork error die "Can't fork: $!\n"; } }

No problem with two things. But how do I get three going? So I looked around and figured maybe I should create a pipe the forked children and tell it what I want it to do (two of the processes launch help apps, so they use the same generic code). So I looked in the camel and located the following snippet:

if (open(TO, "|-")) { print TO $fromparent; } else { $tochild = <STDIN>; #Do whatever with $child exit; };
OK. So, put my pieces together, run script and it dies with, "Bad command or file name."
That's nice, after a bit of testing, I figure out that Windows thinks I want a file named |-.
Now I'm stuck. Did a search for |- and only one node came up. Not what I was looking for.

So then I tried "Pipe Child" and "Pipe STDIN" and couldn't discover the little nugget.
I did however, find a hint, "pipe," and I think I can utilize this to communicate to my children. Problem is, I'm not sure how this is done. The format follows something like, pipe READHANDLE,WRITEHANDLE. I figure I could use the $pid of the child, but what do I used for the WRITEHANDLE?

Again, the Camel has another example on how to use Pipe to communicate with children. I type the code example in the book to open a pipe to my child:

pipe(FROM_PARENT, TO_CHILD) or die "pipe: $!"; select((select(TO_CHILD), $| = 1))[0]);
And the Perl script dies with a "Not enough arguments for select system call" along with a syntax error.... grrr.....
I've examined this code and compared it to the book character and character. I've left the entire script out, but the code is exactly as it's presented to me in the Camel.

I can't believe that Pipe is a UNIX only function. I swear on my left nut, I've used it in Windows environment before. But if Pipe is a UNIX only function, what is the comparable Windows call? How can I communicate to my children? Besides gathering the family together for dinner.

----
Thanks for your patience.
Prove your knowledge @ HLPD


In reply to open(TO, "|-") is a bad file name? by SavannahLion

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.