This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Operating Systems: Paging/1
1
Paging
Segmentation helps deal with:
– modularity, sharability, protection, relocation
Paging can help with:
– insufficient memory, external fragmentation, efficiency of memory use
•Physical memory divided into fixed size page-frames e.g. 4096 bytes
•Virtual pages mapped into page-frames:
CPU page offset frame offset
frame
page table physical memory
Operating Systems: Paging/1
2
•Virtual address :
–total virtual address space of 2p+d , 2p pages, each of 2d bytes
»e.g. p = 20, d = 12 : 232 bytes virtual space : 220 pages of 4096 bytes each
–virtual space usually very much larger than amount of physical memory
»4Gb physical memory soon possible and common?
»in the near future, 64-bit addressing to extend virtual address space: Intel Itanium, AMD Clawhammer, DEC Alpha etc.
page number page offset
p bits d bits
page 0
page 0
page 2
page 1
virtual memory physical memory
page 1
page 2
page 3
1
4
3
6
page table
0
1
2
3
4
5
6
7
page 3
.
.
.
.
.
.
.
.
Operating Systems: Paging/1
3
•Page table the same length as the number of virtual pages
–can get extremely long
»e.g. 220 pages, each page table entry 4 bytes typically = 4Mb
»252 pages in a 64-bit virtual memory = 254 bytes! bigger pages a possible solution?
e.g. 1Mb pages still gives page table length of 244
–usually have a page table limit hardware register :
»hardware checks page number within the limit for each translation
»limits the length of the page table
»but reduces the size of virtual space – undesirable!
•Each process has its own virtual space
–so each needs its own page table
–may be hundreds of processes around even on a single user workstation
»e.g. scheduling, device driver, comms system processes etc.
»though many of these will be small and not need a large page table
•Need a better system – multi-level page tables described later
Operating Systems: Paging/1
4
•Not all virtual pages can be mapped into physical memory at once–a Presence (or Valid) bit part of each page table entry
»P=1 : page present in memory»P=0 : page not present in memory – page-frame number invalid
page may be temporarily stored on hard disc may not even exist – no problem with gaps in virtual space!
–if the page table entry corresponding to a presented virtual address has P=0
»a page-fault interrupt generated by address translation hardware
page 0
page 0
page 2virtual memory physical memory
page 1
page 2
page 3
1
3
6
0
1
2
3
4
5
6
7
page 3
.
.
.
.
.
.
.
.
1P
0
1
1
Operating Systems: Paging/1
5
•Demand Paging :–invoked when a page-fault interrupt occurs
–a policy in which :»the operating system automatically retrieves the page from hard disc»allows the process to continue with just a short hiatus»transparent to the user
–steps:»check that the missing page actually exists in virtual space (not in a
gap)»find a spare page-frame in memory
may involve removing an existing page according to some page replacement policy
»initiate transfer of missing page from hard disc into spare page-frame»wait until transfer complete
schedule another process in the meantime
»update the page table entry with page-frame number»set P = 1 in the page table entry»put process back on the Run Queue»when process dispatched onto the CPU, instruction causing page-fault is
retried this time no page-fault interrupt will occur
Operating Systems: Paging/1
6
•Multiple processes in physical memory :
page A0
page A0
page A2
page B1
process Avirtual memory
physical memorypage A1
page A2
page A3
1
3
6 0
1
2
3
4
5
6
7
8
9
10
11
page A3
.
.
.
.
.
.
.
.
1
page table A
0
1
1
page B0
page B3
page B0
process Bvirtual memory
page B1
page B2
page B3
8
10
.
.
.
.
.
.
.
.
1
page table B
1
1
0
4
Operating Systems: Paging/1
7
•Pages shared in memory
–e.g. a text editor used by two processes – each with their own data
editor0
editor0
data A
process Avirtual memory
physical memoryeditor1
editor2
1
6
10
3
0
1
2
3
4
5
6
7
8
9
10
11
editor1
.
.
.
.
.
.
.
.
1
page table A
1
1
1
data B
editor2
editor0
process Bvirtual memory
editor1
editor2
data B
1
10
8
.
.
.
.
.
.
.
.
1
page table B
1
1
1
6
data A
Operating Systems: Paging/1
8
•Benefits of Paging :
–avoids external fragmentation – no unusable holes in memory
–some internal fragmentation
»the last part of the last page in a sequence may be wasted
»more waste the larger the page size
–small page size :
»less internal fragmentation – more efficient memory utilisation
»larger page tables and other kernel tables e.g. free page list
»higher kernel overheads – dealing with more individual pages
–larger page size :
»longer disc transfer times
»hidden internal fragmentation - less of each page may actually be used
»smaller Translation Lookaside Buffer needed
–small pages often grouped together by OSes to make larger effective pages
»presence bit in level 1 page table entries indicates whether level 2 page table present or not
»virtual address partition usually organised to fit a single page table into a page e.g. for a 32-bit machine, partitions of 10 bits, 10 bits and 12 bits:
index partition sizes of 10 bits with 4 bytes per entry gives a page table size of 4kb
Operating Systems: Paging/1
25
–effective access time = ( h*t + (1-h)*(t+2*m) ) + m
–e.g. h = 0.95, m = 100ns, t = 10ns : e.a.t. = 120ns (up from 115ns) h = 0.99, m = 25ns, t = 2ns : e.a.t. = 27.5ns (up from 27.25ns)
–Three and more level Page Tables
»more partitions of the virtual address space:
»likely to be necessary with 64-bit virtual addressing
»page tables at any intermediate level can be paged in and out using presence bits at each level
1st index 2nd index 3rd index 4th index offset
Operating Systems: Paging/1
26
•Inverted Page Tables
–a page table with one entry per page-frame of physical memory
–each entry contains the virtual page number of the page in that frame
»plus a process ID to disambiguate virtual page numbers
–memory manager inserts data into table when allocating page-frames
–table has to be searched for a PID/virtual page number combination
»index of matching position is the page-frame number
–if PID/virtual page number not found, page-fault interrupt triggered
»memory manager may also keep traditional page tables for its own use
PID virtual page number
.
.
.
.
page-frame number
Operating Systems: Paging/1
27
–linear table searching entry by entry will be slow and inefficient
–use Hash Table searching by hardware :
–hashing clashes dealt with by chaining entries with same hash index together
»hash index calculation, PID/page no. comparison and chaining done by hardware
–a TLB is still required
»searched associatively before inverted page table consulted