fork system call understanding

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

i have a problem in understanding the behaviour of fork .

i understood fork as to create a new process and all the statements that fall after fork are executed by both old and new process.

here's the code i have written and i dont understand how its produced so - called out put.

  1. Relevant commands, code, scripts, algorithms:

  2. The attempts at a solution (include all code and scripts):

#include<stdio.h>

int main( void ) {
        int i ;
        i = 0 ;
        int x;
        while ( i < 2) {
                printf("before fork():getpid()= %d \n", getpid());
                x = fork();
                if ( x == 0 ) {
                        printf("in child when i = %d ", i);
                        printf("getpid()= %d", getpid());
                        printf("getppid()= %d\n",getppid());
                }
                else {
                        printf("in parent when i = %d ", i);
                        printf("getpid()= %d", getpid());
                        printf("getppid()= %d\n",getppid());
                }
                i++;
                printf("\n");
        }
return 0;
}

the out put what i am getting is :

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

VSU,Bangalore,India,Govardhan.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Your program creates four processes.Because two child processes creates another two child processes.Because,the loop runs twice for each process.

Normally when we use fork the child will inherit the workspace of the parent.And also the child and the parent will run simultaneously.and any one can finish its process first.

Now come to your doubt,

Your while loop runs twice.Beginning of the loop you print some thing like " before fork():getpid()= 6010 " .When the first iteration a child process will get create.So the child and parent start to run simultaneously.So the statements can print in any order.

So the child have a provision the iterate the loop.So it will print the "before fork statement" .When the next iteration of the loop also the "bofore fork will get print.And the child process will get create and the appropriate statements will get print.

When the try for the next iteration the condition will get false.So it will come out.

Here :
One of the "before fork" statement is printed by the child process.

is there any technique to print the statements in the right order.
by right order i mean : to print the statements that come under same process rather than printing randomly.

i am unable to understand the process concept it self.

Yeah ! you can do this using the wait,waitpid functions.

man wait

Refer this urls too :

http://www.cim.mcgill.ca/~franco/OpSys-304-427/lecture-notes/node16.html