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:
- Determining where on disk the contents of the desired page are stored
- Locating an available physical page of RAM and assigning it to the faulting process
- Reading the disk contents into the available page
- Updating the page table entry for the location of the page, and to set the "valid" bit
- 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:
The memory management chapters of Windows Internals by Solomon, Russinovich, and Ionescu - any version, as none of the above is Windows-version-specific.
Intel® 64 and IA-32 Architectures Software Developer’s Manuals, Volume 3A ("System Programming"), Chapters 3 ("Protected Mode Memory Management") and 4 ("Paging").
if you're doing a course, what's your course text? don't you have any resources from people that speak english?! – barlop – 2016-12-27T03:18:53.890