[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: popen2 vs pclose
From: |
Rob Mahurin |
Subject: |
Re: popen2 vs pclose |
Date: |
Tue, 25 Aug 2009 17:37:46 -0400 |
On Aug 25, 2009, at 3:22 PM, John W. Eaton wrote:
Maybe it is just best to expect users of the popen2 function to do
something like
[in, out, pid] = popen2 (...);
...
fclose (in);
fclose (out);
waitpid (pid);
and not try to do this automatically for them. If so, then I think we
just need to document this behavior and make sure that this is done
properly for any code in Octave that uses popen2.
This seems to be the approach taken by Perl's IPC::Open2:
open2() does not wait for and reap the child process after it
exits. Except for short programs where it's acceptable to let the
operating system take care of this, you need to do this yourself.
This is normally as simple as calling "waitpid $pid, 0" when
you're done with the process. Failing to do this can result in an
accumulation of defunct or "zombie" processes. See "waitpid" in
perlfunc for more information.
Python (http://pydoc.org/2.4.1/popen2.html) seems to have a class
with poll() and wait() methods, closer perhaps to your proposed patch.
The reason to call wait() is to collect the status of the child, and
the reason that zombies stay in the process table is to allow this
status to be collected. If you write auto-reaping code where waitpid
() is called at least twice for each pipe, then the whichever call
happens second is guaranteed to fail and there's no reliable access
to the exit status unless you invent a user-space-accessible object.
I think the documentation solution is the simplest and the least
likely to cause mysteries later on.
Rob
--
Rob Mahurin
University of Manitoba, Dept. of Physics & Astronomy
at: Oak Ridge National Laboratory 865 207 2594
Oak Ridge, Tennessee address@hidden