Let's say I have a Operating System with 4KB page size, but I need to allocate 8kb of memory for all the variables. Does the Process get new page (second one) or does the current page table simply get bigger?
2 Answers
Some architectures support multiple page sizes. For example x86 supports 4kB, 2MB (512*4kB) and 1GB (512*2MB) because of its hierarchical page table design. MIPS supports any power of two within a certain range, because of its software TLB design.
An operating system should allocate the largest page that it can - if the size and alignment permit. If an x86 OS notices that a whole group of 512 4kB pages are all identical refer to consecutive physical addresses, it may combine them into a 2MB page. It may even rearrange pages in physical memory to enable this to happen. The advantage of using larger pages is that it reduces cache pressure on the TLB, which leads to faster address translation on average, and that less memory is needed to store the page tables. The disadvantage is that virtual memory cannot operate in a fine-grained way, since the whole 2MB has to be swapped out, or split up into small pages again.
Typically, the smallest page size (or a reasonable page size) is used as "the" page size, and then larger pages are created as an optimization when possible. So even a MIPS OS might prefer to treat the 8kB process memory as two 4kB pages. Treating large pages as anything other than an optimization is a non-trivial design challenge.
It gets a new page.
The page size is an operating system constant, and therefore - will not change. The page size determines how "big" are individual chunks of memory.
So, for example, if your page size is 4K, and your program needs 5K bytes, then your operating system will have to allocate at least two pages for your program - one for the first 4K bytes, and another for the last 1K remaining bytes that you use.
-
$\begingroup$ Can that second half empty page be shared with other process? $\endgroup$ Nov 2, 2022 at 17:29
-
1$\begingroup$ depends on the specific operating system, but in most systems thats a no (to my knowledge). Since the page allocation occurs in the page table, and the page table works with the pages as whole units (it cannot cut a page in half) - then it does not allow to give the rest of the memory with another process $\endgroup$ Nov 2, 2022 at 17:35
-
$\begingroup$ @nirshahar: usually, virtual memory OSes support a mechanism to share memory between processes. These pages will appear in the tables of both processes, whether on disk or in RAM. $\endgroup$– user16034Nov 2, 2022 at 17:52
-
2$\begingroup$ Memory should never be shared with other processes, so a page would be protected from being accessed by another process. That protection will be done for a complete page, so partial pages would be very dangerous. But don't worry about wasting memory, a typical cheap computer would have one or two million pages at least. $\endgroup$ Nov 2, 2022 at 19:06
-
1$\begingroup$ Memory is never shared with other processes unless both processes ask for it to be shared in which case there is no problem. $\endgroup$ Nov 3, 2022 at 17:42