Heap Copying from Parent to Child

We recently had a problem in a pre-lab that tried to answer the question of whether dynamically allocated data would be shared between parent and child processes. The actual heart of the question is whether, after a fork, a parent and its child processes would share a single heap. If this were true, any changes one process would make to malloc’ed data would become immediately visible to all other processes.

To verify what happens in Linux, one could put together a program like this:

Running this program, we see the following output like this:

From the start of the output, we learn that the child inherited “something” from the parent: it got a heap with the same original content. The open question is whether this heap is a copy or if it’s shared memory. Looking at what happens to the values printed after parent and child change the contents of (*p), we see that we must be talking about disjoint areas of memory. That is, the heap is copied after fork, but parent and child have their own separate heaps.

Now, that “conclusion” gets muddied when we look at the memory addresses of the malloc’ed integer, that is the address stored in pointer p: it is exactly the same for both parent and child. It’s fair for one to ask how it’s possible that with addresses being the same, we get the behavior of having different memory locations for the two heaps. One thing we can be sure of: this doesn’t happen by magic. The answer involves copy-on-write semantics and virtual memory, as we see in this StackOverflow post. This goes back to what we should understand as the “context” of a running process.

The calls to sleep are a bit of overzealousness, but they are there to demonstrate to the observer that the child has a chance to store value 4 in the supposedly shared memory area well before the parent has chance to set it to 0.

Print Friendly

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">