2

Does the process pre-allocate heap and stack memory while dividing it into pages? If yes, will all those pages be empty initially?

1 Answer 1

4

Processes (or the kernel, acting on behalf of processes) pre-allocate address space, not pages. When a process allocates memory, the corresponding page-table entries are allocated, and initialised to point to the zero page (except on architectures which forbid this). The zero page is set up to return all zeroes on reads, and fault on writes — the fault handler will then allocate a separate physical page.

7
  • tnx @Stephen, Page map table should have entry as equal to no of pages in the process right? or page map table will grow dynamically? Feb 7, 2020 at 9:57
  • Ah right, you’re interested in the PTE allocation; I’ve updated my answer to address that. Feb 7, 2020 at 10:18
  • tnx @Stephen, Now I can understand. Just curious to know "The zero page is set up to return all zeroes on reads" read also should cause page fault right? Feb 7, 2020 at 11:15
  • No, because as long as you haven’t written to the page, reads should return zero, so there’s no need to handle reads specially. Feb 7, 2020 at 11:37
  • 1
    The zero page is mapped read-only. Any attempted writes cause a page fault; the kernel then maps in a different page, updates the PTE, and restarts the write to the new page, and subsequent reads come from the new page. Feb 7, 2020 at 11:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .