Page Fault

An interrupt that occurs when a program requests data that is not currently in real memory.
The interrupt triggers the operating system to fetch the data from a virtual memory and load it into RAM.
An invalid page fault or page fault error occurs when the operating system cannot find the data in virtual memory.
This usually happens when the virtual memory area, or the table that maps virtual addresses to real addresses, becomes corrupt.
A page fault is a trap to the software raised by the hardware when a program accesses a page that is mapped in the virtual address space, but not loaded in physical memory.
The hardware that detects a page fault is the memory management unit in a processor.
The exception handling software that handles the page fault is generally part of the operating system.

Types of Page Faults

1. Minor

  • If the page is loaded in memory at the time the fault is generated, but is not marked in the memory management unit as being loaded in memory, then it is called a minor or soft page fault.
  • The page fault handler in the operating system merely needs to make the entry for that page in the memory management unit point to the page in memory and indicate that the page is loaded in memory.
  • If we remove a page that does not need to be written to disk (if it has remained unchanged since it was last read from disk, for example) and place it on a Free Page List if the working set is too large.
  • However, the page contents are not overwritten until the page is assigned elsewhere, meaning it is still available if it is referenced by the original process before being allocated. 
  • Since these faults do not involve disk latency, they are faster and less expensive than major page faults.

2. Major

  • This is the mechanism used by an operating system to increase the amount of program memory available on demand.
  • The operating system delays loading parts of the program from disk until the program attempts to use it and the page fault is generated.
  • If the page is not loaded in memory at the time of the fault, then it is called a major or hard page fault.
  • The page fault handler in the OS needs to find a free location: either a page in memory, or another non-free page in memory.
  • Once the space has been made available, the OS can read the data for the new page into memory, add an entry to its location in the memory management unit, and indicate that the page is loaded.
  • Thus major faults are more expensive than minor faults and add disk latency to the interrupted program's execution.

3. Invalid

  • If a page fault occurs for a reference to an address that's not part of the virtual address space, meaning there can't be a page in memory corresponding to it, then it is called an invalid page fault.
  • The page fault handler in the operating system then needs to terminate the code that made the reference, or deliver an indication to that code that the reference was invalid.
  • Attempts to read or write the memory referenced by a null pointer get an invalid page fault.

Handling illegal accesses and invalid page faults

  • Illegal accesses and invalid page faults can result in a program crash, segmentation error, bus error or core dump depending on the operating system environment.
  • Operating systems such as Windows and UNIX (and other UNIX-like systems) provide differing mechanisms for reporting errors caused by page faults.
  • Windows uses structured exception handling to report page fault-based invalid accesses as access violation exceptions, and UNIX systems typically use signals to report these error conditions to programs.
  • If the program receiving the error does not handle it, the operating system performs a default action, typically involving the termination of the running process that caused the error condition, and notifying the user that the program has malfunctioned.

Performance

Page faults, by their very nature, degrade the performance of a program or operating system and in the degenerate case can cause thrashing.
Optimization of programs and the operating system that reduce the number of page faults improve the performance of the program or even the entire system.
The two primary focuses of the optimization effort are reducing overall memory usage and improving memory locality. 
To reduce the page faults in the system, programmers must make use of an appropriate page replacement algorithm that suits the current requirements and maximizes the page hits.

Translation

          Suppose your program generated the following virtual address F0F0F0F0 hex (which is
1111 0000 1111 0000 1111 0000 1111 0000 two )

How would you translate this to a physical address?
Fig 1: Translation of virtual address to physical address

  • First, you would split the address into a virtual page, and a page offset
  • Then, you'd see if the virtual page had a corresponding physical page in RAM using the page table. 
  • If the valid bit of the PTE is 1, then you'd translate the virtual page to a physical page, and append the page offset. 
  • That would give you a physical address in RAM.

Comments

Popular posts from this blog

Pentium microprocessors

Multilevel Organization of Cache Memory