Solved

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

Posted on 2004-04-18
8
327 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
why should double free() crash? 7 30
yum update fails 11 372
check failing on yum update 5 29
Replication of Nagios surveilance data 2 90
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

13 Experts available now in Live!

Get 1:1 Help Now