Prashant Pandey Prashant Pandey - 1 year ago 62
C Question

Copy-on-write during fork

Here is my understanding of fork():
The fork() system call spins-off a child process from the parent process. The core-image(address-space) of the child process is an exact copy of that of the parent. The address space contains:

  1. Stack.

  2. Text segment (containing the program).

  3. Data segment (containing the variables).

But the copy is not actually done, until one of the process (parent or child) begins writing into its address space. It is only then when the child is allocated a separate address space.

I wrote a program to test my understanding, and the results show I might be missing something:

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main()
pid_t pid;
int a=5;

if(pid<0){/*error condition*/
printf("Error forking\n");

if(pid==0){/*child process*/
printf("Child process here\n");
printf("The value of a is %d\n",a);
printf("The address of a is %p\n",&a);
printf("Child terminated\n");
exit(getpid()); /*child terminates*/
else{/*parent process*/
printf("Parent blocked\n");
wait(NULL); /*waiting for child process to exit*/
printf("Parent process here");
printf("The value of a is %d\n",a);
printf("The address of a is %p\n",&a);
printf("parent terminated");


Here is the output of the above program:

Parent blocked
Child process here
The value of a is 10
The address of a is 0x7ffe4c37b1a0
Child terminated
Parent process hereThe value of a is 5
The address of a is 0x7ffe4c37b1a0

Can someone explain to me why the addresses of both the a's are the same? Since the child process updated its variable, it should have been assigned a separate memory location.

Answer Source

Not so.

The addresses seen by both the child and the parent are relative to their own address spaces, not relative to the system as a whole.

The operating system maps the memory used by each process to a different location in physical (or virtual) memory. But that mapping is not visible to the processes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download