How is memory displayed when using fork?

I am new to fork (), I read everywhere that when fork () is called, an exact copy of the current (calling) process starts. Now, when I run the following code, there should be two different processes, with two different memory cells assigned to their vars and functions.

#include<stdio.h>
int i=10;
int pid;
 int main(){
  if((pid=fork())==0){
    i++;//somewhere I read that separate memory space for child is created when write is needed
    printf("parent address= %p\n",&i);// this should return the address from parent memory space
  }else{
    i++;
    i++;
    printf("child address= %p\n",&i);// this should return the address of child memory space 
  }
  wait(0);
  return(0);
 }
Why The output looks like :: 
child address :: 804a01c 
parent address :: 804a01c

Why are both addresses the same for parent and child?

+5
source share
3 answers

with two different memory locations assigned to their vars and functions.

; Linux , , . fork .

( : VM , copy-on-write.)

+8

. 804a01c 804a01c .

+2

- : . , , . ( ), , , ( ).

There are also many other complications: it is most recognized that the return value fork()is different between the processes, although this is usually the difference in the value of the register rather than in memory. However, open files and some other resources can be marked as un-inheritable, so there may be other differences: minor, but sometimes useful.

+2
source

All Articles