Program stack and heap, how do they work?

前端 未结 5 1297
青春惊慌失措
青春惊慌失措 2020-12-23 23:09

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 h

5条回答
  •  有刺的猬
    2020-12-23 23:30

    This is my understanding of those questions:

    1. Is stack also part of some page in main memory?

      Yes, the stack is usually also stored in the process address space.

    2. What happens when the program is moved to waiting state, where is the stack pointer, program counter and other info stored?

      When the operative system takes the process from active to waiting, it stores all registers (that includes the stack pointer and the program counter) in the kernel's process table. Then, when it becomes active again, the OS copies all that information back into place.

    3. Why stack grows down and heap grows up?

      That because they usually have to share the same address space, and as a convenience they each begin on one end of the address space. Then they grow towards each other, giving that grow down-grow up behavior.

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

      The CPU caches will store recently used chunks of the memory. Because both the stack and the heap are stored in main memory, the caches can contain portions of both.

提交回复
热议问题