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 6640271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:39:10+00:00 2026-05-25T23:39:10+00:00

I am having a difficult time understanding what the fork() command does under different

  • 0

I am having a difficult time understanding what the fork() command does under different scenarios. Here is some sample code from my book:

int main() {
    int a = 12;
    int b = 9;
    int fid = fork();

    if (fid == 0) {
        a++;
    }
    else {
        wait(NULL);
        b = b - 5;
    }

    printf("program exit. a = %d, b = %d\n", a, b);
    return 0;
}

Can someone walk me through what the fork() command is doing in this case and perhaps give some more examples to clarify?

  • 1 1 Answer
  • 0 Views
  • 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-25T23:39:11+00:00Added an answer on May 25, 2026 at 11:39 pm
                     [main]
                     a = 12
                     b = 9
    
                     fork()
                       |
                       |
        +--------------+--------------+
        |                             |  
        |                             |
    [parent]                       [child]
    
    fid = 1234                     fid = 0
    wait(NULL)                     a++
       ...                         printf("a = 13, b = 9");
       ...                         return 0
       ...                         <exit>
    b = b - 5
    printf("a = 12, b = 4");
    return 0
    <exit>
    

    After fork() executes there are two copies of the program. Each process gets its own copies of the variables, so there are now two a‘s, two b‘s, etc. The only difference between the two programs is the value returned from fork(): in the child process fork() returns 0; in the parent process it returns the PID of the child.

    The child process increments a, prints the values of a and b, and exits.

    The parent process first waits for the child process to terminate. Only after the child has finished does it continue on, subtracting 5 from b, printing out a and b, and then exiting.

    The wait(NULL) ensures that the child process’s printout always comes before the parent’s, so you will always get the same output in a reliable order. Without it you wouldn’t be able to depend on the order of the two printouts. They would be the same messages, just in an unpredictable order.

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

Sidebar

Related Questions

I'm having a difficult time understanding how to get Rails to show an explicit
I'm having a difficult time understanding the paradigm of Matlab classes vs compared to
i'm having a difficult time understanding how to control the z property of display
I'm having a really difficult time understanding how this all works together. I've fiddled
I'm having a difficult time understanding where actual Leaks are happening and where they
I'm having a difficult time building a left join query that does something like
I'm having a difficult time trying to load System.Data.SQLite.dll from PowerShell in Windows 7
I'm having a difficult time understanding what's going wrong. I have an NSWindowController with
I am having a difficult time understanding two design approaches in terms of long
I'm having a very difficult time understanding how a c# form can populate and

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.