90 likes | 196 Views
Chapter 7d: V.M. Protection. Individual Space. Every process wants its own space Ideally, it would like the entire computer to itself Sharing the computer’s memory creates problems Sometimes a program will be at location 4000, sometimes at location 5888820, etc.
E N D
Chapter 7d: V.M. Protection
Individual Space • Every process wants its own space • Ideally, it would like the entire computer to itself • Sharing the computer’s memory creates problems • Sometimes a program will be at location 4000, sometimes at location 5888820, etc. • Use Virtual Memory to fool each process to thinking that it starts at location 0 • CPU uses virtual addresses - Start the program at virtual page 0, even if it’s not physical page 0 • Each process must have an individual page table to make this work • Every virtual page 0 must point to a different physical page, and so on 7.4
Page Table for process A: Page Table for process B: VPN Valid PPN/(index) Bit Disk address000000 1 1001000001 0 sector 5000...000010 1 1100000011 0 sector 4323…000100 1 1011000101 1 0101000110 0 sector 1239...000111 1 0001 VPN Valid PPN/(index) Bit Disk address000000 1 0010000001 1 0000000010 1 0011000011 1 1100000100 0 sector 2311...000101 0 sector 158...000110 0 sector 555...000111 1 0100 Example Virtual page 000000 (process A) points to physical page 1001. Virtual page 000000 (process B) points to physical page 0010. The processes both can start at location 000000, but have different data. Note: Physical page 1100 is shared. 7.4
Protection Using Virtual Memory • We want to protect different processes from each other • Can’t read or write to any other process’s memory, unless specifically allowed • Providing separate page tables fixes this problem • Each process can only access pages through its page table • As long as the page table doesn’t point to pages belonging to other processes, no problem • Since only the OS can write the page tables, the system is safe 7.4
Page Table for process A: Page Table for process B: VPN Valid PPN/(index) Bit Disk address000000 1 1001000001 0 sector 5000...000010 1 1100000011 0 sector 4323…000100 1 1011000101 1 0101000110 0 sector 1239...000111 1 0001 VPN Valid PPN/(index) Bit Disk address000000 1 0010000001 1 0000000010 1 0011000011 1 1100000100 0 sector 2311...000101 0 sector 158...000110 0 sector 555...000111 1 0100 Protection Example How can process A access process B’s V.P. 000010? None of process A’sV.P. point to Physical page 0011 - Impossible toto access it! Note: Since physical page 1100 is shared, protection is violated. 7.4
Shooting Ourselves in the Foot • Virtual Memory Access • Look up page number in page table • Access memory • Each memory access becomes two accesses • Even for addresses stored in the cache • Solution: Cache the page table entries in a special cache • The Translation Lookaside Buffer (TLB) is just a cache that holds recently accessed page table entries • A TLB hit means that we don’t have to actually look in the page table 7.4
TLB Design • We want the TLB to have a high hit rate • Fortunately, pages are huge, providing super-high locality • TLB usually only has a small number of entries (i.e. 64) and is fully-associative • Typical hit rates are 98.0 to 99.9% • The TLB should store anything needed from the page table • Physical page number • Valid bit, Dirty bit • Warning: TLB can violate protection after a process switch • Flush the TLB on each process switch 7.4
Virtual Memory Benefits • Virtual Memory frees the programmer from many issues • Large programs can run in smaller systems • It doesn’t matter what else is running on the system, all programs start at a virtual address of zero and can access the entire address space • Virtual memory protects different processes from each other 7.4
Evidence of Virtual Memory at Work • Thrashing • If a program is just too big, it will constantly page fault to read in new pages (and throw out ones it needs) • Paging Out • If a program has been sitting idle for a long time, it is likely that it will be completely paged out to disk • When you return to the program, it will start out slow as it pages all of the memory back in • Loading • Bringing in a new program may require writing pages for an old one out to disk 7.4