Ah, great idea! That worked. I had to add some kludginess to get CTRL-C to kill the grandchild process (and for some reason an INT signal wasn't enough, so I had to use KILL), but here's a final working version.

#!perl -w use strict; use IO::Select; use Socket; use POSIX; use FileHandle; socketpair(SOCK_READ,SOCK_WRITE,AF_UNIX,SOCK_STREAM,PF_UNSPEC) or die "Couldn't create socket pair: $!\n"; binmode SOCK_READ, ':crlf'; binmode SOCK_WRITE, ':crlf'; use vars qw($grandchild_pid); $SIG{INT} = sub { # For some reason, an INT signal doesn't do anything... kill 9, $grandchild_pid if ($grandchild_pid); exit(2); }; print "STDOUT print 1.\n"; $grandchild_pid = open(CMD,'perl -e "$|=1; while(1) { sleep(5); warn q +q:boing\n:; print q:The time is now :,time,qq:\n:; };"|') or die "Couldn't run perl: $!\n"; if (!defined(my $f = fork())) { die "Fork error: $!\n"; } elsif (!$f) { # child close(SOCK_READ); autoflush SOCK_WRITE 1; print SOCK_WRITE while (<CMD>); close(CMD); exit($? & 255 ? 255 : $?>>8 ); } # parent close(SOCK_WRITE); warn "Parent is PID $$\n"; # We can't reliably print to STDOUT after this point (at some point, # it will start going to the socket) #sleep(5); print "STDOUT print 2.\n"; my $sel = IO::Select->new; $sel->add(\*SOCK_READ) or die "Couldn't add pipe to IO::Select: $!\n"; print "STDOUT print 3.\n"; while (1) { print "STDOUT print 4.\n"; warn "DEBUG: select on ",$sel->count," handles.\n"; my @ready = $sel->can_read(10); warn "Parent DEBUG: ",scalar(@ready)," handles readable.\n"; print "STDOUT print 5.\n"; foreach my $rh (@ready) { my $line=<$rh>; warn "Parent read $rh: $line"; } sleep(3); }

In reply to Re: Re: Re: Re: Windows weirdness after fork, dup2 by sgifford
in thread Windows weirdness after fork, dup2 by sgifford

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.