CMSC 421 Operating Systems Lecture Notes (c) 1993 Howard E. Motteler Unix (Early System V flavor) Paging ==================================== When a process is swapped in, its `user structure' (Process Control Block) and page tables are loaded into frames A swapped in process can then be scheduled to run No prepaging; pages are brought in as they are referenced Paging operations are divided between the kernel and a _page daemon_ Memory Organization -------------------- Physical memory is divided into Kernel, Core Map, and Frames : -------- frame 1 -------- frame 0 -------- Core Map -------- Kernel 0 -------- Frames can hold text, data, stack, or page tables The core map (or _frame table_) has one entry per frame, in frame order Each core-map record is 16 bytes, and includes fields for A linked list for free frames - next free - prev free Location of page on disk - disk block - disk device Process related info - pointer to process table (PCB) - segment info Fields for paging algorithm - free, wanted, locked, etc. Disk organization ------------------ Unix uses text (executable), data, and stack segments Data and stack are swapped and paged out to paging/swapping disks (using first-fit allocation) Text is paged from the executable binary on disk (i.e., thru the filesystem) Page Replacement ---------------- Basic idea: keep a pool of free frames The page daemon runs the page replacement algorithm Every 1/4 sec, it check the number of free frames This number is compared to system parameters min and max (The daemon tries to keep the number of free frames between min and max) If there are not _min_ free frames, pages are copied to disk until _max_ frames are free The pager does global page replacement (one process can take from another) The pager uses a form of the _clock algorithm_ (2nd chance was a simple type of clock algorithm) If the page daemon does not find _min_ free frames: - The `clock' hand scans core-map entries - Successive clock passes `age' frames; access resets age - An old enough frame goes into the free list - Modified frames are written to disk before being moved A frame on the free list keeps its contents, and can be _recovered_ (if needed), until it has been overwritten The page table sets invalid bit, but keeps pointer to page on free list [ note: old vax did not have hardware page reference bits, and used the clock to mark pages invalid. Using the page then caused a page fault, and software could set a reference bit and mark page valid again. ] Swapping --------- If the number of free pages falls below _min_ too often, the swapper is called to remove a process Selection for swapping out - If one or more processes has been idle for more than 20 seconds, swap out the longest idle - Otherwise, look at the 4 largest processes, and swap out the one that has been in memory the longest Selection for swapping in is based on priority and time swapped out, and size - usually, longest `out' job is selected - only swap in if enough free pages - only user structure (PCB) and page tables are loaded by the swapper