How does page table handle both stack and heap memory addresses?

I've been taught that when the CPU generates a logical address, it is translated to physical address using page table, which usually is a contigous array allocated in operating system's memory space. The index into this array is the uppermost bits of the virtual address (aka page number).

Also, I know that the logical address for heap addresses are low while those for stack addresses are high, since they grow in opposite directions with stack starting from 0xffffffffffffffff.

So my question is that since the page number is low for heap addresses but very high for stack addresses, the page number (or the index in the page table) that CPU generates is not contigous but is either very high or very low. Then how is it used to index the page table without creating a very large page table.

Also, we need to maintain the size of the process and raise segmentation fault when the page number is greater than the limit (the number of pages/frames allocated to out process). How is this done if the valid page numbers aren't continous.


So my question is that since the page number is low for heap addresses but very high for stack addresses, the page number (or the index in the page table) that CPU generates is not contigous but is either very high or very low. Then how is it used to index the page table without creating a very large page table.

What CPU generates is virtual memory(virtual means not real) and it has no relation whatsoever with the actual physical memory(also called main memory). Virtual memory is nothing but one big imaginary contiguous block of memory.

It is the duty of page table(Memory management unit), to take care of these virtual addresses and convert them to actual physical addresses of your RAM(or main memory).

A virtual page number i could be mapped to frame number 2000 of physical memory, and the very next virtual page number i + 1 could be mapped to frame number 10000 of physical memory. That is the beauty of Paging , because it allows non-contiguous memory allocation.

So coming to your question: If the page table is very large, then there is a mechanism called multi-level page tables , which can easily tackle the problem of large page tables.

Also, we need to maintain the size of the process and raise segmentation fault when the page number is greater than the limit (the number of pages/frames allocated to out process). How is this done if the valid page numbers aren't continous

First of all, segmentation fault, occurs when you have segmentation as your non-contiguous memory allocation scheme. Paging and segmentation are 2 different schemes of non-contiguous memory allocation. And invalid page numbers are dealt with valid/invalid bit associated with each page table entry.

链接地址: http://www.djcxy.com/p/14516.html

上一篇: 线程堆和堆栈

下一篇: 页表如何处理堆栈和堆内存地址?