简体   繁体   中英

How to solve this fork() example in c

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

图

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?

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)

If you call fork() then code below it is for both the processes ie parent and child .

fork creates a new process(known as child) whose address space is different than the parent process. So, nothing is shared between them.

You are calling fork in the loop , Actually 2 times , So there will be total 4 independent process.

If you want to do the separate coding in child process always use the returned value of the fork() like this :

if(!fork())
{
 // child process
}
else 
{
 // parent process
}

For questions this :

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

The answer is ,You can not return directly a value from one process to another . The communication between 2 or more process is achieved using the concept called Inter process commmunicaton (IPC) which you can done in 2 ways.

1. shared memory 
2. memssage passing (pipe, rpc)

Also there are lot many things you will have to understand before solving this problem. Specially when you are trying to do it using fork and also want to return values

You should see this

Or, this may help you more

fork();

creates a child process and that copies all the variables of parent to the child variables.

Parent:

Your main program create 2 children as you mentioned in For-Loop, when it creates child1 , the value is i is 0 when it creates child2 , the value is i is 1

Child1:

Child1 start its execution after fork(), i++ executed, in the next iteration( i = 1 ) - condition true ( 1 < 2 ), Child1 forked another child that is child3 , in child3 the value of i is 1 .

Child2 and Child3:

Child2 and Child3 start its execution with i = 1 after fork(), i++ executed, now i becomes 2 , condition false, no further child is created.

3 processes created in Total.

CODE:

int x=0;
int main()
{
  for(i=0;i<2;i++)
  {
    fork();
    x=x+5;
  }
  printf("x = %d\n", x);
 return 0;
}

OUTPUT:

x = 10
x = 10
x = 10
x = 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM