Process Memory Organization

XINU Memory Organization

I've mentioned before that processes consist of

  1. code they execute,
  2. data they operate on, and
  3. a run-time stack for local variables, etc.

The question is - where are all of these kept for a running process?

How is the memory of each process typically organized?

Typically, each process has its own (possibly, but not necessarily) contiguous piece of memory dedicated to it. There may or may not be protection between address spaces. The address space of a process might look like the following:

The data can be further divided as follows:

As memory is dynamically allocated, the heap grows upward into the free space between the data segment and the stack. Likewise, when the stack grows, it grows downward into the same free space. If either (or both) get too big, they will collide.

This view of "typical" memory organization is similar to what XINU does, but not exactly the same. We will discuss what XINU does in a later chapter.

Here is another question for you:

Does malloc() directly increase the size of the data segment?

NO!

Typically, each process has access to variables that point to its end of text, end of data, and top of stack.

If we have a virtual memory system, we can avoid having the stack and heap bump into one another because the physical addresses occupied by the process need not be contiguous. Here, we can put a large "virtual" gap in the addresses between the stack and heap without wasting any memory. Demand pagin will automatically allocate real memory for the stack and heap as they grow.

Without virtual memory, how can we detect a collision of the stack and heap?

We can put a magic number (some value unlikely to occur in nature) at the top of the data segment. At every context switch we can check to be sure that value is still intact. If ther stack grows into the data segment, there is a good change that the value will be corrupted.

1