Page 264 - DCAP403_Operating System
P. 264

Unit 13: Case Study: Linux




          The page table is accessed using the virtual page frame number as an offset. Virtual page frame   Notes
          5 would be the 6th element of the table (0 is the fi rst element).

          To translate a virtual address into a physical one, the processor must first work out the virtual
          addresses page frame number and the offset within that virtual page. By making the page size
          a power of 2 this can be easily done by masking and shifting. Looking again at Figures 13.2 and
          assuming a page size of 0x2000 bytes (which is decimal 8192) and an address of 0x2194 in process
          Y’s virtual address space then the processor would translate that address into offset 0x194 into
          virtual page frame number 1.
          The processor uses the virtual page frame number as an index into the processes page table to
          retrieve its page table entry. If the page table entry at that offset is valid, the processor takes the
          physical page frame number from this entry. If the entry is invalid, the process has accessed a
          non-existent area of its virtual memory. In this case, the processor cannot resolve the address and

          must pass control to the operating system so that it can fix things up.

          Just how the processor notifies the operating system that the correct process has attempted
          to access a virtual address for which there is no valid translation is specific to the processor.

          However the processor delivers it, this is known as a page fault and the operating system is
          notified of the faulting virtual address and the reason for the page fault.

          Assuming that this is a valid page table entry, the processor takes that physical page frame
          number and multiplies it by the page size to get the address of the base of the page in physical
          memory. Finally, the processor adds in the offset to the instruction or data that it needs.
          Using the above example again, process Y’s virtual page frame number 1 is mapped to physical
          page frame number 4 which starts at 0x8000 (4 x 0x2000). Adding in the 0x194 byte offset gives

          us a final physical address of 0x8194.
          By mapping virtual to physical addresses this way, the virtual memory can be mapped into the
          system’s physical pages in any order. For example, in Figure 13.2 process X’s virtual page frame
          number 0 is mapped to physical page frame number 1 whereas virtual page frame number 7 is
          mapped to physical page frame number 0 even though it is higher in virtual memory than virtual
          page frame number 0. This demonstrates an interesting byproduct of virtual memory; the pages
          of virtual memory do not have to be present in physical memory in any particular order.

          13.5.1 Demand Paging

          As there is much less physical memory than virtual memory the operating system must be careful

          that it does not use the physical memory inefficiently. One way to save physical memory is to
          only load virtual pages that are currently being used by the executing program. For example, a
          database program may be run to query a database. In this case not the entire database needs to
          be loaded into memory, just those data records that are being examined. If the database query is
          a search query then it does not make sense to load the code from the database program that deals
          with adding new records. This technique of only loading virtual pages into memory as they are
          accessed is known as demand paging.
          When a process attempts to access a virtual address that is not currently in memory the processor
          cannot find a page table entry for the virtual page referenced. For example, in Figure 13.2 there is

          no entry in process X’s page table for virtual page frame number 2 and so if process X attempts
          to read from an address within virtual page frame number 2 the processor cannot translate the
          address into a physical one. At this point the processor notifies the operating system that a page

          fault has occurred.
          If the faulting virtual address is invalid this means that the process has attempted to access a
          virtual address that it should not have. Maybe the application has gone wrong in some way, for
          example writing to random addresses in memory. In this case the operating system will terminate
          it, protecting the other processes in the system from this rogue process.



                                           LOVELY PROFESSIONAL UNIVERSITY                                   257
   259   260   261   262   263   264   265   266   267   268   269