C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 11th, 2012, 12:07 PM
ccsr ccsr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 13 ccsr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old October 11th, 2012, 12:50 PM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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.
__________________

My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw

Reply With Quote
  #3  
Old October 11th, 2012, 01:36 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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).

Reply With Quote
  #4  
Old October 11th, 2012, 11:48 PM
ccsr ccsr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 13 ccsr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old October 12th, 2012, 04:53 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
Not outside the kernel. That is by design.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Address space for parent and child in linux

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap