Solved

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

Posted on 2004-04-18
8
345 Views
Last Modified: 2010-04-22
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
0
Comment
Question by:arunakumarig
8 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 10857205
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
 
LVL 3

Expert Comment

by:PerlKing
ID: 10857407
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10857577
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Mercantilum
ID: 10857661
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10857916
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
 
LVL 3

Expert Comment

by:PerlKing
ID: 10858043
Sorry guys, I seem to have mixed up two irrelevant concepts. :-((

BTW, what is MMU?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10858055
memory management unit
0
 

Author Comment

by:arunakumarig
ID: 10875311
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question