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

  • SEARCH
  • Home
  • 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 8924331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:22:59+00:00 2026-06-15T07:22:59+00:00

int x=0; int main() { for(i=0;i<2;i++) { fork(); x=x+5; } return 0; } I

  • 0
int x=0;
int main()
{
  for(i=0;i<2;i++)
  {
    fork();
    x=x+5;
  }
  return 0;
}

diagram

I am a newbie to the fork() concept. Is the above tree (with x values) a correct solution for the C code mentioned above? The values in the nodes are the x values of their processes respectively.

And also can we return values to the parent process from the child process? Suppose in the above example code can I return the x value of the child to the parent process?

  • 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-06-15T07:23:00+00:00Added an answer on June 15, 2026 at 7:23 am

    You mean that’s a process tree and in the bubbles is the value of x? Then no, that’s not correct.

    When a child is spawned, it gets an exact copy of the parent… so let’s “print” some values so we can see the state of things (I’m makeing up PIDs for everything)

    When we start, it’s just the parent:

    parent (11174) x = 0, i = 0
    

    Then we hit the fork(), now we have two processes:

     +--parent (11174) x = 0, i = 0
     |
     +->child1 (11175) x = 0, i = 0
    

    Next the math:

     parent (11174) x = 5, i = 0
    
     child1 (11175) x = 5, i = 0
    

    When we loop back up, our i’s will be incremented, and each process now runs the loop and hits fork():

     +--parent (11174) x = 5, i = 1
     |
     +->child2 (11176) x = 5, i = 1
    
     +--child1 (11175) x = 5, i = 1
     |
     +->child  (11177) x = 5, i = 1
    

    Now everyone hits the math again:

     parent (11174) x = 10, i = 1
    
     child2 (11176) x = 10, i = 1
    
     child1 (11175) x = 10, i = 1
    
     child  (11177) x = 10, i = 1
    

    Finally everyone hits the loop and increments i breaking from it. So your end result is:

     parent (10)----->child1(10)---->child(10)
               |
               +----->child2(10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying the following C code: int main() { printf(text1\n); fork(); printf(text2\n); return
int main(void) { std::string foo(foo); } My understanding is that the above code uses
I tried this experiment on my Linux desktop: int main() { while(1) fork(); return
How exactly does fork() work? The following code #include <stdio.h> int main (int argc,
int main() { ... if(!fork()) { execvp(cmdName,cmdParam); } printf(In main()...); return(0); } Assuming I
#include <stdio.h> int main(void) { fork(); fork(); printf(ppid %d pid %d\n, getppid(), getpid()); return
... int main(void) { int i; for(i=0;i<2;i++){ switch(fork()){ case 0: sleep(1); break; default: sleep(1);
int main() { char *p,c; for(p=Hello World;c=*p;++p) { printf(%c,c); } } In the above
int main() { int *d=0; printf(%d\n,*d); return 0; } this works fine. >cc legal.c
Considering the below code : int main() { int pid; pid=vfork(); if(pid==0) printf(child\n); else

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.