When fork() sys call is executed by the process, a child process generated from it. All the codes following the fork() call is copied to the new physical pages of memory i.e frames. I am not able to visualize the virtual memory part of the child process.because in the following code the address of char variable is same in child as well as parent.
#include <stdio.h>
#include <sys/types.h>
int main(void)
{
pid_t pid;
char y='Y';
char *ptr;
ptr=&y;
pid = fork();
if (pid == 0)
{
y='Z';
printf(" *** Child process ***\n");
printf(" Address is %p\n",ptr);
printf(" char value is %c\n",y);
sleep(5);
}
else
{
sleep(5);
printf("\n ***parent process ***\n",&y);
printf(" Address is %p\n",ptr);
printf(" char value is %c\n",y);
}
}
and its output:
*** Child process ***
Address is 69002894
char value is Z
***parent process ***
Address is 69002894
char value is Y