Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

What is the advantage of paging?


  • Please log in to reply
2 replies to this topic

#1 DarkLordCthulhu

DarkLordCthulhu

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 422 posts
  • Location:The bash shell
  • Programming Language:C, JavaScript, Bash, Others
  • Learning:Ruby, Others

Posted 13 September 2010 - 05:30 AM

I'm trying to understand how operating systems work. Right now I'm studying virtual memory. Here's my understanding: Memory is divided into fixed-size blocks called pages, which are not contiguous in memory but are treated as if they were. Page sizes are a power of 2. The operating system has the job of finding a page and returning its value to a process that requests it. It does this using the Memory Management Unit, a hardware component that divides virtual memory into pages and stores their virtual addresses. The MMU caches virtual addresses in the Translation Lookaside Buffer (TLB), which is a cache stored in the CPU cache that holds entries from the page table. The page table is content-addressable memory (CAM), which is the hardware equivalent of an associative array, and it contains mappings of virtual addresses to physical addresses. When memory is requested from CAM, it searches its entire table and then returns a value.

So the question I want to ask is, first of all, am I right in my understanding of virtual memory and paging? Second, what advantage does paging serve and how does it improve the performance of a computer?
  • 0
Programming is a journey, not a destination.

#2 dbug

dbug

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 148 posts

Posted 13 September 2010 - 06:32 AM

Here's my understanding: Memory is divided into fixed-size blocks called pages, which are not contiguous in memory but are treated as if they were. Page sizes are a power of 2. The operating system has the job of finding a page and returning its value to a process that requests it. It does this using the Memory Management Unit

Basically correct. The RAM we have in our computers is called physical memory. We have a certain amount of it. It is contiguous and is addressed from 0 to the maximum we have (1GB, 2GB, 4GB, ...). The MMU (Memory Management Unit) is the responsible to fragment this contiguous space into pages and map each page to a position in the virtual address space (or virtual memory).

the Memory Management Unit, a hardware component that divides virtual memory into pages and stores their virtual addresses.

As I've just said the MMU divides the physical memory (not the virtual memory, although it might be seen as blocks of the same size than a page) into pages and stores a mapping table between physical addresses and virtual addresses (of course, virtual addressess where the pages can be mapped are multiples of the page size)

The MMU caches virtual addresses in the Translation Lookaside Buffer (TLB), which is a cache stored in the CPU cache that holds entries from the page table. The page table is content-addressable memory (CAM), which is the hardware equivalent of an associative array, and it contains mappings of virtual addresses to physical addresses. When memory is requested from CAM, it searches its entire table and then returns a value.

Yes, the TLB contains the most recently used entries of the map table between physical and virtual addresses to speed up memory accesses.

So the question I want to ask is, first of all, am I right in my understanding of virtual memory and paging?

Basically, you are right. There are a lot more details about paging and caches, but the basic concepts are correct.

Second, what advantage does paging serve and how does it improve the performance of a computer?

Paging has many advantages. First of all, paging allows you to relocate memory areas to other places where they are more useful. For example, any operating system must divide memory in two areas, one for the operating system itself (OS, data, management tables, ...) and one for the user programs. The area for the OS must be subdivided into areas to store each driver and its data. The area for the user must be subdivided to store each program and its data.

If you do not have any virtual memory, you must precalculate the needed space for each program. For example you reserve from address 1000 to 2000 for the first program. The second program is stored between 2000 and 3000, and so on. What happens if the first program needs to allocate more memory than it was initially reserved ? will you deny the request, even if the area between 3000 and the end of memory is available ? or will you allow that programs have memory fragments dispersed through all the memory space ? Another problem is that each program could "see" the memory of the other programs.

Obviosly this is not good for any modern operating system. What paging adds in this case is:

  • Space: every program has virtually all memory available
  • Security: one program cannot access the memory area of other programs
The MMU can remove the memory pages used by one program from the virtual memory and put the pages of another program in the same place. This means that only the pages owned by the current running process are really mapped into the virtual memory, so it can only access its own data.

For example, the first program uses 10 pages, and the operating system allocates these pages at address 1000. Another program is loaded. It uses 20 pages. The operating system removes the 10 pages used by the first program from the virtual address space and allocates the new 20 pages at address 1000 (the same address of the first program). When the operating system decides to switch the current process, it removes its pages from memory and remaps the pages of the other program.

If one of the processes requests more memory, the operating system searches a new free physical page and maps it just after the last used virtual page. Thus, the process sees a contiguous memory space all the time.

Another advantage of having pages is that you can assign rights independently to each page. So you can define some pages that the program can read but not write, other pages that cannot contain executable code, pages that are reserved for private use of the operating system...

Another advantage of paging is that you can have a mechanism that allows you to have more memory than you actually have (using a paging file or swap area in disk). Each page can be marked as present or not present. When some page is not used for a long period of time, the operating system writes the contents of this page to the disk and marks the page as not present. This means that the physical memory page is freed and available to other processes that need memory.

When a program tries to access a non present page, the MMU intercepts the access and tells the operating system that that page must be reloaded. Then, the OS reads the page from disk and marks it as present again so that the program can continue.

All this requires some processing time, but the advantages are worth it. Most of this things are totally transparent to the user and programs.
  • 0

#3 bettylovelife

bettylovelife

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 24 September 2010 - 07:40 PM

As far as I know, the paging can save a lot of memory for the hard disk.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download