Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-18
8
Medium Priority
?
368 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 750 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

972 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