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
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– Karthik Nedunchezhiyan2020-02-07 09:57:10 +00:00Commented Feb 7, 2020 at 9:57
- 
        Ah right, you’re interested in the PTE allocation; I’ve updated my answer to address that.Stephen Kitt– Stephen Kitt2020-02-07 10:18:22 +00:00Commented 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?Karthik Nedunchezhiyan– Karthik Nedunchezhiyan2020-02-07 11:15:07 +00:00Commented 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.Stephen Kitt– Stephen Kitt2020-02-07 11:37:23 +00:00Commented Feb 7, 2020 at 11:37
- 
        1The 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.Stephen Kitt– Stephen Kitt2020-02-07 11:50:24 +00:00Commented Feb 7, 2020 at 11:50
