Difference between the address space of parent process and its child process in Linux?
- by abbas1707
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);
}
}