My queries are :
1. how the fork actually works regarding the memory space of child and parent
2. the below code shows same address for a auto variable "var" , but different
values in case of parent and child.
3. how could same address contain two different values.
/* the code may look big, but its relatively simple, plz
* do look into it
*/
/* headers included */
int main( int argc, char* argv[] ){
pid_t pid;
int var = 0;
printf("before fork address of var = %x\n", &var);
if( (pid = fork()) < 0 )
perror("perror");
if( pid == 0 ){
printf("child process running pid = %d \n",getpid());
var++;
printf("child address of &var = %x\n", &var);
printf("child value of var = %d\n", var);
}
else{
wait();
printf("parent process running = %d \n",getpid());
printf("parent address of &var = %x\n", &var);
printf("parent value of var = %d\n", var);
}
exit(0);
}
/* sample output */
[root@localhost process]# cc p2.c -o p2.out
[root@localhost process]# ./p2.out
before fork address of var = bff5a700
child process running pid = 3918
child address of &var = bff5a700
child value of var = 1
parent process running = 3917
parent address of &var = bff5a700
parent value of var = 0
Thanks in advance,
Vinod