matz / zetabits.com (Yukihiro Matsumoto) wrote:
>Hi,
>
>In message "[ruby-talk:8271] Re: "Catching" "command not found" when using 
>backquotes?"
>     on 00/12/30, "Ben Tilly" <ben_tilly / hotmail.com> writes:
>
>|>You could look at using open3, which lets you capture the stderr of a
>|>subprocess. The downside is that open3 hides the return status of the
>|>subprocess from you ($? is always 0).
>|
>|It also produces zombies if you don't reap your children.  But
>|use the variations on wait to reap children and you can kill two
>|birds with one stone.
>
>I think the one comes with 1.6.2 reaps them.  Still not knowing how to
>report child's exit status, let me see your approach.

I don't have Ruby here to run tests on so I can't give you
what should be working code, but the idea is simple.

open3 returns a process ID.  wait and relatives also return
a process ID.  When you reap a kid, compare the process IDs.
If they are different, throw an exception.  If they are the
same, then $? has your exit status.

At least this works in Perl.  If you want to have lots of
children forked off, then managing this can be messy.  But if
you limit which sections of code create kids and have them
be responsible for cleaning their kids up, it still is not
that difficult.

The example I posted a link to is about the hairiest example
I have in use.

Cheers,
Ben

PS I use IPC::Open3 in Perl quite a bit, but not once have I
found it necessary to use all 3 handles.  Inevitably I have 2
of them just pass through from elsewhere and interact on one
handle.  Which makes my life much simpler.
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com