Why the pointer value is same in both parent and child process after fork()

Hi,

I have code like this

#include<stdio.h>
int main(void)  {
int pid,i=10,*ptr=&i;
pid=fork();
if(pid==0)  {
  printf(%u\n",ptr);
 *ptr=100;
printf("%d\n",*ptr);
}
else if(pid>0) {
printf("%u %d\n",ptr,*ptr);
}
return 0;
}

Here in the about program after the fork operation, the duplicate address space is created for child ,it copies all variable and pointers also into child address space. When I am printing the address of ' i '  in both the processes, it is printing the same value in both parent and child processes, but if i am changing the value of  'i'  through it's pointer in process, its not being reflected in the other process. I am not getting the reason behind it. pls help me

regards

aruna
arunakumarigAsked:
Who is Participating?
 
sunnycoderCommented:
Hi arunakumarig,

> When I am printing the address of ' i '  in both the processes, it is printing the same value
> in both parent and child processes, but if i am changing the value of  'i'  through it's
> pointer in process, its not being reflected in the other process. I am not getting the reason
> behind it.
You just answered your own question ... the address space of the child is a *copy* of the parent ... It is not shared or same ... both are distinct and different address spaces ... it is just the contents that are the same ... thus changes to one are not reflected in another ...

Sunnycoder
0
 
PerlKingCommented:
What arunakumarig pointed out is correct but it doesn't explain why the "address of i" is the same in both cases.

I am not sure which implementation of "fork()" you are using, I mean which OS/Version, but usually when a process does a fork(), the entire memory segement is not immediately duplicated. Now suppose the process is doing a fork() so that it may call an exec() immediately after the fork(), in that case the "duplication" goes waste because the memory segement of the original process is no more going to be used.

So, after a fork() is done, the "same" memory area is used as long as both the processes are "only reading" from this memory. The moment either of the processes tries to write to the memory, the system creates a duplicate of the memory for the child.

Don't forget to mention your OS/Version.
0
 
sunnycoderCommented:
The reason why both the pointers are pointing to the same memory location is not copy on write ... If that were the case, pointers would point to different memory locations after modification in child and/or parent .... this is not the case ... try this

#include<stdio.h>
int main(void)  {
        int pid,i=10,*ptr=&i;
        pid=fork();
        if(pid==0)  {
                  printf("child address, value %p %d\n",ptr,*ptr);
                           *ptr=100;
                  printf("child address, value %p %d\n",ptr,*ptr);
        }
        else if(pid>0) {
                printf("parent address, value %p %d\n",ptr,*ptr);
                *ptr = 200;
                printf("parent address, value %p %d\n",ptr,*ptr);
                fflush (stdout);
                wait(NULL);
        }
        return 0;
}

the reason both pointers point to the same memory location is "virtual memory" ... both the processes have their own virtual address space ... This is the address space that you get to see and manipulate .... in reality this space corresponds to different physical memory locations and the translation from virtual memory location to physical memory location is carried out by the virtual memory system of the OS
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
MercantilumCommented:
Just one more thing:

  - the variable is automatic and allocated in the stack

In this case there is obviously a copy of the stack data as it contains function return addresses etc...

Anyway it works the same for global data :)
0
 
stefan73Commented:
Hi PerlKing,
> why the "address of i" is the same in both cases

It HAS to be. Just image a fork call on a system without virtual addresses/MMU support.
The system would have to relocate all pointers in address space.

Copy on write is a totally different issue. It is a generic memory handling concept.

Cheers,
Stefan
0
 
PerlKingCommented:
Sorry guys, I seem to have mixed up two irrelevant concepts. :-((

BTW, what is MMU?
0
 
sunnycoderCommented:
memory management unit
0
 
arunakumarigAuthor Commented:
Hi,
Thanx for your participation, I got the answer for the question, as the fork() system call carefully copies the address space of the parent in child process, it copies all the linear addresses. Its true for local as well global variables and pointers too. But the MMU will translate this linear addressses into different physical addresses, thats why it is having two different containers one in parent address space and other in child address space. So the chanage made  to the value at this address in one process wont reflect in the other process .Well it won't copy the file descriptor table of parent process on to the child address space, instead it shares the same with the child. Hence the file opened in perant will also be opened in child.

Well ,the "copy on write" is a mechanism used to reduce the overhead of copying the parent address space on to child, it's no way related to the question i asked.

Thanx for giving me the realization regarding this question.


Regrads

aruna
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.