How is the Virtual address mapped into physical address? What are the different methods of writing into cache?
How is the Virtual address mapped into physical address? What are the different methods of writing into cache?
Quick answer
A virtual (logical) address is the address made by the program. It must be changed to a physical address in main memory. This is done by the memory management unit (MMU) with the help of a page table. The virtual address is divided into a page number and an offset. The page table is stored in main memory. It has one entry for each page, and the entry holds the frame (block) number and a valid bit.
(a) Mapping of a virtual address to a physical address
The page number is used as an index into the page table. If the valid bit is 1, the frame number is read from the entry. This frame number is joined with the offset to make the physical address. The offset is copied unchanged, because pages and frames have the same size. If the valid bit is 0, a page fault occurs and the operating system loads the page from disk.
A page table lookup costs one more memory access. So a small and fast memory called the translation lookaside buffer (TLB) holds the recent page-to-frame translations. The MMU looks at the TLB first. A TLB hit gives the frame number at once, and only a TLB miss needs the page table.
Virtual address [ page no. | offset ]
| |
TLB / page table
| |
Physical address [ frame no. | offset ]
(b) Methods of writing into cache
When the CPU writes, the cache and the main memory can become different. Two main policies solve this.
Write-through. Every write goes to the cache and to main memory at the same time. Main memory always holds the latest data, so it is simple and safe for I/O devices and multiple processors. The disadvantage is that every write is slow because it uses the main memory, and it creates a lot of bus traffic. A write buffer is often used to hide this delay.
If a program changes the same variable 100 times, write-through sends 100 writes to main memory. The next policy sends just one.
Write-back (copy-back). A write goes only to the cache, and the block is marked dirty with a modified bit. Main memory is updated only when the dirty block is removed from the cache. This gives fewer memory writes and faster operation: the 100 changes above cost only one write, made when the block is replaced. The disadvantage is that main memory may hold old data for some time, so it is more complex to keep consistent.
When a write misses, there are two choices. In write-allocate, the block is first brought into the cache, and then written. In no-write-allocate, the data is written straight to main memory. Write-back is usually used with write-allocate, and write-through with no-write-allocate.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.
Up nextExplain how the computer buses can be used to communicate with memory and I/O. Also draw t