#!perl -w use strict; use IO::Select; use Socket; use POSIX; socketpair(SOCK_READ,SOCK_WRITE,AF_UNIX,SOCK_STREAM,PF_UNSPEC) or die "Couldn't create socket pair: $!\n"; print "STDOUT print 1.\n"; if (!defined(my $f = fork())) { die "Fork error: $!\n"; } elsif (!$f) { # child close(SOCK_READ); warn "CHILD PID: $$\n"; close(STDOUT) or die "Couldn't close STDOUT: $!\n"; POSIX::dup2(fileno(SOCK_WRITE),1); sleep(100) while(1); } # 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); }