The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Address space for parent and child in linux
Discuss Address space for parent and child in linux in the C Programming forum on Dev Shed. Address space for parent and child in linux C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 11th, 2012, 12:07 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 13
Time spent in forums: 3 h 34 m 57 sec
Reputation Power: 0
|
|
|
Address space for parent and child in linux
Code:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int *p ;
if ((p = (int *)malloc(5*sizeof (int)))==NULL){
perror("malloc");
return 0;
}
if(0== fork()){
printf("in child\n");
printf(" p = %p\n", p);
sleep(3);
printf(" *P = %d and *P + 1 = %d\n", *p, *(p+1));
printf(" p = %p\n", p);
free(p);
}else {
printf("in parent\n");
printf(" p = %p\n", p);
*p = 100 , *(p+1)= 200;
printf(" *p = %d and *p + 1 = %d\n", *p, *(p+1));
printf(" p = %p\n", p);
wait();
free(p);
}
return 0;
}
I have read that
Parent and child have different address space.
Parent and child share same address space as long as changes are not done by any one.
I have created a situation where changes are done by one and printed the address but
i see the same address as before.
Am i doing something wrong.
I am expecting the value of p from the last printf statement is
different in 2 processes.
|

October 11th, 2012, 12:50 PM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
It is important to understand the concepts of virtual memory management in order to understand what is happening. Each process gets its own virtual memory space, but might share actual code pages. When a process gets duplicated (i.e., fork) initially the code pages are shared between them. As long as there are no changes to the shared memory pages the processes actually use the exact same physical memory, but once one of them makes a change a copy is made (copy on write). At that point the virtual memory manager makes a copy of the relevant page in memory (generaly 2, 4 or 8 K bytes) and assigns it to a new _real_ memory address. However, the _virutal_ memory address remains unchanged.
Generally speaking you cannot access the real memory location outside of kernel space.
|

October 11th, 2012, 01:36 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by ccsr
Code:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int *p ;
if ((p = (int *)malloc(5*sizeof (int)))==NULL){
perror("malloc");
return 0;
}
if(0== fork()){
printf("in child\n");
printf(" p = %p\n", p);
sleep(3);
printf(" *P = %d and *P + 1 = %d\n", *p, *(p+1));
printf(" p = %p\n", p);
free(p);
}else {
printf("in parent\n");
printf(" p = %p\n", p);
*p = 100 , *(p+1)= 200;
printf(" *p = %d and *p + 1 = %d\n", *p, *(p+1));
printf(" p = %p\n", p);
wait();
free(p);
}
return 0;
}
I have read that
Parent and child have different address space.
Parent and child share same address space as long as changes are not done by any one.
I have created a situation where changes are done by one and printed the address but
i see the same address as before.
Am i doing something wrong.
I am expecting the value of p from the last printf statement is
different in 2 processes. |
You're not doing anything wrong. Linux will only share memory among processes that is read-only. If you want to share writable memory across processes then you'll have to check-out IPC(shared memory) or write your own kernel module which will modify the memory pages which you want to modify and share. I did the latter and it really teaches you how memory works on the Linux system(nothing like walking the memory page tables).
|

October 11th, 2012, 11:48 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 13
Time spent in forums: 3 h 34 m 57 sec
Reputation Power: 0
|
|
|
Thanks for the replies i figured it out as , though the kernel is using seprate pages (after modification by either process), the virtual address is same.
so ,there is no way to see the real pages used by the processes?
|

October 12th, 2012, 04:53 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
Not outside the kernel. That is by design.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|