For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2008 > popen: how to know the child has exited?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author popen: how to know the child has exited?
Bin Chen

2008-03-11, 10:16 pm

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?

Thanks

Bin
WANG Cong

2008-03-12, 4:46 am

On Tue, 11 Mar 2008 18:37:25 -0700,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?


No. You shouldn't expect the parent receive a SIGPIPE, it should get
a SIGCHLD instead. SIGPIPE is gernated when you use socket. When a
process writes to a socket that has received an RST, the SIGPIPE signal
will be delivered to this process.
Bin Chen

2008-03-12, 4:46 am

On Mar 12, 1:13 pm, WANG Cong <xiyou.wangc...@gmail.com> wrote:
> On Tue, 11 Mar 2008 18:37:25 -0700$B!$(BBin Chen wrote$B!'(B
>
>
>
>
> No. You shouldn't expect the parent receive a SIGPIPE, it should get
> a SIGCHLD instead. SIGPIPE is gernated when you use socket. When a
> process writes to a socket that has received an RST, the SIGPIPE signal
> will be delivered to this process.


man 7 signal

SIGPIPE 13 Term Broken pipe: write to pipe with no
readers

My mistake is I didn't write to this pipe... But seems you
misunderstand the SIGPIPE...
But anyway I want a sync signal to tell me as soon as the child exits.
Oh, SIGCHLD may help.

Thanks.
Bin
WANG Cong

2008-03-12, 4:46 am

On Tue, 11 Mar 2008 23:04:01 -0700,Bin Chen wrote:

> On Mar 12, 1:13 pm, WANG Cong <xiyou.wangc...@gmail.com> wrote:
>
> man 7 signal
>
> SIGPIPE 13 Term Broken pipe: write to pipe with no readers
>
> My mistake is I didn't write to this pipe... But seems you misunderstand
> the SIGPIPE...


Where I only met SIGPIPE is in socket programming. ;(

> But anyway I want a sync signal to tell me as soon as the child exits.
> Oh, SIGCHLD may help.


Of course.

man 7 signal

SIGCHLD 20,17,18 Ign Child stopped or terminated
Rex Mottram

2008-03-12, 8:12 am

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
Barry Margolin

2008-03-12, 7:31 pm

In article < r7idnbqa6MDWJkranZ2dnUVZ_gudnZ2d@comcast
.com>,
Rex Mottram <rex.mottram@not.here> wrote:

> Bin Chen wrote:
>
> 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().

One possibility is to spawn a thread that calls pclose().

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
Rex Mottram

2008-03-12, 10:24 pm

Barry Margolin wrote:
> In article < r7idnbqa6MDWJkranZ2dnUVZ_gudnZ2d@comcast
.com>,
> Rex Mottram <rex.mottram@not.here> wrote:
>
>
> 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.

RM
Bin Chen

2008-03-13, 8:16 am

On Mar 13, 8:51 am, Rex Mottram <rex.mott...@not.here> wrote:
> Barry Margolin wrote:
>
>
>
>
>
> 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().

Thanks again.

Bin
Barry Margolin

2008-03-13, 7:52 pm

In article <_qudnf-jKK0J50XanZ2dnUVZ_ternZ2d@comcast.com>,
Rex Mottram <rex.mottram@not.here> wrote:

> Barry Margolin wrote:
>
> You may be right but I see no clear evidence to that effect in the post.
> The OP really should clarify.


It was my interpretation of "the child program will run for some
time(not fixed)".

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
Scott Lurndal

2008-03-13, 7:52 pm

Bin Chen <binary.chen@gmail.com> writes:
>On Mar 13, 8:51 am, Rex Mottram <rex.mott...@not.here> wrote:
>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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com