child exit status query

Discussion in 'C Programming' started by Mike, Jan 28, 2009.

  1. Mike

    Mike Guest

    Hi everybody

    i'm playing round with more child process code.

    this is a program straight from my textbook:

    void main(argv, argc)
    int argc; char *argv[];
    {
    int wch, wpar;

    if(!fork())
    {
    printf("child pid %d\n", getpid());
    printf("enter child exit status:");
    scanf("%d", &wch);
    printf("child exiting\n");
    _exit(wch);
    }
    printf("waiting for child to exit\n");
    waitpid(-1, &wpar, 0);
    printf("child exit status was %d\n", (wpar>>8)&0xff);
    }


    i'm confused, because when i run it i get

    child pid 9567
    enter child exit status:waiting for child to exit

    and the program just hangs without giving me a chance to enter an exit
    status for the child.

    can anyone explain? thanks.
    Mike, Jan 28, 2009
    #1
    1. Advertising

  2. On 28 Jan 2009 at 23:17, Mike wrote:
    > i'm confused, because when i run it i get
    >
    > child pid 9567
    > enter child exit status:waiting for child to exit
    >
    > and the program just hangs without giving me a chance to enter an exit
    > status for the child.


    All that's happening is that after the fork(), the child process happens
    to be the first to execute: it displays its prompt and blocks at the
    scanf() call.

    Meanwhile, the parent gets scheduled; it prints its message which gets
    appended to your prompt.

    You should still be able to enter an exit status - just type one in and
    press return!
    Antoninus Twink, Jan 28, 2009
    #2
    1. Advertising

  3. Mike

    Ian Collins Guest

    Mike wrote:
    > Hi everybody
    >
    > i'm playing round with more child process code.
    >
    > this is a program straight from my textbook:
    >
    > void main(argv, argc)
    > int argc; char *argv[];


    I'm starting to think you're taking the piss.

    If you are serious, post to comp.unix.programmer. You are learning Unix
    system programming, not C.

    --
    Ian Collins
    Ian Collins, Jan 28, 2009
    #3
  4. Mike <> writes:
    > i'm playing round with more child process code.

    [...]
    > can anyone explain? thanks.


    Why do you persist in posting to the wrong newsgroup?

    Standard C doesn't support processes. Ask your questions in
    comp.unix.programmer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 28, 2009
    #4
  5. Mike <> writes:
    > i'm playing round with more child process code.
    >
    > this is a program straight from my textbook:
    >
    > void main(argv, argc)
    > int argc; char *argv[];
    > {

    [...]

    What textbook are you using? The use of the old-style function
    definition is excusable if it's a very old book; in that case, you
    should get a newer one if at all possible. But the use of void as the
    return type of main, though it's a common error, is harder to
    understand. The "void" keyword was introduced in the 1989 ANSI C
    standard -- the very same document that established the requirement
    that main returns int. "void main" has *never* been correct.

    (Quibble: an implementation is allowed to accept forms of main other
    than the two presented in the standard -- but no implementation is
    required to do so, and *all* hosted implementations must support "int
    main(void)" and "int main(int argc, char *argv[])".)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 28, 2009
    #5
  6. Mike

    Default User Guest

    Mike wrote:

    > Hi everybody
    >
    > i'm playing round with more child process code.


    This is one too many. You're either trolling, or unable to read and
    comprehend. Either way, a waste of time.

    You know, you hate it, but:

    *plonk*




    Brian
    Default User, Jan 28, 2009
    #6
  7. Mike

    Ian Collins Guest

    Keith Thompson wrote:
    > Mike <> writes:
    >> i'm playing round with more child process code.

    > [...]
    >> can anyone explain? thanks.

    >
    > Why do you persist in posting to the wrong newsgroup?
    >

    Because we've been had old boy. Best of all, old Twinky boy's been had
    good a proper!

    --
    Ian Collins
    Ian Collins, Jan 29, 2009
    #7
  8. Mark McIntyre <> writes:

    > Mike wrote:
    >> Hi everybody
    >>
    >> i'm playing round with more child process code.
    >>
    >> this is a program straight from my textbook:
    >>
    >> void main(argv, argc)
    >> int argc; char *argv[];

    >
    > You need to get a new text-book. This style of C is at least 30 years
    > old and has been deprecated (ie obsolete) since the mid eighties.


    I don't think it was deprecated then and it certainly was not
    obsolete. Portable code had to use this style right up into the early
    90s (and maybe even later). Your point is valid: it is an obsolete
    style, but a 20-25 year old Unix book would have had to use it.

    --
    Ben.
    Ben Bacarisse, Jan 29, 2009
    #8
  9. Mike

    Ian Collins Guest

    Ben Pfaff wrote:
    > Keith Thompson <> writes:
    >
    >> Mike <> writes:
    >>> void main(argv, argc)
    >>> int argc; char *argv[];
    >>> {

    >> [...]
    >>
    >> What textbook are you using? The use of the old-style function
    >> definition is excusable if it's a very old book; in that case, you
    >> should get a newer one if at all possible. But the use of void as the
    >> return type of main, though it's a common error, is harder to
    >> understand. [...]

    >
    > Getting the return type of main wrong is only a nit, compared to
    > getting its parameters in the wrong order!


    Trolls just ain't what they used to be, are they?

    --
    Ian Collins
    Ian Collins, Jan 29, 2009
    #9
  10. Mike

    Ben Pfaff Guest

    Keith Thompson <> writes:

    > Mike <> writes:
    >> void main(argv, argc)
    >> int argc; char *argv[];
    >> {

    > [...]
    >
    > What textbook are you using? The use of the old-style function
    > definition is excusable if it's a very old book; in that case, you
    > should get a newer one if at all possible. But the use of void as the
    > return type of main, though it's a common error, is harder to
    > understand. [...]


    Getting the return type of main wrong is only a nit, compared to
    getting its parameters in the wrong order!
    --
    "The way I see it, an intelligent person who disagrees with me is
    probably the most important person I'll interact with on any given
    day."
    --Billy Chambless
    Ben Pfaff, Jan 29, 2009
    #10
  11. Mike

    Richard Guest

    "Default User" <> writes:

    > Mike wrote:
    >
    >> Hi everybody
    >>
    >> i'm playing round with more child process code.

    >
    > This is one too many. You're either trolling, or unable to read and
    > comprehend. Either way, a waste of time.
    >
    > You know, you hate it, but:
    >
    > *plonk*
    >
    >
    >
    >
    > Brian


    Bwian, fix your signature.

    Oh, and nice contribution.
    Richard, Jan 29, 2009
    #11
  12. Mike

    Richard Guest

    Keith Thompson <> writes:

    > Mike <> writes:
    >> i'm playing round with more child process code.

    > [...]
    >> can anyone explain? thanks.

    >
    > Why do you persist in posting to the wrong newsgroup?


    Yes, he might look for one which could have corrected/modernised his
    main declaration.

    What part of his post was not C?

    >
    > Standard C doesn't support processes. Ask your questions in
    > comp.unix.programmer.


    C supports processes via libraries. This is comp.lang.c. I suggest you
    go to comp.lang.iso.c.
    Richard, Jan 29, 2009
    #12
  13. Mike

    Kaz Kylheku Guest

    On 2009-01-28, Mike <> wrote:
    > and the program just hangs without giving me a chance to enter an exit
    > status for the child.


    Appears to work here:

    0:flynux:/home/kaz/test$ gcc test.c
    test.c: In function `main':
    test.c:3: warning: return type of 'main' is not `int'
    0:flynux:/home/kaz/test$ ./a.out
    child pid 6245
    enter child exit status:waiting for child to exit
    42
    child exiting
    child exit status was 42
    25:flynux:/home/kaz/test$


    The "25" in my command prompt is the exit status of your overall program. I
    get a different number on different runs, sometimes 24, and at other times 26.

    That's because of your void main!

    With all this fuss over a child's exit status, it's rather ironic that you
    declared main to return void.

    :)
    Kaz Kylheku, Jan 29, 2009
    #13
  14. In article <>,
    Keith Thompson <> wrote:
    >Mike <> writes:
    >> i'm playing round with more child process code.

    >[...]
    >> can anyone explain? thanks.

    >
    >Why do you persist in posting to the wrong newsgroup?
    >
    >Standard C doesn't support processes. Ask your questions in
    >comp.unix.programmer.


    Keith Thompson (The_Other_Keith) is a known troll.
    Please ignore his rantings and ravings. Just keep on posting your
    questions here, and the helpful people will do their best to help you
    with your issues.
    Kenny McCormack, Jan 29, 2009
    #14
  15. In article <>,
    Ian Collins <> wrote:
    >Mike wrote:
    >> Hi everybody
    >>
    >> i'm playing round with more child process code.
    >>
    >> this is a program straight from my textbook:
    >>
    >> void main(argv, argc)
    >> int argc; char *argv[];

    >
    >I'm starting to think you're taking the piss.
    >
    >If you are serious, post to comp.unix.programmer. You are learning Unix
    >system programming, not C.


    "Ian Collins" is a known troll. Please ignore his rantings.

    Your postings are completely interesting and definitely on-topic here.
    Kenny McCormack, Jan 29, 2009
    #15
  16. Mark McIntyre <> writes:
    > Ben Bacarisse wrote:
    >
    > <of pre-ansi style declarations>
    >
    >> Portable code had to use this style right up into the early
    >> 90s

    >
    > Er, no, 1989.
    >
    > Unless by "portable" you mean "back-portable to pre-ansi compilers".


    Er, yes, I'm sure that's what he meant by portable.

    For several years after the introduction of the ANSI C standard in
    1989, it was commonly necessary to write code so it would compile with
    pre-ANSI compilers, often by using lots of #ifdefs. Many of the
    #ifdefs could have been avoided by writing pure pre-ANSI C code, most
    of which which was still valid ANSI C code, but programmers with an
    eye to the future wanted to take advantages of the nifty new features
    like prototypes.

    We face a similar situation today with C99, except that the new
    features aren't quite as nifty and the previous standard is more
    rigorously defined.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 29, 2009
    #16
  17. Mark McIntyre <> writes:
    > Keith Thompson wrote:
    > (I wrote)
    >>> Unless by "portable" you mean "back-portable to pre-ansi compilers".

    >> Er, yes, I'm sure that's what he meant by portable.

    >
    > Yes, me too. The /other/ meaning of portable... :)
    >
    >> For several years after the introduction of the ANSI C standard in
    >> 1989, it was commonly necessary to write code so it would compile with
    >> pre-ANSI compilers,

    >
    > I have some eggs here, perhaps you could teach me....
    >
    > gd&r


    Ok ... but then I fail to see why you disagreed with Ben's statement
    upthread.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 29, 2009
    #17
  18. Mike

    Guest

    On 29 Jan, 21:49, Mark McIntyre <>
    wrote:
    > Ben Bacarisse wrote:
    >
    > <of pre-ansi style declarations>
    >
    > >  Portable code had to use this style right up into the early
    > > 90s

    >
    > Er, no, 1989.
    >
    > Unless by "portable" you mean "back-portable to pre-ansi compilers".


    there were still plenty of non-ANSI compilers around
    for a loong time after 1989. HP charged you extra
    for an ANSI compiler.

    I had to modify some K&R C just a few weeks ago.
    It's still around.
    , Jan 30, 2009
    #18
    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. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,873
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,081
    David Schwartz
    Dec 9, 2003
  3. jacob navia
    Replies:
    3
    Views:
    541
    Nick Keighley
    Feb 24, 2010
  4. Keith Thompson
    Replies:
    10
    Views:
    670
    Tim Rentsch
    Mar 3, 2010
  5. kaleem
    Replies:
    8
    Views:
    785
    Tad McClellan
    Dec 14, 2006
Loading...

Share This Page