11 segmentation
TRANSCRIPT
-
8/8/2019 11 Segmentation
1/12
1
11: PagingImplementation andSegmentation
Mark Handley
Page Size
Small page size:
AdvantagesLess internal fragmentation (on average, half of last
page is unused)Better fit for various data structures, code sectionsLess unused program in memory
DisadvantagesPrograms need many pages, larger page tables
-
8/8/2019 11 Segmentation
2/12
2
Overhead due to size of page table and internalfragmentation:
wheres = average process size
p = page sizee = size of page entry in page table
This has a minimum when:
Page Size
page tablesize
page tablesize
wasted space dueto fragmentationwasted space dueto fragmentation
Separate Instruction and Data Spaces
One address space Separate I and D spaces
-
8/8/2019 11 Segmentation
3/12
3
Shared Instruction Pages
Two processes sharing same program share its instruction pagetable, but not its data page table.
Sharing Data Pages
Sharing program text pages is easy if I and D spaces areseparate.
Sharing program data pages is also possible:Eg: fork() system call in Unix.
After fork() , both program and data pages are shared, butdata pages become read-only .
When a write occurs, a protection trap occurs, and the OScopies the page, to create two identical read-write pages,and updates the page tables accordingly.
This is known as copy-on-write .
-
8/8/2019 11 Segmentation
4/12
4
Cleaning Policy
For good performance, youd like there to always be a few free pages ready to handle page faults.
Use a background paging daemon process, which periodicallyinspects state of memory:
When too few frames are free, it selects pages to evict using areplacement algorithm.
Schedules write to disk of modified pages to be evicted, sotheyre clean when page fault occurs.
Can use same circular list (clock) as regular page replacementalgorithm, but with an additional pointer .
Implementation Issues
Operating System Involvement with PagingFour 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 processs page table and pages.
-
8/8/2019 11 Segmentation
5/12
5
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 withanother one.
Can page the program text directly from the program fileon disk (file is memory-mapped ).
Dont need to load any of it to start running it! Just let a page fault cause each page to be loaded as its needed.Known as demand-paging .
Page Fault Handling
1. Faulting instruction causes MMU to trap to OS.2. Save general registers and program state3. Determines which virtual page is needed4. 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 process10. Restore registers, process state, and re-run faulting instruction.
-
8/8/2019 11 Segmentation
6/12
6
Instruction Backup
An instruction causing a page fault.Did the page fault occur on the instruction load, the firstoperand, 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 processs 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 pagedout.
Need to specify some pages as locked in memory .They are temporarily exempted from being paged out.
-
8/8/2019 11 Segmentation
7/12
7
Backing Store
(a) Paging to static swap area(b) Backing up pages dynamically
Segmentation
Programmers view of memory is not usually as a single linear address space:
Programmer doesnt know how large these will be, or howtheyll grow, and doesnt want to manage where they go invirtual memory.
mainprogram
threadstack
threadstack heap
library
function
symbol
table
libraryfunction
-
8/8/2019 11 Segmentation
8/12
8
Example:Compiler with One-dimensional Address Space
Example:
Compiler with Segmentation
Segmentation maintains multiple separate virtual addressspaces per process.Allows each table to grow or shrink, independently.
-
8/8/2019 11 Segmentation
9/12
9
Segmentation Hardware
CPUCPU MemoryMemory++
logicaladdress
physicaladdress
-
8/8/2019 11 Segmentation
10/12
10
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 fragmentationUses memory efficiently.
Intel x86 processors since the 386 combine both.
Segmentation with Paging: Pentium
To select a segment, program loads a selector for that segmentinto one of six segment registers
CS register: selector for code segmentDS 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:
Index
13 bits 1 2
LDT/GDT Privilege Level (0-3)
-
8/8/2019 11 Segmentation
11/12
11
Segmentation with Paging: Pentium
Step 1 : Use the Selector to convert the 32 bit virtual offsetaddress to a 32 bit linear address.
index g p
segment descriptor limit
selector
32 bit offset32 bit offset
32 bit linear address32 bit linear addresssegment descriptor table
base ++
Segmentation with Paging: Pentium (4)
Step 2: Convert the 32 bit linear address to a physicaladdress using a two-stage page table.
-
8/8/2019 11 Segmentation
12/12
12
Segment Protection: Pentium
At any instant a programis in one protection level.Calls to higher levels are
permitted.Calls to lower levels mustcontain a selector insteadof an address, whichdesignates a segmentdescriptor called a call-gate, which in turn gives
the address to be called.Thus only restricted entry
points are permitted.Level