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


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.

  • 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? – Karthik Nedunchezhiyan Feb 7 '20 at 9:57
  • Ah right, you’re interested in the PTE allocation; I’ve updated my answer to address that. – Stephen Kitt Feb 7 '20 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? – Karthik Nedunchezhiyan Feb 7 '20 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. – Stephen Kitt Feb 7 '20 at 11:37
  • ohh nice, how CPU finds page is still not written, is there any additional bit stored in page table? – Karthik Nedunchezhiyan Feb 7 '20 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.