in reply to open3 hangs

IPC::Open3 in order to work in as many platforms as possible has become so complex that its inners are almost inaccessible...

Anyway, you have several options:

# untested use POSIX (); sub my_open3 { pipe my($in_c), my($in_p) and pipe my($out_p), my($out_c) and pipe my($err_p), my($err_c) or croak "unable to create pipes"; my $pid = fork; if (!$pid) { defined $pid or croak "fork failed"; unless (open STDIN, '<&', $in_c and open STDOUT, '>&, $out_c and open STDERR, '>&, $err_c) { warn "redirections failed"; POSIX::_exit(1); do { exec @_ }; POSIX::_exit(1); } close $in_c; close $out_c; close $err_c; return ($in_p, $out_p, $err_p, $pid); }

Replies are listed 'Best First'.
Re^2: open3 hangs
by stephan_a (Novice) on Feb 20, 2009 at 10:10 UTC

    Hi

    and thanks for your proposal. I have modified my code to use the old-fashioned pipe/fork/exec procedure showing me that open3 actually is not my problem. Instead fork blocks.

    I did some more investigation and found that forking from a thread can be tricky because one may have to deal with locking issues (fork seems to clone all threads). Now I'm looking for a way to cleanly fork from a (Perl) thread and exec afterwards. Any ideas?

    Kind regards,

    Stephan

      Instead of forking, try creating a new thread and launching the child via system... maybe your libc system() call will use a different aproach that doesn't need to lock other threads, for instance, a vfork based one.

        Hmmm… The documentation (perlfunc) says:

        Does exactly the same thing as "exec LIST", except that a fork is done first, and the parent process waits for the child process to complete.