Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Forks, Pipes and Exec (file descriptors)

by diabelek (Beadle)
on Oct 30, 2008 at 19:05 UTC ( [id://720543]=note: print w/replies, xml ) Need Help??


in reply to Re: Forks, Pipes and Exec (file descriptors)
in thread Forks, Pipes and Exec

Thanks, that seems to have done it. I guess I didn't think of the filenumber changing since the print("foobar") worked before I execed the other process.

almut++

Replies are listed 'Best First'.
Re^3: Forks, Pipes and Exec (file descriptors)
by diabelek (Beadle) on Nov 06, 2008 at 18:01 UTC

    Update...

    This works great in Linux but now I'm trying to get it working in Windows. The problem is that Windows doesn't seem to have the process print to the pipe that's attached to stdout. The only thing I can gather from testing variations is that STDOUT is the same for both "processes" in Windows. If I sleep and let the child run first or don't restore STDOUT, the pipe works as designed. As soon as I restore STDOUT, the pipe is broken even though they are separate processes.

    Is there any way of doing this in Windows?

    print "before the pipe\n"; open $old_stdout, ">&STDOUT" or die "open: $!"; local(*WH); pipe $rh, WH; open STDOUT, ">&WH" or die "open: $!"; STDOUT->blocking(0); # write something to the pipe # and have some fork/exec'ed process write to the pipe (via stdout) if( fork() ) { open( STDOUT, ">&", $old_stdout ) or die "open: $!"; print STDERR "I'm the parent\n"; sleep 3; } else { exec( 'echo fubar' ); exit( 0 ); } close WH; my $out = <$rh>; print "got from pipe: $out"; # $out is empty since fubar printed to +the screen

        If it is a separate thread, is there any way to have STDOUT for thread A be different from thread B?

        From the activestate documentation, the STDOUT filehandle should have been duped for the child thread but I don't believe that's what I'm seeing. I can make a change in the parent to STDOUT that affects the child's STDOUT. Can anyone explain what I'm missing?

        active state documentation http://www.xav.com/perl/lib/Pod/perlfork.html
        "Open filehandles
        Any filehandles open at the time of the fork() will be dup()-ed. Thus, the files can be closed independently in the parent and child, but beware that the dup()-ed handles will still share the same seek pointer. Changing the seek position in the parent will change it in the child and vice-versa. One can avoid this by opening files that need distinct seek pointers separately in the child."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://720543]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found