Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • 16
    Just for the sake of your own sanity later, put a "break;" on the "case -1:" line. You'll thank yourself for it later. Also, have the child process call _exit(0), and the parent call exit(0).
    – user732
    Commented Aug 13, 2012 at 1:22
  • 4
    @BruceEdiger Why the need for _exit? What's wrong with doing any cleanup that's been registered?
    – OrangeDog
    Commented Aug 13, 2012 at 12:49
  • 5
    exit(0) will flush stdout and stderr. _exit(0) will not. You can end up with double outputs if there's some bytes on stdout when your program does the fork(), and the child calls exit(0). Because you're learning how fork() works why confuse yourself?
    – user732
    Commented Aug 13, 2012 at 14:38
  • @BruceEdiger learning how fork() works includes learning that it can copy buffered output. Being a reasonably complicated system call, some confusion is probably necessary in the learning process.
    – OrangeDog
    Commented Aug 14, 2012 at 9:36