Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}

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?

like image 334
starkk92 Avatar asked Nov 28 '12 18:11

starkk92


People also ask

What does fork () do in C?

fork() in C. Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.

What is fork () and why is it used?

fork() is how you create new processes in Unix. When you call fork , you're creating a copy of your own process that has its own address space. This allows multiple tasks to run independently of one another as though they each had the full memory of the machine to themselves.

What library is fork () in C?

The C library defines fork() . It is the UNIX/Linux-specific system calls to create a process, on linux etc.

How many child process will be created with 3 fork () calls?

Explanation – Here, we had used fork() function to create four processes one Parent and three child processes. An existing process can create a new one by calling the fork( ) function. The new process created by fork() is called the child process.


3 Answers

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)
like image 103
Mike Avatar answered Oct 17 '22 04:10

Mike


If you call fork() then code below it is for both the processes i.e. 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

like image 33
Omkant Avatar answered Oct 17 '22 05:10

Omkant


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
like image 2
Adeel Ahmed Avatar answered Oct 17 '22 04:10

Adeel Ahmed