26 processors : processor 17 modular (clock) system of 25. 17 communicate: 8 8,10,12,14,16,18,20,22,24,1,3,5,7,9,11,13,15,25,19,21,23,0,2,4,6 9 Processors: processor 2: 7,0,nil,4,6,8,1,3,5 VIRTUAL MEMORY PAGING / SWAPPING Every process has its own memory address system which consist of distinct memory locations far exceeding the amount of memory the machine actually has. In a 64-bit address space, does your machine really store 2^64 bytes? So, when your program accesses a memory location, the OS (you don't have to do it yourself) translates your memory address to a frame stored in physical memory. If there is no frame stored there, that's a page fault. If the page fault is due to malformed memory address, your program crashes. If the page fault is due to that particular page not being loaded up into memory, then that page will have to be loaded into memory from backup storage (oftentimes the hard drive). Now what generally happens is that your computer is already using every frame of its memory, so in order to swap in the memory your program wants, some other page will have to be swapped out (stored to backup) to make room for your page? Which page should it be? Which page should the OS swap out? The one that's going to be used at a time the furthest in the future. Good luck with that, psychic. What's a more realistic system? Some systems use FIFO. The one that's been in memory the longest is the one that gets swapped out. This doesn't work too well. Having every page take its turn getting swapped out doesn't work in situations where some pages get far more hits than other pages...which is to say, it doesn't work. But it's easy to implement, the pages are stored in a queue, and when one needs to be swapped out, the old page is dequeued and the new page is queued. A better system, and one more frequently is called LRU or the least-recently-used page. The page that got used (not loaded up) but actually used the longest ago. There are two basic strategies to keep track of the least recently used page. Strategy One: Every page has a designated section for a time stamp. Every time you access a page you set the time stamp to the current time. When a page needs to be swapped out, swap out the one with the earliest time stamp. Downside: Requires your system to have a clock. Other way, is to store pages in a double-linked list. When you access page, move the page to one end of the linked list. When you need to swap out a page, swap out the page from the other end of the linked list. A<->B<->C<->D<->E access C C<->A<->B<->D<->E access E E<->C<->A<->B<->D access A A<->E<->C<->B<->D access D D<->A<->E<->C<->B a page needs to be swapped out, so I'll swap out B. Another strategy is LFU: Least frequently used. Every time you access a page, you set a counter for that page. When a page needs to be swapped out, swap out the one with the lowest count. Downside: if you're getting rid of a page that hasn't been hit much, you may just be getting rid of the page most recently swapped in. It hasn't been around much to get a lot of hits. One way to counter this, it to not reset a hit count to zero when a page is loaded up, so that it still remembers the number of hits from before it was swapped out in the first place. Still, a page that used heavily early in the program and not much used since will take a while to get swapped out because it generated a lot of hits for itself when the program first began. Most of the swapping algorithms I'm familiar with are variations on LRU. (Least Recently Used). In actual practice, LRU seems to work pretty well. Dirty bit: Anytime you swap a page in, you set the dirty bit to 0. Anytime you modify that page (while it's swapped in), you set the dirty bit to 1. When that page gets swapped out, if the dirty bit is 1, it has to be rewritten to disk. But if the dirty bit is 0, this means that it hasn't been modified since it was loaded (it's been accessed, though), so there's no need to write it to disk; the previous image is still good. (Which means that the disk drive has to keep around a copy of the page while your program is running, even if it's swapped in.) Let's say your program needs to swap a page in, so some page needs to be swapped out. Should that be one of your pages, or is every program on the system fair game for being swapped out? (My porno page needs more memory swapped in, so a page controlling machinery in the ICU gets swapped out.) Not every OS works the same way, but by and large, any page of any program can be swapped out if another program needs a page swapped in.