popen: how to know the child has exited? - Unix

This is a discussion on popen: how to know the child has exited? - Unix ; 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 ...

+ Reply to Thread
Results 1 to 10 of 10

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

  1. popen: how to know the child has exited?

    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

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

    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.

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

    On Mar 12, 1:13 pm, WANG Cong wrote:
    > On Tue, 11 Mar 2008 18:37:25 -0700$B!$(BBin Chen wrote$B!'(B
    >
    > > 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.


    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

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

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

    > On Mar 12, 1:13 pm, WANG Cong wrote:
    >> 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.

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

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

    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

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

    In article ,
    Rex Mottram 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().

    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 ***

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

    Barry Margolin wrote:
    > In article ,
    > Rex Mottram 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.

    RM

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

    On Mar 13, 8:51 am, Rex Mottram wrote:
    > Barry Margolin wrote:
    > > In article ,
    > > Rex Mottram 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().

    Thanks again.

    Bin

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

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

    > Barry Margolin wrote:
    > > In article ,
    > > Rex Mottram 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.


    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 ***

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

    Bin Chen writes:
    >On Mar 13, 8:51 am, Rex Mottram wrote:
    >> Barry Margolin wrote:
    >> > In article ,
    >> > Rex Mottram 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

+ Reply to Thread