How do I wait for the child process ?

Discussion in 'C Programming' started by codefixer@gmail.com, Mar 28, 2005.

  1. Guest

    Hi,

    I have a situation where I have to handle the following scenario.
    The main() must wait for child to complete or the main() must kill the
    child after 3 seconds and exit.

    /* Assume everythign is declared */

    time(&start);
    while ( childpid != wait(&status) )
    {
    time(&end);
    diff = difftime(end,start);
    fprintf(stderr, "\n%ld",diff);
    if (diff >= 3.00)
    {
    /* Kill child */
    exit(0);
    }
    }
    }


    Even though the child is taking more than 3 seconds, this doesn't seem
    to work. Any idea why ?

    Thanks.
     
    , Mar 28, 2005
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    > I have a situation where I have to handle the following scenario.
    > The main() must wait for child to complete or the main() must kill the
    > child after 3 seconds and exit.


    >/* Assume everythign is declared */


    >time(&start);
    >while ( childpid != wait(&status) )


    Wait is unconditional.

    >{
    > time(&end);
    > diff = difftime(end,start);


    So all you are calculating there is the time until you did get a status
    report. That's not at all the same as limiting the time you want to
    wait for a report.

    To limit the time you want to wait for a report, you may wish
    to investigate alarm().

    Also, you may wish to take into account the fact that under
    some circumstances, the report you get will not be that the
    child has completed, but rather that the child has stopped
    (e.g., SIGSTOP such as if the user ^Z'd in a bourne shell
    derivative.)
    --
    "I want to make sure [a user] can't get through ... an online
    experience without hitting a Microsoft ad"
    -- Steve Ballmer [Microsoft Chief Executive]
     
    Walter Roberson, Mar 28, 2005
    #2
    1. Advertising

  3. On 28 Mar 2005 10:57:06 -0800, in comp.lang.c ,
    wrote:

    >Hi,
    >
    >I have a situation where I have to handle the following scenario.
    >The main() must wait for child to complete or the main() must kill the
    >child after 3 seconds and exit.


    wait and its friends are not part of C, but are typically part of the
    unix OS. You will get a better response in comp.unix.programmer.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Mar 28, 2005
    #3
  4. On 28 Mar 2005 19:03:05 GMT, in comp.lang.c ,
    -cnrc.gc.ca (Walter Roberson) wrote:

    >Wait is unconditional.


    and offtopic here.

    >So all you are calculating there is the time until you did get a status
    >report. That's not at all the same as limiting the time you want to
    >wait for a report.


    you have absolutely no way to know that, since you don't even know
    what wait() does on that guy's OS. I grant you, it may do what it does
    on unix but then again it may not.

    Please, do redirect people to the right group, and if you /must/
    answer, make it clear that you're guessing.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Mar 28, 2005
    #4
  5. Guest

    Mark McIntyre wrote:
    > On 28 Mar 2005 10:57:06 -0800, in comp.lang.c ,
    > wrote:
    >
    > >Hi,
    > >
    > >I have a situation where I have to handle the following scenario.
    > >The main() must wait for child to complete or the main() must kill

    the
    > >child after 3 seconds and exit.

    >
    > wait and its friends are not part of C, but are typically part of the
    > unix OS. You will get a better response in comp.unix.programmer.
    >


    OK Thanks. I will try it there.

    > --
    > Mark McIntyre
    > CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    > CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
    >
    > ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet

    News==----
    > http://www.newsfeeds.com The #1 Newsgroup Service in the World!

    120,000+ Newsgroups
    > ----= East and West-Coast Server Farms - Total Privacy via Encryption

    =----
     
    , Mar 28, 2005
    #5
  6. In article <>,
    Mark McIntyre <> wrote:
    >On 28 Mar 2005 19:03:05 GMT, in comp.lang.c ,
    >-cnrc.gc.ca (Walter Roberson) wrote:


    >>So all you are calculating there is the time until you did get a status
    >>report. That's not at all the same as limiting the time you want to
    >>wait for a report.


    >you have absolutely no way to know that, since you don't even know
    >what wait() does on that guy's OS.


    I doubt there has ever been an OS written that would look forward
    into a user's program for an occurance of a time difference, and modify
    the behaviour it assigns to wait() so as to wait no longer than the
    tested difference.


    >Please, do redirect people to the right group, and if you /must/
    >answer, make it clear that you're guessing.


    When you answer enough Usenet questions, you develop a good sense of
    Read Programmer's Mind. "guess" doesn't come into it.
    --
    "Mathematics? I speak it like a native." -- Spike Milligan
     
    Walter Roberson, Mar 28, 2005
    #6
  7. -cnrc.gc.ca (Walter Roberson) writes:
    > In article <>,
    > Mark McIntyre <> wrote:
    >>On 28 Mar 2005 19:03:05 GMT, in comp.lang.c ,
    >>-cnrc.gc.ca (Walter Roberson) wrote:

    >
    >>>So all you are calculating there is the time until you did get a status
    >>>report. That's not at all the same as limiting the time you want to
    >>>wait for a report.

    >
    >>you have absolutely no way to know that, since you don't even know
    >>what wait() does on that guy's OS.

    >
    > I doubt there has ever been an OS written that would look forward
    > into a user's program for an occurance of a time difference, and modify
    > the behaviour it assigns to wait() so as to wait no longer than the
    > tested difference.
    >
    >
    >>Please, do redirect people to the right group, and if you /must/
    >>answer, make it clear that you're guessing.

    >
    > When you answer enough Usenet questions, you develop a good sense of
    > Read Programmer's Mind. "guess" doesn't come into it.


    Sure, but the folks who have the knowledge necessary to confirm that
    your well-informed response is correct (or to point out any errors you
    might have made) hang out in comp.unix.programmer, not in comp.lang.c.
    (As we've already discussed at length in the endless topicality
    debates that make up too much of the traffic here.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 29, 2005
    #7
  8. In article <>,
    Keith Thompson <> wrote:
    >-cnrc.gc.ca (Walter Roberson) writes:
    >> In article <>,
    >> Mark McIntyre <> wrote:


    >>>Please, do redirect people to the right group, and if you /must/
    >>>answer, make it clear that you're guessing.


    >> When you answer enough Usenet questions, you develop a good sense of
    >> Read Programmer's Mind. "guess" doesn't come into it.


    >Sure, but the folks who have the knowledge necessary to confirm that
    >your well-informed response is correct (or to point out any errors you
    >might have made) hang out in comp.unix.programmer, not in comp.lang.c.


    By saying that the appropriate folk are in comp.unix.programmer then
    you are implicitly agreeing that the question was one about unix (or
    work-alike systems.) If so, then my answer was certainly not a
    "guess".

    Mark's point was that we weren't given enough explicit information to
    even know that it was a unix or unix-like OS, and thus that my
    unix-like answer was a "guess" about the host OS behaviour. He is, of
    course correct... in about the same sense as supposing that water put
    on to boil might freeze instead.
    --
    Entropy is the logarithm of probability -- Boltzmann
     
    Walter Roberson, Mar 29, 2005
    #8
  9. -cnrc.gc.ca (Walter Roberson) writes:
    > In article <>,
    > Keith Thompson <> wrote:
    >>-cnrc.gc.ca (Walter Roberson) writes:
    >>> In article <>,
    >>> Mark McIntyre <> wrote:

    >
    >>>>Please, do redirect people to the right group, and if you /must/
    >>>>answer, make it clear that you're guessing.

    >
    >>> When you answer enough Usenet questions, you develop a good sense of
    >>> Read Programmer's Mind. "guess" doesn't come into it.

    >
    >>Sure, but the folks who have the knowledge necessary to confirm that
    >>your well-informed response is correct (or to point out any errors you
    >>might have made) hang out in comp.unix.programmer, not in comp.lang.c.

    >
    > By saying that the appropriate folk are in comp.unix.programmer then
    > you are implicitly agreeing that the question was one about unix (or
    > work-alike systems.) If so, then my answer was certainly not a
    > "guess".


    Mark said it was a guess. I didn't, and I agree with you that the OP
    was almost certainly asking about a Unix system. My point was simply
    that it was off-topic, and should have been posted to
    comp.unix.programmer and *not* to comp.lang.c.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 29, 2005
    #9
  10. On 28 Mar 2005 22:29:56 GMT, in comp.lang.c ,
    -cnrc.gc.ca (Walter Roberson) wrote:

    >I doubt there has ever been an OS written that would look forward
    >into a user's program for an occurance of a time difference, and modify
    >the behaviour it assigns to wait() so as to wait no longer than the
    >tested difference.


    I've no idea what you're trying to say. The point is, wait() need not
    have anything to do with waiting for children. It could wait for a
    signal, wait for user input, wait for some time interval to pass. I've
    worked with several osen that didn't even have the concept of wait().

    >>Please, do redirect people to the right group, and if you /must/
    >>answer, make it clear that you're guessing.

    >
    >When you answer enough Usenet questions, you develop a good sense of
    >Read Programmer's Mind. "guess" doesn't come into it.


    Axiomatically, if answering an offtopic question in CLC, you're
    guessing.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Mar 29, 2005
    #10
  11. On 29 Mar 2005 01:21:54 GMT, in comp.lang.c ,
    -cnrc.gc.ca (Walter Roberson) wrote:

    >
    >By saying that the appropriate folk are in comp.unix.programmer then
    >you are implicitly agreeing that the question was one about unix (or
    >work-alike systems.) If so, then my answer was certainly not a
    >"guess".


    "if so", I'd agree. Did the OP state he was on unix? If not, you were
    guessing.

    >Mark's point was that we weren't given enough explicit information to
    >even know that it was a unix or unix-like OS, and thus that my
    >unix-like answer was a "guess" about the host OS behaviour.


    No, my point was that anyone can say anything they like about unix
    system calls in CLC, and not get called up for their error. This is
    because the experts hang out over in CUP. So as far as I'm concerned,
    your answer was an uncorroborative guess.

    >He is, of
    >course correct... in about the same sense as supposing that water put
    >on to boil might freeze instead.


    In about the same sense as it makes to answer unix questions here.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Mar 29, 2005
    #11
  12. In article <>,
    Mark McIntyre <> wrote:
    >No, my point was that anyone can say anything they like about unix
    >system calls in CLC, and not get called up for their error. This is
    >because the experts hang out over in CUP. So as far as I'm concerned,
    >your answer was an uncorroborative guess.


    POSIX 1003.1-1990, section 3.2.1.2

    The wait() function shall suspend execution of the calling process
    until status information for one of its terminated child processes
    is available, or until a signal whose action is either to
    execute a signal-catching function or to terminate the process
    is delivered. If status information is available prior to the call
    to wait(), return shall be immediate.


    As I said, it was not a guess.

    You are, of course, free to consider anything you want to be "a guess",
    but if your expressed rule is that anything not specified in one of the
    C standards is "a guess", then unless you want to be hypocritical, then
    you should be qualifying even remarks such as "The Sun rose here today"
    as "a guess", and you should be qualifying as a "guess" the very
    existance of the newsgroups you redirect people to. "I'm guessing that
    there might be such a thing as other newsgroups, and I'm guessing that
    one of them might be named comp.unix.programming, and I'm guessing that
    this topic might be appropriate there." For that matter, you should, if
    you are to be consistant, be treating the very existance of the people
    you are replying to as being "a guess" -- and you are only guessing
    that someone actually posted the message, rather than the universe
    spontaneously generating the message. But then, the existance of the
    universe is another "guess" too, since it's existance and properties is
    not specified in one of the C standards.
    --
    This signature intentionally left... Oh, darn!
     
    Walter Roberson, Mar 29, 2005
    #12
  13. On 29 Mar 2005 20:29:03 GMT, in comp.lang.c ,
    -cnrc.gc.ca (Walter Roberson) wrote:

    >In article <>,
    >Mark McIntyre <> wrote:
    >>No, my point was that anyone can say anything they like about unix
    >>system calls in CLC, and not get called up for their error. This is
    >>because the experts hang out over in CUP. So as far as I'm concerned,
    >>your answer was an uncorroborative guess.

    >
    >POSIX 1003.1-1990, section 3.2.1.2
    >
    > The wait() function shall suspend execution of the calling process


    So what? Posix isn't part of C, and for all I know, you made up this
    reference - after all, anyone can type in some random text. You're
    merely strengthening my argument.

    >As I said, it was not a guess.


    I'm sure it wasn't, but in the context of CLC, it was unprovable.

    >You are, of course, free to consider anything you want to be "a guess",
    >but if your expressed rule is that anything not specified in one of the
    >C standards is "a guess",


    in the context of CLC.

    >then unless you want to be hypocritical, then
    >you should be qualifying even remarks such as "The Sun rose here today"
    >as "a guess",


    When we start discussing astronomy here, I'll be saying exactly that.
    Along with other, more pithy, remarks.

    Next.

    >and you should be qualifying as a "guess" the very
    >existance of the newsgroups you redirect people to.


    idiot.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Mar 29, 2005
    #13
  14. Mark McIntyre <> writes:
    > On 28 Mar 2005 22:29:56 GMT, in comp.lang.c ,
    > -cnrc.gc.ca (Walter Roberson) wrote:

    [...]
    >>When you answer enough Usenet questions, you develop a good sense of
    >>Read Programmer's Mind. "guess" doesn't come into it.

    >
    > Axiomatically, if answering an offtopic question in CLC, you're
    > guessing.


    There is no such axiom. Perhaps the word "guessing" doesn't mean what
    you think it means.

    Mark, I'm afraid you're being ridiculous. The subject header is "How
    do I wait for the child process ?", and the posted code clearly used
    POSIX-specific functions and terminology (childpid, wait(&status),
    etc.). Anyone familiar with POSIX and/or Unix-like systems does not
    need to "guess" to know what's being talked about.

    Both the question and the answer happen to be off-topic.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 29, 2005
    #14
  15. On Tue, 29 Mar 2005 21:43:19 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >There is no such axiom. Perhaps the word "guessing" doesn't mean what
    >you think it means.


    I assure you it does!

    >Mark, I'm afraid you're being ridiculous.


    I agree completely. On the other hand, Walter is being a right royal
    prat. In my original post, I merely noted that he should perhaps have
    redirected the OP to the right group. I did however use sarcasm, and I
    should have remembered that few outside Europe understand it.

    >The subject header is "How
    >do I wait for the child process ?", and the posted code clearly used
    >POSIX-specific functions and terminology (childpid, wait(&status),
    >etc.). Anyone familiar with POSIX and/or Unix-like systems does not
    >need to "guess" to know what's being talked about.


    Thats true but wholly irrelevant. My point, which you understand
    perfectly, is that Walter's answer could quite easily have been a
    complete guess, and nobody in CLC could have properly called him on it
    as Posix is offtopic here.

    >Both the question and the answer happen to be off-topic.


    Precisely.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Mar 29, 2005
    #15
  16. Mark McIntyre <> writes:
    > On Tue, 29 Mar 2005 21:43:19 GMT, in comp.lang.c , Keith Thompson
    > <> wrote:

    [...]
    >>Mark, I'm afraid you're being ridiculous.

    >
    > I agree completely.


    Good. Are we done here?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 29, 2005
    #16
  17. Alan Balmer Guest

    On 29 Mar 2005 20:29:03 GMT, -cnrc.gc.ca (Walter
    Roberson) wrote:

    >In article <>,
    >Mark McIntyre <> wrote:
    >>No, my point was that anyone can say anything they like about unix
    >>system calls in CLC, and not get called up for their error. This is
    >>because the experts hang out over in CUP. So as far as I'm concerned,
    >>your answer was an uncorroborative guess.

    >
    >POSIX 1003.1-1990, section 3.2.1.2


    More to the point - you don't know if the OP's system is POSIX, or
    even Unix, for that matter. Other systems have child processes, and
    even wait() functions. In any case, whether your assumptions are
    correct or not, the OP should have been directed to a more appropriate
    forum ("Go to a group appropriate for your system. if it's a Unix
    system, try comp.unix.programmer") for one or more peer-reviewed
    answers. You can pop over there and answer it for him, if you want,
    and he would have the comfort of knowing that someone else would
    correct you if you were wrong, and the advantage of having more than
    one viewpoint.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Mar 29, 2005
    #17
  18. In article <>,
    Mark McIntyre <> wrote:
    >On the other hand, Walter is being a right royal
    >prat.


    To be consistant, you should be saying that you -guess- that I'm
    being a right royal prat.

    >In my original post, I merely noted that he should perhaps have
    >redirected the OP to the right group. I did however use sarcasm, and I
    >should have remembered that few outside Europe understand it.


    What is this Europe you speak of? I do not find "Europe" indexed
    anywhere in X3.159-1989, and for all I know you are making up it's
    existance.

    >Thats true but wholly irrelevant. My point, which you understand
    >perfectly, is that Walter's answer could quite easily have been a
    >complete guess, and nobody in CLC could have properly called him on it
    >as Posix is offtopic here.


    Is that "could have" in the sense of "not allowed to", or in the
    sense of "would not have the knowledge in order to do so" ?
    --
    Any sufficiently advanced bug is indistinguishable from a feature.
    -- Rich Kulawiec
     
    Walter Roberson, Mar 29, 2005
    #18
  19. Alan Balmer Guest

    On Tue, 29 Mar 2005 21:43:19 GMT, Keith Thompson <>
    wrote:

    >he posted code clearly used
    >POSIX-specific functions and terminology (childpid, wait(&status),
    >etc.).


    ?
    I've used a couple of non-Unix systems that used the "pid"
    terminology. "Process identifier" is a fairly obvious name, when it
    doesn't mean proportional, integral, derivative. And I've used DOS
    compilers which provided a wait() function.

    Heck, there are lots of OS's which I don't know anything about, which
    the OP might be using.

    Odds are high that the OP was working with a Unix system, but there's
    no need to make that assumption, since he probably knows.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Mar 29, 2005
    #19
  20. In article <>,
    Mark McIntyre <> wrote:
    >So what? Posix isn't part of C, and for all I know, you made up this
    >reference - after all, anyone can type in some random text.


    Well, I don't know, Mark -- what are your standards of proof for
    matters that are not specified in one of the C standards?

    I could fax you a copy of the page, but you wouldn't know whether I had
    it specially printed out and was just making the whole thing up.

    I could point you to online man pages, but you wouldn't know whether I
    had secretly bought out control of those domains and changed the text,
    and you wouldn't know whether I had perhaps started conspiring years
    ago to slip in misinformation about wait() into references.

    I could, for all you know, be a member of the equivilent of the
    Illuminati, except dedicated to perpetuating the misinformation that
    wait() is unconditional. I could have secret control over the various
    opensource implimentations of wait(), and have, as in "Reflections on
    Trust", perverted gcc and all other known compilers and "dists" so that
    the source could would make it -appear- that wait() was unconditional
    but in actuallity make it sometimes unconditional.

    You never know, I could be a one-man army, sworn to a secret order
    of extra-terrestrials, and publically stating basic falsehoods
    about wait() could be part of a holistic plan to bring down
    civilization, myself driven to join by my addiction to Borganzola cheese.

    So it's probably better that you don't trust anything I say. I guess.
    --
    Look out, there are llamas!
     
    Walter Roberson, Mar 30, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,211
    David Schwartz
    Dec 9, 2003
  2. Huey

    How to make "fork/wait" to WAIT longer?

    Huey, Mar 1, 2004, in forum: C Programming
    Replies:
    1
    Views:
    2,014
    -berlin.de
    Mar 1, 2004
  3. Markus Franz
    Replies:
    1
    Views:
    658
    Jeff Epler
    Apr 22, 2004
  4. empriser
    Replies:
    1
    Views:
    661
    Ian Collins
    Mar 6, 2007
  5. Z W
    Replies:
    0
    Views:
    197
Loading...

Share This Page