0

I know what the term Virtual Memory means and what how does the Paging System work, but I want to understand , how Virtual memory is implemented via Paging ?

So let me put the following example , if program wants to run does it first brought to the Virtual Memory before its pages are brought to Main Memory Frames? is that the relation between the Virtual memory and the Paging System ?

Kind regards

Colin Jack
  • 209
  • 3
  • 13

1 Answers1

0

When you run a C program, or a program in some other "down to the metal" language, and it dereference a wild pointer (a pointer to some nonexistent memory location) you get an error message saying Segmentation Fault, Page Fault, or something similar. (But the term Page Fault is sometimes reserved to mean a Segmentation Fault that the operating system can resolve, read on.)

A lot happens before you see that message. Whenever a program tries to use a nonexistent memory address, its host computer raises a fault. It looks something like an interrupt from a device. The OS then looks at an internal data structure describing the virtual memory of the process to determine whether the address in question is part of the virtual memory address range of the program. If it is, the OS either retrieves the page -- the chunk of memory holding the address in question -- from disk, or if it's a response to a request for new data memory, delivers a new page full (usually) of zeros. It then updates the computer's virtual-to-physical address translation registers and restarts the instruction that generated the fault. Zap! the illusion of lots of memory--virtual memory.

Only if the address was not part of the program's declared memory space does the fault make it to an error message visible to the programmer or user.

This is grossly oversimplified: thousands of hardware and software developers have been working on this problem space continuously for over half a century now, and it has many variations and refinements.

The sequence of events that start a new process varies from OS to OS. They all involving loading at least one page and jumping to it: setting the computer's program counter register to point to it.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • ,thanks a lot ,The process you explained above is the scenario of Page Fault , so when we execute a process the process is brought to Virtual memory section of secondary memory or its pages could be stored in any part of the Secondary Memory ? one more thing ,in the c programming example you mentioned above does it mean that operating Systems like windows uses Segmentation instead of paging or using combination of both ? – Colin Jack Mar 17 '20 at 11:31
  • Paging and segmentation mean the same thing in my high-level view of all this. When you dive into the details of the hardware and operating system the terminology is much more precise and specific. Read this for background. https://en.wikipedia.org/wiki/X86_memory_segmentation – O. Jones Mar 17 '20 at 13:35
  • You have your terms backwards in the first paragraph. A segfault is the signal a POSIX OS delivers after the HW raises a page fault exception (no HW page table mapping for that page) that the OS's page-fault handler determines to be "invalid". Valid page faults include reading from memory the OS moved to the page file (hard page fault), or copy-on-write / lazy allocation (soft page fault). [Why page faults are usually handled by the OS, not hardware?](https://stackoverflow.com/a/60825304) details those categories. – Peter Cordes Mar 29 '20 at 08:16
  • TL:DR: the hardware mechanism is paging, not segmentation, even on 32-bit x86 where the HW does support segmentation (OSes use a flat memory model; just relying on paging). So the relevant HW exception is always page fault. If you can somehow get the hardware to raise a segmentation exception, it's never valid. – Peter Cordes Mar 29 '20 at 08:20