Difference between the address space of parent process and its child process in Linux?

Posted by abbas1707 on Stack Overflow See other posts from Stack Overflow or by abbas1707
Published on 2011-01-04T13:51:07Z Indexed on 2011/01/04 13:53 UTC
Read the original article Hit count: 139

Filed under:
|

Hi,

I am confused about it. I have read that when a child is created by a parent process, child gets a copy of its parent's address space. What it means here by copy? If i use code below, then it prints same addresses of variable 'a' which is on heap in all cases. i.e in case of child and parent. So what is happening here?

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main ()
{
    pid_t pid;
    int *a = (int *)malloc(4);
    printf ("heap pointer %p\n", a);
    pid = fork();
    if (pid < 0) {
            fprintf (stderr, "Fork Failed");
            exit(-1);
    }
    else if (pid == 0) {
            printf ("Child\n");
            printf ("in child heap pointer %p\n", a);
    }
    else {

            wait (NULL);
            printf ("Child Complete\n");
            printf ("in parent heap pointer %p\n", a);
            exit(0);
    }
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux