Re: popen: how to know the child has exited?



Bin Chen <binary.chen@xxxxxxxxx> writes:
On Mar 13, 8:51 am, Rex Mottram <rex.mott...@xxxxxxxx> wrote:
Barry Margolin wrote:
In article <r7idnbqa6MDWJkranZ2dnUVZ_gudn...@xxxxxxxxxxx>,
Rex Mottram <rex.mott...@xxxxxxxx> wrote:

Bin Chen wrote:
Hi,

I am using popen() to run a program, the parameters I passed to
popen() is "w" because the program will read some input from stdin
which I want to write from the parent program.

The child program will run for some time(not fixed), but when the
child exits, the parent didn't get the SIGPIPE, so how to be notified
when the child is exit?
man pclose

I don't think he wants to block. He wants to do other things, and get
notified when the child has exited, at which time he will call pclose().

You may be right but I see no clear evidence to that effect in the post.
The OP really should clarify. The way I read it, and the way I would
normally use popen("cmd", "w"), is that when I was done writing to the
pipe I'd close it, and then call pclose with confidence that it wouldn't
block for long. But there are of course scenarios which differ from this.

Oh thank you!
I didn't notice you reply because I thought the pclose() work just
like close(), now I reread the man manual and find that it will block
until the program exits! Its useful although I don't want block, as
Barry said I can use a thread to call pclose().


Or you can use fork/exec instead of popen (that's all popen does anyway)
and catch SIGCLD/SIGCHLD to determine when the program exits (or use
waitpid with NOHANG).

scott
.



Relevant Pages

  • Re: popen: how to know the child has exited?
    ... which I want to write from the parent program. ... child exits, the parent didn't get the SIGPIPE, so how to be notified ... and then call pclose with confidence that it wouldn't ...
    (comp.unix.programmer)
  • Re: Got stuck with redirecting to less
    ... I have originally tried "your code with my corrections" on ... > ever in the meantime the child accidentally exits, the parent receives ... > pipe when it exits, so child receives the EOF on the other side. ...
    (comp.unix.programmer)
  • Re: using pipes and signals.
    ... You must have this line only if the child exits if it finds that ... (I assumes that child ends only if there is no message on the pipe. ... int main ...
    (comp.unix.programmer)
  • Re: using pthreads with fork() in linux
    ... the child program performs some action and exits. ... the parent doesn't success to see that his child exited. ... The cast is unnecessary and dangerous here. ...
    (comp.os.linux.development.apps)
  • [PATCH 2/2] wait_task_zombie: fix 2/3 races vs forget_original_parent()
    ... and P is not a child of ptracer's ... thread group. ... P exits and goes to TASK_ZOMBIE. ... This patch hopefully solves 2 and 3, the 1st bug will be fixed later, we need ...
    (Linux-Kernel)