Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 671603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:23:32+00:00 2026-05-14T00:23:32+00:00

I try to repeat and learn more advanced uses and options when cutting trees

  • 0

I try to repeat and learn more advanced uses and options when cutting trees with forks in the jungle of C. But foolishly I find an example which should be very easy as I have worked with forks before and even written some code, but i can’t understand it fully.

Here comes :

main() {
 if (fork() == 0) {
  if (fork() == 0) {
   printf("3");
  }
  else if ((wait(NULL)) > 0) {
   printf("2");
  }
 }
 else {
  if (fork() == 0) {
   printf("1");
   exit(0);
  }
  if (fork() == 0) {
   printf("4");
  }
 }
 printf("0");
 return 0;
}

Possible solutions are :

  1. 3201040
  2. 3104200
  3. 1040302
  4. 4321000
  5. 4030201
  6. 1403020

where 2, 5 and 6 are correct answers.

First of all, shouldn’t there be four zeroes in the output?
Second… How does one come to the solution at all? Been doing this on paper for almost an hour and I’m not even close to understanding why the given solution are more correct than the false ones (except for nr3 as it can’t end with 2 since a 0 must follow).

Anyone with his forks in check who can offer some good explanation?

EDIT:

Found this here look at pdf’s from 2009. Can people now stop making posts about this being a homework and actually try to help? If not please find some other topics to spend your time. Thanks !

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:23:33+00:00Added an answer on May 14, 2026 at 12:23 am

    I think there should be 4 zeroes, and that is what I see if I run your code…

    A good way to analyse this is to draw a diagram like this one – I’ve shown the forks as * with the parent continuing horizontally, and the child below, so that each individual process is on a separate line:

    ----*----*----*----0----exit (return from main)
        |    |    |
        |    |    +----4----0----exit (return from main)
        |    |
        |    +----1----exit (explicitly)
        |
        +-----*----wait----2----0----exit (return from main)
              |
              +----3----0----exit (return from main)
    

    Now it’s easy to see that, because of the wait(), you must see 3 followed some time later by 0, before seeing 2 followed by 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Try to find a global function, to get and to put the backgroundPosition-values, but
I try to add an addons system to my Windows.Net application using Reflection; but
I try Request.Form.Set(k, v) but it's throwing exception Collection is read-only
I'm getting a nothing to repeat error when I try to compile this: search
I know its probably possible, but is it practical and doable to try and
I know its probably possible, but is it practical and doable to try and
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
try { ... } catch (SQLException sqle) { String theError = (sqle).getSQLState(); ... }
I try to fetch a Wikipedia article with Python's urllib: f = urllib.urlopen(http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes) s
I try to define a schema for XML documents I receive. The documents look

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.