Sei sulla pagina 1di 12

11: Paging

Implementation and
Segmentation

Mark Handley

Page Size

Small page size:


 Advantages
 Less internal fragmentation (on average, half of last
page is unused)
 Better fit for various data structures, code sections
 Less unused program in memory
 Disadvantages
 Programs need many pages, larger page tables

1
Page Size
Overhead due to size of page table and internal
fragmentation:

where page table wasted space due


s = average process size size to fragmentation
 p = page size
 e = size of page entry in page table

This has a minimum when:

Separate Instruction and Data Spaces

One address space Separate I and D spaces

2
Shared Instruction Pages

 Two processes sharing same program share its instruction page


table, but not its data page table.

Sharing Data Pages


 Sharing program text pages is easy if I and D spaces are
separate.

 Sharing program data pages is also possible:


 Eg: fork() system call in Unix.

 After fork(), both program and data pages are shared, but
data pages become read-only.

 When a write occurs, a protection trap occurs, and the OS


copies the page, to create two identical read-write pages,
and updates the page tables accordingly.

 This is known as copy-on-write.

3
Cleaning Policy
 For good performance, you’d like there to always be a few free
pages ready to handle page faults.

 Use a background paging daemon process, which periodically


inspects state of memory:
 When too few frames are free, it selects pages to evict using a
replacement algorithm.

 Schedules write to disk of modified pages to be evicted, so


they’re clean when page fault occurs.

 Can use same circular list (clock) as regular page replacement


algorithm, but with an additional pointer.

Implementation Issues
Operating System Involvement with Paging
Four times when OS is involved with paging:
1. Process creation:
− Determine program size.

− Create page table.

2. Process execution (context switch):


− MMU reset for new process

− TLB flushed.

3. Page fault time:


− Determine which virtual address caused the fault.

− Swap target page out, swap needed page in.

4. Process termination time:


− Release process’s page table and pages.

4
Process Creation
 Need to get process into memory and stored on paging area of
disk.
 Can use copy-on-write when forking a process.

 On Unix, exec() replaces current executing program with


another one.
 Can page the program text directly from the program file
on disk (file is memory-mapped).
 Don’t need to load any of it to start running it! Just let a
page fault cause each page to be loaded as it’s needed.
Known as demand-paging.

Page Fault Handling


1. Faulting instruction causes MMU to trap to OS.
2. Save general registers and program state
3. Determines which virtual page is needed
4. Check validity of address and page protection bits, seek page
frame to replace.
5. If selected frame is dirty, write it to disk.
[Allocate CPU to another process while this completes.]
6. Start transfer of page from disk to empty frame.
[Allocate CPU to another process while this completes.]
7. Update page table to show page is now in memory.
8. Back up faulting instruction to state when it began.
9. Schedule faulting process
10. Restore registers, process state, and re-run faulting instruction.

5
Instruction Backup

An instruction causing a page fault.


 Did the page fault occur on the instruction load, the first
operand, or the second operand?
 Made even worse by auto-increment instructions (Eg: j = ++i)

Locking Pages in Memory

 Virtual memory and I/O operations may interact.

 Process issues call to read from a device into a buffer in the


process’s memory space.
 While waiting for I/O, another processes is run, and has a
page fault.
 Buffer for the first process might be chosen to be paged
out.

 Need to specify some pages as locked in memory.


 They are temporarily exempted from being paged out.

6
Backing Store

(a) Paging to static swap area


(b) Backing up pages dynamically

Segmentation
 Programmer’s view of memory is not usually as a single linear
address space:

main
thread
program
stack
thread library
function heap
stack

symbol library
table function

 Programmer doesn’t know how large these will be, or how


they’ll grow, and doesn’t want to manage where they go in
virtual memory.

7
Example:
Compiler with One-dimensional Address Space

Example:
Compiler with Segmentation

 Segmentation maintains multiple separate virtual address


spaces per process.
 Allows each table to grow or shrink, independently.

8
Segmentation Hardware
segment
s table

limit base

physical
y address
CPU
CPU ss dd << ++ Memory
Memory
logical n
address
trap

Segmentation vs Paging
Similarity:
 Address space can exceed size of real memory.
Differences:
 Programmer is aware of segmentation. Paging is hidden.
 Segmentation maintains multiple address spaces per
process. Paging maintains one address space.
 Segmentation allows procedures and data to be separately
protected. This is hard with paging.
 Segmentation easily permits tables whose size varies.
 Segmentation facilitates sharing of procedures between
processes. This is hard with paging.
 Pure segmentation suffers from memory fragmentation.

9
Segmentation with Paging
Segmentation and Paging can be used together.

 Programmer is aware of segments.


 Gains all the protection and sharing benefits.

 Within each segment, paging is used.


 Avoids external memory fragmentation
 Uses memory efficiently.

 Intel x86 processors since the 386 combine both.

Segmentation with Paging: Pentium

 To select a segment, program loads a selector for that segment


into one of six segment registers
 CS register: selector for code segment
 DS register: selector for data segment
 Selector can specify whether segment table is Local to the
process, or Global to the machine.

 Format of a selector:
13 bits 1 2
Index

LDT/GDT Privilege Level (0-3)

10
Segmentation with Paging: Pentium

Step 1: Use the Selector to convert the 32 bit virtual offset


address to a 32 bit linear address.
selector
index g p 32
32bit
bitoffset
offset

segment descriptor
limit base ++

32
32bit
bitlinear
linearaddress
address
segment descriptor
table

Segmentation with Paging: Pentium (4)


Step 2: Convert the 32 bit linear address to a physical
address using a two-stage page table.

11
Segment Protection: Pentium

 At any instant a program


is in one protection level.
 Calls to higher levels are
permitted.
 Calls to lower levels must
contain a selector instead
of an address, which
designates a segment
descriptor called a call-
gate, which in turn gives
the address to be called.
 Thus only restricted entry Level
points are permitted.

12

Potrebbero piacerti anche