0

When a fork is called, the stack and heap are both copied from the parent process to the child process. Before using the fork system call, I malloc() some memory; let's say its address was A. After using the fork system call, I print the address of this memory in both parent and child processes. I see both are printing the same address: A. The child and parent processes are capable of writing any value to this address independently, and modification by one process is not reflected in the other process. To my knowledge, addresses are globally unique within a machine.

My question is: Why is it that the same address location A stores different values at the same time, even though the heap is copied?

J.S.
  • 19
  • 3
Abhishek Sagar
  • 1,189
  • 4
  • 20
  • 44

1 Answers1

2

There is a difference between the "real" memory address, and the memory address you usually work with, i.e. the "virtual" memory address. Virtual memory is basically just an abstraction from the Operating System in order to manage different pages, which allows the OS to switch pages from RAM into HDD (page file) and vice versa.

This allows the OS to continue operating even when RAM capacity has been reached, and to put the relevant page file into a random location inside RAM without changing your program's logic (otherwise, a pointer pointing to 0x1234 would suddenly point to 0x4321 after a page switch has occured).

What happens if you fork your process is basically just a copy of the page file, which - I assume - allows for smarter algorithms to take place, such as copying only if one process actually modifies the page file.

One important aspect to mention is that forking should not change any memory addresses, since (e.g. in C) there can be quite a bit of pointer logic in your application, relying on the consistency of the memory you allocated. If the addresses were to suddenly change after forking, it would break most, if not all, of this pointer logic.

You can read more on this here: http://en.wikipedia.org/wiki/Virtual_memory or, if you're truly interested, I recommend reading "Operating Systems - Internals and Design Principles" by William Stallings, which should cover most things including why and how virtual memory is used. There is also an excellent answer to this in this StackOverflow thread. Lastly, you might want to also read answers from this, this and this question.

Community
  • 1
  • 1
SebiH
  • 669
  • 1
  • 8
  • 18
  • Thanks for Answering, so are you saying that parent and child processes shows the same Address A though, but, at a lower level, they are different memory locations, accessible to each of the respective processes independently. – Abhishek Sagar Apr 04 '15 at 12:05
  • Indeed, although the operating system might apply some behind-the-scenes magic to save performace (only copying memory if it's actually modified etc). In a normal application, however, you don't have access to that kind of information, so you can safely assume that both point to different "real" memory addresses despite having the same "display" address. – SebiH Apr 04 '15 at 12:10