I need to do a Win32 fork, and the child needs to stay waiting on the parent until the parent signals for the child to exit, then the parent waits until the child exits. I got the code from perlfork. Tried it on 5.12 and 5.17 same result.
use strict; use warnings; my ($child, $parent); pipe($child, $parent) or die; my $pid = fork(); die "fork() failed: $!" unless defined $pid; if ($pid) { close $child; } else { close $parent; print "child waiting\n"; my $read; read($child, $read, 1); print "child exiting\n"; exit(0); } print $parent "exit now\r\n\r"; print "parent going to wait\n"; waitpid($pid, 0);
All I get in console is
parent going to wait child waiting
and then I kill perl.exe since it hung. If I add a "close($parent);" after "print $parent "exit now\r\n\r";" it works. I dont know why. Can someone explain what is happening here?

update: the real purpose of this code is for it to be part of a unit test to make an XS module psuedo-fork safe. Since the object was copied during the fork, when the child psuedo proc exits, the C resource is freed, and using the object in the parent caused a crash. The C resource has its own internal reference count which can be queried in C, so I need to check refcount before the fork, make sure it is 1, do a fork, check refcount, make sure it is 2, then tell the child to exit, when child exists, check refcount, make sure it is 1. If no solution was possible (BrowserUK gave 2), I would have been forced to add Win32::IPC as a build/test dep.

In reply to pipe fork win32 by bulk88

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.