in reply to Windows weirdness after fork, dup2

Instead of dup2(), use this
open STDOUT, ">&SOCK_WRITE" or die "STDOUT: $!\n";
The "fork" in ActivePerl is not a real fork, it just creates a separate thread. I think by using dup2() you are bypassing the logic that makes them work like separate processes with separate file descriptors.

Replies are listed 'Best First'.
Re: Re: Windows weirdness after fork, dup2
by sgifford (Prior) on Mar 05, 2004 at 20:23 UTC
    Thanks Thelonius, but no luck. If I use:
    open STDOUT, ">&SOCK_WRITE" or die "STDOUT: $!\n"; warn "fileno(STDOUT)=",fileno(STDOUT),"\n";
    I find that it's not using the right file descriptor:
    fileno(STDOUT)=3
    so when I exec a new process it doesn't have the file handles hooked up right. For example, if I change the infinite sleep loop to actually run something:
    exec('perl','-e','$|=1; while(1) { sleep(5); print q:The time is now : +,time,qq:\n:; };') or die "exec error: $!\n";
    the output from the child goes to the console, not the socket. With dup2, it goes to the socket.
      What you can do is set up the child to forward the output of the command to the main parent through the socket:
      # child close(SOCK_READ); open CMD, "ipconfig|" or die "ipconfig: $!\n"; print SOCK_WRITE while <CMD>; exit(0);
        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.