Solved

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

Posted on 2004-04-18
8
331 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Downgrade centos 6.5 to 6.4? 7 1,000
Is init file for an application part of rpm 3 40
Understanding curl command 1 50
Problem to run file 9 105
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 purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now