# If you increase the address space for an OS, does the Phys Mem used by the program increase?

I am learning about the OS.

I know: that Address Space Size = # of pages x page size

So if I increase Address Space Size, then the number of pages increases:(not the page size). However, isn't the Virtual Page Number a one-for-one mapping to Physical Page Numbers(frames), so if the number of pages increases with the address space doesn't the frames and hence, the size of RAM used increase with address space increases?

Or, does bigger address space cause more physical frames so each frames physical memory size reduces as total memory is split among more frames.

No. Due to the way virtual memory works, you can have more virtual pages than physical pages (frames). Some virtual pages might not be mapped to any physical page.

It doesn't really make sense to talk about "if you increase the address space size, the number of pages increases". It might be slightly more useful to think of it as "if you increase the number of pages, the address space size increases".

But really, in any given architecture, the address space size is often fixed and cannot be modified: the size of a virtual address is fixed and cannot be changed, and that determines the number of pages and thus the maximum size of the available virtual address space.

I know: that Address Space Size = # of pages x page size

First note that different hardware behaves differently here:

The 80286 CPU did not know about "pages" at all, the ARM250 CPU had a configurable page size while the 80386 CPU has a fixed page size (4096 bytes).

In the rest of the text I'm assuming you are talking about the 80386 case because nearly all modern x86 OSs use this.

However, isn't the Virtual Page Number a one-for-one mapping to Physical Page Numbers

Yes and no:

One virtual page (4096 bytes) corresponds to one physical page (4096 bytes) which means that there is an 1:1 mapping.

However virtual pages have a "present" flag and a "read-only" flag and multiple virtual pages can be mapped to the same physical page.

This is used for the following mechanisms:

If the "present" flag is not set then a virtual page is not mapped to any physical page. This means any attempt to access this page will cause the CPU to notify the OS before the instruction accessing the memory is executed.

(Btw.: This mechanism is responsible for the crash of a C program when accessing the NULL pointer.)

If your program allocates memory many OSs will not really do anything but the OS will only "remember" that certain virtual memory pages can be used by the program. As soon as the CPU notifies the OS about an attempt to access the page the OS will assign a physical page to the virtual page.

The "read-only" flag is used for a mechanism that is called "copy on write":

In many cases there is the probability that two pages contain exactly the same data. Under Linux/Unix this is the case when using the fork() command; using shared libraries (.so or .dll) under any OS (Linux/Unix/Windows/...) is another example.

In such a case the OS will map the virtual pages containing the same data to the same physical page: Because they contain the same data only one page in RAM is needed.

If the program makes the attempt to write to one of these pages the OS is notified (due to the "read-only" flag). The OS will then take a free physical page, make a copy of the original physical page and then map the virtual page to the physical page containing the copy.