I am studying operating systems and was going through Copy On Write mechanism. From Wiki:
When one process modifies the memory, the operating system's kernel intercepts the operation and copies the memory; thus a change in the memory of one process is not visible in another's.
It says a particular virtual page is remapped if any of the processes that are sharing the address space tries to modify any data value. So, I tried testing it by writing following code :
#include<stdio.h>
#include<stdlib.h>
int main()
{
pid_t pid;
int a = 30;
pid = fork();
if(pid == 0)
{
printf("Parent\t %x\t %d\n",&a,a);
}
else
{
printf("Child\t %x\t %d\n",&a,a);
a = 25;
printf("Child\t %x\t %d\n",&a,a);
}
return 0;
}
Now when I run the program, the output I get is :
$ ./test
Child ae9dfe3c 30
Child ae9dfe3c 25
Parent ae9dfe3c 30
I understand why the address in first and third line are same but my doubt is why is the memory location in second line same as the other two? It should be different because the child process is modifying the value of a ?