/*:) forktestV1 :) have fun
1. the variable i is declared common to both the parent as well as the child process. but parent and child each get their own version of the variable with the samme name.incrementing the variable in the child does not increment the variabble in the parent.
2. also if a pointer is used the value is not incremented. parent and child both gets a pointer to a variable.
3. when declared globally also does not increment the variable. each process gets a global variable.
4. printing the addres in child and parent gives the same value for the address
5. how it works is the child process executes and after its tiem slice it is moved to the swap space. the parent then executes and after its time slice it is moved to the swap space. the address of the variable remains the same.
*/
// header files
#include<unistd.h>
#include<stdio.h>
//main func
int i=10;
int main(int argc, char *argv[])
{
int pid;
pid=fork();
if(pid==0)
{
printf("the value of i in the child process is %d\n",i);
i=i+10;
printf("the value of i inside child after incrementing is %d\n",i);
printf("the address of i in child is %u\n ",&i);
printf("child terminated\n");
}
else
{
wait(0);
printf("the address of i in parent is %u\n ",&i);
printf("the value of i in the parent process is %d\n",i);
}
return 0;
}
Comments
Post a Comment