0

i'm confused with some concepts that i need someone to clarify it for me .

i know that there is a part of the program is stored on the hard disk during the execution time , that is on virtual memory , right ?! the virtual memory is divided into blocks of data called "Pages" that in paging system these pages have fixed size (Like 4KBytes ) , some of these pages are mapped (that is exists and loaded in main memory ) into real physical addresses and stored in fixed size block of data (the same size as pages which is for example 4kbytes ) and these blocks of data are called page frames " or Just Frames "
now , there is a mapping table between virtual memory and physical memory right ?! and also there is a logical address is generated by the cpu and is mapped to real physical memory address right ?! so page mapping and logical address mapping are two different things ,but some how works like the same right or wrong ?!

note : until this point i need someone to correct me if i'm wrong !!

now when the program want to use data that are stored on the disk (pages ) ,but not mapped in memory (frames ) the memory needs to do page replacement right ?!

for example it uses the optimal page replacement algorithm , now as we know (if true ) that there is a frames in physical memory and every frame has a size of bytes , (like a block of data containing a series of bytes representing the instructions of program right ?! ) if a program asked for the data that isn't mapped into memory (page fault ) and need to do page replacement using the optimal algorithm , well i know my next question is stupid due because the page replacement is clear that it it replace a full page , but does it really replace a full page or the bytes inside the page (that is a page frame ) because i saw this example in the following video and i got confused of what he said in the beginning of the video that we have frame of size 3 (not 3 frames in memory ) and he starts to replace the pages within a page of size 3 i mean to replace 3 pages you should have 3 corresponding pages in physical memory right ?!

Please i need someone to enplane to me what just happens , because i understood how the algorithm works ,but have problem with the concepts he uses !!

the video : https://www.youtube.com/watch?v=uYcVtK65KHk

P.S : Sorry for taking too long to explain and sorry for my bad english ,

Kind regards

1
  • if you're doing a course, what's your course text? don't you have any resources from people that speak english?!
    – barlop
    Commented Dec 27, 2016 at 3:18

1 Answer 1

2

Disclaimer: Much of this answer is Windows-centric, particularly some of the terminology, and x86/x64-centric. Most of the concepts apply to most other general-purpose virtual memory operating systems and most other platforms, although fine details and some terminology will differ.

part of the program is stored on the hard disk during the execution time , that is on virtual memory , right ?!

Er, yes, but that's not all there is to VM. (n.b.: "VM" in this answer will always mean "virtual memory", never "virtual machine".) In a typical VM environment, the entire address space of the program + the operating system is regarded as being in virtual memory.

At any moment, a subset of virtual memory is "resident", meaning "in RAM and can be referenced without incurring a page fault". Most of the remainder is in "secondary storage", which is almost always generally disk or SSD these days. (Another name for "secondary storage" that is specifically being used to hold not-resident virtual memory is "backing store".)

There is a third subset of the virtual address space, which is stuff that's in RAM but nevertheless will require a page fault to get to it - in Windows this would be a page on the modified or standby page list, or a shared page that's already in RAM for some other process but is not yet valid for the one that incurred the fault. There are also "demand zero" faults. All of these are examples of "soft faults", i.e. those that don't involve disk reads. I'm not going to get into these cases here because this will be a long answer anyway, and also they're irrelevant to your question: none of them involve page replacement.

where virtual memory is divided into blocks of data called "Pages" that in paging system these pages have fixed size (Like 4KBytes ) , some of these pages are mapped (that is exists and loaded in main memory ) into real physical addresses and stored in fixed size block of data (the same size as pages which is for example 4kbytes ) and these blocks of data are called page frames " or Just Frames "

It's the pages of RAM that are called "page frames", not their contents (i.e. not the data). Also btw, it's not just data, but also the program's code that is brought in by paging.

there is a mapping table between virtual memory and physical memory right?

Yes. They're called page tables. A better way to think of it is that the page tables map between virtual page numbers and physical page numbers, aka page frame numbers. That is, they allow address translation from virtual addresses to physical.

btw: We regard resident stuff as still being "virtual" because the addresses we use to refer to it still have to be translated to physical. And even stuff that's resident can have its physical location changed at any time.

and also there is a logical address is generated by the cpu and is mapped to real physical memory address right ?! so page mapping and logical address mapping are two different things ,but some how works like the same right or wrong ?!

Both "logical address" and "linear address" are terms usually associated with x86 segmented addressing. (or as we who have been living in flat address spaces since the 70s call it, "demented addressing".) The "logical address" is composed of a "displacement" or "offset" that comes from the instruction operand, plus the base address of the "memory segment" either implicitly or explictly used by that operand. There are six possible segments, called C for code (used implicitly for anything referencing the instruction stream, like CALL and JMP instructions), D for data (used implicitly for most non-code references), S for stack (used implicitly for references involving the stack pointer register), and three "extras", E, F, and G, that can be used for whatever you want. The operand coding allows you to override the default segment in some cases. The MMU adds the displacement to the base address of the segment together to form a "linear address".

In practice, in modern OSs, segmenting is almost disabled - by which I mean that nearly all segments (except the F and G segments) have a base address of 0, so the "displacement" asserted by the instruction operands just becomes the linear address. And when "paging" is turned on in the CPU (this happens very early in the OS boot, and is never turned off while the OS is running), the linear address is then translated as a virtual address, via the page tables. So we really don't think in terms of "linear addresses" in VM operating systems. The displacement is the same as the linear address is the same as the virtual address, and we just think about virtual addresses.

btw, the x64 designers at AMD, after consulting with the kernel engineers of various OSs, left out the segmenting mechanism almost completely... except for the F and G segments, which Windows and other OSs use to locate key OS data structures.

The way we think of it in the OS is: the CPU, when executing instructions in a VM environment, is always referencing memory via virtual addresses. A virtual address consists of a virtual page number and a byte offset within the page, the latter being the low-order 12 bits. The CPU's memory management unit (MMU) looks up the translation for the virtual page number to physical page number in the page tables. If the page table entry (PTE) does not have the "valid" bit set, that means the page is not "resident", and the processor raises a page fault exception. The OS's pager is the exception handler for that type of exception. If possible, it resolves the fault, resulting in a valid PTE, and then dismisses the exception. The processor then re-executes the instruction that raised the fault - but this time, it doesn't raise the fault.

now when the program want to use data that are stored on the disk (pages ) ,but not mapped in memory (frames ) the memory needs to do page replacement right ?!

Not necessarily. Resolving a "hard" page fault consists, in general and omitting a bunch of optimizations, of:

  1. Determining where on disk the contents of the desired page are stored
  2. Locating an available physical page of RAM and assigning it to the faulting process
  3. Reading the disk contents into the available page
  4. Updating the page table entry for the location of the page, and to set the "valid" bit
  5. Dismissing the page fault exception

Step 2 MAY involve page replacement, but it more commonly uses a page that isn't currently in use. Typical VM OSs maintain lists of such pages.

does it really replace a full page or the bytes inside the page (that is a page frame ) because i saw this example in the following video and i got confused of what he said in the beginning of the video that we have frame of size 3 (not 3 frames in memory ) and he starts to replace the pages within a page of size 3 i mean to replace 3 pages you should have 3 corresponding pages in physical memory right ?!

At step 3 in my list above, the target page(s) are always overwritten in their entirety. I frankly don't know what "replace the pages within a page size of 3" would mean.

Now, one of the optimizations I alluded to above is that if you need virtual page n now, you probably will need virtual page n+1 fairly soon, and etc. So pagers typically do some amount of readahead, reading more than a page at a time. Yes, if you want to read in three pages, you would be dealing with three pages in physical memory. They need not necessarily be contiguous, not in the working set list nor in terms of physical RAM addresses. In fact, one of the huge benefits of virtual memory is that concern for contiguous free virtual memory basically goes away.

References:

5
  • You've written that with no references at all.. Can you state what book(s) and/or websites or methods you studied this from
    – barlop
    Commented Dec 27, 2016 at 3:20
  • @barlop ok, done. Commented Dec 27, 2016 at 7:28
  • First of all thank you for your great answer , you said that "The MMU adds the displacement to the base address of the segment together to form a linear address." so does that mean that linear address is physical ?! or to be more clear does that mean linear address with paging on is virtual address (the same as logical right ?!) and without paging is the same as physical ? one last thing , when we say "page is referenced (read or written)" does that mean that it is in memory (or page is mapped to physical memory right )?! sorry for my stupid questions Kind regards Commented Dec 29, 2016 at 23:48
  • Yes, the linear address with paging enabled is a virtual address (but that is not the same as logical). With paging off the linear address is a physical (RAM) address. "Page is referenced" means an instruction refers to an address within the page. The page may not be in RAM yet, in which case the reference will incur a page fault. Commented Dec 30, 2016 at 1:16
  • And btw, please don't think you're asking "stupid questions". Nobody is born knowing this stuff, and there's alas a lot of variation in the terminology that different people use to explain things. Questions are how we all learn. :) Commented Dec 30, 2016 at 1:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .