How the memory is mapped when fork is used?
Asked Answered
N

3

5

i am new to "fork()",I read everywhere that when a fork() is called an exact copy of current (calling) process is started.Now when I run following code ,there should be two different processes, having two different memory locations 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's memory space
  }else{
    i++;
    i++;
    printf("child address= %p\n",&i);// this should return the address of child's memory space 
  }
  wait(0);
  return(0);
 }
Why The output looks like:: 
child address::804a01c 
parent address::804a01c

Why both the address are the same for parent as well as child?

Negligee answered 15/3, 2012 at 16:56 Comment(2)
They had better be the same. You want the pointers to reference the memory the same way in both processes (even if both memory chunks are distinct). Read about virtual memory, this will answer your question.Branch
Note that the values of i are different between parent and child, even though both copies are stored at the same virtual address.Endoderm
G
9

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

Nope; Linux implements virtual memory, meaning that each process has its own complete address space. As a result, after a fork, both processes see the same addresses for their copies of in-memory objects.

(As an aside: VM also causes code to be shared between the process in physical memory, and all data will only be copied-on-write.)

Greenwood answered 15/3, 2012 at 16:58 Comment(6)
The "aside" is in fact quite important: nothing is really copied when the child process is spawned. This would be a huge performance loss, given that forking happens very often on Unixes (virtually every time you spawn a new process). The precise mechanism relies on "page faults". You may want to read about them if you are interested.Branch
ok, so how can get we get the actual address of that var and not the virtual one?(asking just to satisfy my curiosity)Negligee
@buch11: I think you cannot, but I will let experts confirm. Anyway, you shouldn't. This kind of information is known by the kernel only, and it will only only let you access process local addresses. This is known as "protected memory". Each process can only access its own memory.Branch
@Negligee you're pretty much asking how the MMU works, if you want to search further.Showily
Only the kernel knows what physical address corresponds to a given virtual address. A user mode program cannot easily detect that it is running on a system with a MMU.Endoderm
to get this exact information you have to probe your program using a tool called systemtapBlackshear
D
3

Addresses are process-local. 804a01c in one process is not the same as 804a01c in another process.

Dicks answered 15/3, 2012 at 16:58 Comment(0)
C
2

Because of virtual memory: both address spaces look identical to the respective processes. The physical memory those are stored in is different. However that is, in practice, complicated by a memory optimization (implemented by most kernels) which maps the corresponding different virtual pages to the same physical pages until one of those processes writes to that page of memory, at which time the page is duplicated physically to another physical address (and the virtual page is remapped for the process).

There are many other complications as well: The most recognized is that the return value of fork() differs between processes, though that is usually a difference in a register value, not memory. However, open files, and some other resources can be marked un-inheritable, so there could be other differences—minor but sometimes useful.

Circular answered 15/3, 2012 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.