COMP 3511: Lecture 20
Date: 2024-11-12 15:01:29
Reviewed:
Topic / Chapter:
summary
❓Questions
- ❓ is page table copied upon clone anyway? can't it be shared?
- 👨🏫 as it's part of PCB, it must be copied.
- in principle, it can be shared
Notes
Virtual Memory
-
Demand paging
- instead of loading the entire process
- bring a "page" into memory only when the page: needed / referenced
- less IO needed
- less memory needed
- faster response
- more users: can be ran
- bring a "page" into memory only when the page: needed / referenced
- similar to: paging system w/ swapping
- page: needed if reference to it is made
- invalid reference (illegal address): abort
- not in memory: bring to memory
- lazy swapper: never swaps a page into memory, unless page is needed
- pager: swapper dealing with pages (unit changed into page, instead of block)
- instead of loading the entire process
-
VM concepts
- pager: brings only those "needed"
- determine the pages to be brought by: modifying MMU functionality
- if pages needed: already memory-resident
- no difference from non demand-paging MMU
- if pages: not in memory
- detect & load page into memory from a secondary storage device
- w/o changing program behavior or code
- detect & load page into memory from a secondary storage device
-
Valid-invalid bit
- w/ each table entry: exists associated valid-invalid bit
v
: in-memory (memory resident)i
: not-in-memory
- initially: all set to
i
- during address translation: if valid=invalid bit is
i
:- results in page fault
- during address translation: if valid=invalid bit is
- w/ each table entry: exists associated valid-invalid bit
-
Page fault
- if: any reference of page is made, first reference to the page: traps application into page fault
- it is handled by:
- OS: look at the corresponding PTE
- invalid reference: abort
- else: just not in memory
- get empty frame, if any
- OS: maintains free-frame list
- swap the page (from secondary) into frame via scheduled disk operation
- update the corresponding PTE
- reset page table to indicate: page is now in memory
- set validation bit:
v
- set validation bit:
- restart the instruction caused the page fault
- OS: look at the corresponding PTE
-
Aspects of demand paging
- extreme cse: starts process w/ no pages in memory
- OS: sets instruction pointer to first instruction of process, non-memory resident
- leads to page fault
- same for all other process page, on first access
- aka pure demand paging
- OS: sets instruction pointer to first instruction of process, non-memory resident
- single instruction: could access multiple pages: resulting multiple faults
- e.g. reading two numbers from memory and storing result in memory again
- page faults: decreases as process runs
- due to locality
- to minimize initial / potential high page fault: OS pre-pace some pages into memory
- before process execute
- hardware support needed for demand paging
- page table w/ valid / invalid bit as indication
- secondary memory (swap device w/ swap space) for page in & out
- instruction restart
- extreme cse: starts process w/ no pages in memory
-
Free frame list
- when a page fault occurs: OS must bring desired page into memory
- most OS: maintains a free-frame list: pool
- kernel data structure: only accessible for OS
- OS: typically allocate free-frames w/ zero-fill-on-demand technique
- content of the frame: zeroed-out before re-allocated
- i.e. writing zeros before providing it to a process
- not doing so: leads to potential security issues
- content of the frame: zeroed-out before re-allocated
- when system starts up: all available memory is on free-frame list
-
Performance of demand paging
- stages in demand paging: handle page faults
- trap to the OS
- save user registers & process state (main delay)
- determine: whether interrupt was a page fault
- check if page reference was legal / determine location of page
- issue: a read from disk to free frame
- wait in que for device until read is done
- wait for device seek / latency time
- begin transfer of page to free frame
- allocate CPU cores to other processes while waiting
- receive: interrupt from dis IO (IO: completed!)
- save registers & process state for the other processes
- determine: interrupt was from the dist
- updateL page table & other tables to show page is not in memory
- wait for CPU to be allocated to process again
- restore: user registers, process state, new page table, resume the interrupted instruction
- three major tasks in page-fault service time
- service the interrupt: requires careful coding for efficiency
- read the page: accessing the hard disk (lots of time)
- restart the process: small amount of time
- page switch: probability close to 8 ms for typical hard disk
- page fault rate:
- : no page faults; : every reference is a fault
- effective access time: EAT
- stages in demand paging: handle page faults
-
Computation example
- memory access time: 200 ns
- average page-fault service time: 8 ms
- 👨🏫 roughly 3-4 magnitude difference
- if one access occurs once in a thousand
- , 40 TIMES SLOWER!!
- if we want performance degradation: less than 10 percent
- less than one fault in every 400,000 memory accesses!
- VERY VERY fortunately: memory locality usually satisfies this...
Page Replacement
-
Copy on write
- traditionally:
fork()
works by creating a copy of parent's address space - copy-on-write (COW): allows both parent & child to initially share the same page in memory
- shared pages: marked as copy-on-write pages
- if either process writes to shared page: copy of shared page must be created
- original, non-modified page: not left with no sharing
- shared pages: marked as copy-on-write pages
- COW: allows efficient process creation
- only modifies pages getting copied / duplicated
- results in rapid process creation
- w/ minimizing no. of new pages to by allocated to newly created process
- e.g. most child process invoke
exec
, so no pages (except PCB) will be copied for them
- e.g. most child process invoke
- traditionally:
-
What if: no free frame?
- used up by process pages. kernel, IO buffer, etc.
- how much memory to each memory: frame-allocation algorithm
- page replacement: find some page in memory, and page it out
- algorithms: terminate process? swap out the entire process image? replace the page?
- performance: wish to result in minimum no. of page faults
- must be transparent to process / program execution
- loading same page multiple time: inevitable
-
Page replacement
- prevent: over-allocation of memory by modifying page-fault service routine
- s.t. it include page replacement
- use modify (dirty) bit to reduce: overhead of page transfers
- only modified pages: written back to dist
- page replacement: completes separation between logical memory & physical memory
- large virtual memory: can be supported on a smaller physical memory
- we must replace some page to load
M
- we must replace some page to load
- prevent: over-allocation of memory by modifying page-fault service routine
-
Basic page replacement
- find the location of desired page on dist
- find: a free frame
- if it exists: use it
- if there is no: use page replace algorithm to select a victim frame
- write victim frame back to dist if it is "dirty"
- bring the desired page into (cleared) free frame; update the page table
- continue the process by restarting the instruction
- potentially: two page transfers for a page fault
- significant increase EAT
-
Page and frame replacement algorithms
- page-replacement algorithms
- decide: which frame to replace
- objective: minimize the page-fault rate
- frame-allocation algorithm
- determine: how many frames allocate to each process
- decided by how process access the memory (locality)
- determine: how many frames allocate to each process
- page-replacement algorithms
-
Page and frame replacement algorithms
- evaluate algorithms by: running particular string of memory references reference string
- then: compute no. of page faults on that string
- for a given page size: only consider the page number
- as: consecutive reference to the same page never causes a page fault
- thus: having the consecutive page number in string: doesn't make sense
- e.g.
7,0,1,2,0,3,0,4,2,3,0,2,2,1,2,0,1,7,0,1
- no. of page faults vs. no. of frame
- evaluate algorithms by: running particular string of memory references reference string
-
First-In First-Out (FIFO)
- reference string:
7,0,1,2,0,3,0,4,2,3,0,2,2,1,2,0,1,7,0,1
- 3 frames, then 15 page faults
- actual result: would differ by reference string
- adding more frame: can cause more page faults (strange)
- Belady's Anomaly
- e.g.
1,2,3,4,1,2,5,1,2,3,4,5
- results in 9 faults in 3 frames, 10 in 4 frames
- tracking ages of pages: easy
- use FIFO queue, or some counter, etc.
- reference string:
-
Optimal algorithm
- theoretically optimal, if we know the future!
- replace a page: not used for the longest period in the future
- ideally: choose a page that will not be used at all in the future
- used for: measuring how well your algorithm performs
- 9 faults: optimal for 3 frames