Re: Stumped: returning a read pipe from a function



kj <socyl@xxxxxxxxxxxxxxxxx> wrote:

Well, I'm getting close (I think). This is the best I've managed,
though it has at least a few problems:

use Socket;
use IO::Handle;
sub foo {
socketpair( my $child, my $parent, AF_UNIX,
SOCK_STREAM, PF_UNSPEC)
or die $!;

$_->autoflush( 1 ) for $child, $parent;

unless ( my $pid = fork ) {
die $! unless defined $pid;
close $child or die $!;

open STDOUT, '>&', $parent or die $!;

my $status = system "/some/command @_ 2>&1";

exit( $status ? 1 : 0 );
}
close $parent or die;
wait;

This is liable to deadlock. The parent won't start reading until the
child has exited. The child won't exit until the spawned process
has exited. The spawned process won't exit until it is done printing.
And, if what it is printing is more than one buffer full, then it can't
finish printing until the parent starts reading. Round and round you go.



This seems to work, but the system() call requires the shell, due
to the "2>&1" redirection. Also, it's a lot of machinery to do
something that is rather simple, conceptually at least.

I think you are over-estimating the conceptual simplicity of what you
are trying to do.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.



Relevant Pages

  • Re: When [exit] wont exit
    ... the child now does not talk to the parent at all. ... I thought maybe you meant for the parent to pass the child the socket over the pipe or some such. ...
    (comp.lang.tcl)
  • Re: trying to understand fork and wait
    ... It seems to be generally preferred in the Perl ... > child I just spawned. ... the parent process is sent SIGCHLD, which means that perl will stop ... which means that it'll never hang around waiting for one to die. ...
    (comp.lang.perl.misc)
  • Re: asking help for a peer-to-peer socket programming question
    ... Amy ... >> let elder child processknow and talk to these younger child ... > each child and the parent, and each child can send messages to the parent ... clients' information (socket discriptor)in order to broadcast messages ...
    (comp.unix.programmer)
  • Forking a daemonic Socket listener from a CGI script - browser times out
    ... Socket server - which listens for a connection request from a socket ... However, I combine them into one CGI program, which forks a child process to ... process from the "CGI program" (parent) process, ...
    (comp.lang.perl.misc)
  • Re: Descriptor passed w/SCM_RIGHTS is invalid
    ... > pipe created by socketpair() to pass this socket to a child process. ... > parent, writes to the socket in the child fail, and errno is set to ... even though lsof gives the descriptor in the child as a valid ...
    (comp.os.linux.development.apps)