Program stack and heap, how do they work?

Boolean picture Boolean · Jan 14, 2010 · Viewed 19.6k times · Source

I know that every running process has pages associated with it in virtual memory and few of them will be loaded into main memory as required. I also know that program will have a stack and also a heap to allocate dynamic memory. Here are my questions.

  1. Is stack also part of some page in main memory?
  2. What happens when the program is moved to waiting state? Where are the stack pointer, program counter and other info stored?
  3. Why stack grows down and heap grows up?
  4. Can L1, L2 cache contain only one chunk of contiguous memory, or can it have some part of stack and heap?

Can you recommend any good book that covers these things?

Answer

vicatcu picture vicatcu · Jan 14, 2010

Is stack also part of some page in main memory?

Yes - the stack is typically stored in the "low" addresses of memory and fills upward toward its upper limit. The heap is typically stored at the "top" of the address space and grows toward the stack.

What happens when the program is moved to waiting state? Where are the stack pointer, program counter and other info stored?

The O/S stores a "context" per running process. The operation of saving and restoring process state is called a "context switch".

Why stack grows down and heap grows up?

Just a convention as far as I know. The stack doesn't really "grow" it's got fixed allocation.

Can L1, L2 cache contain only one chunk of contiguous memory, or can it have some part of stack and heap?

Caches simply contain snapshots of parts of RAM that have been used (either recently or nearby). At any moment in time they can have memory from any part of the address space in them. What shows up where depends heavily on the structural parameters of the cache (block length, associativity, total size, etc.).

I would suggest Computer Architecture: A Quantitative Approach as a good reference on the underlying hardware and any book on Operating Systems for how the hardware is "managed".