#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    13
    Rep 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.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    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).
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    13
    Rep 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?
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Not outside the kernel. That is by design.

    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

IMN logo majestic logo threadwatch logo seochat tools logo