0% found this document useful (0 votes)
44 views113 pages

06 Memory

The document discusses memory management in operating systems. It covers the goals of memory management which are to subdivide memory for multiple processes and ensure a reasonable supply of ready processes. It describes preparing programs for execution through program transformations, logical to physical address binding, and different memory partitioning schemes like fixed and variable partitions. The key requirements of memory management are relocation, protection, sharing, and the logical versus physical organization of memory. Address binding can be static at programming time or dynamic at execution time. The fundamental memory management problem is how to manage applications larger than available memory through partitioning and loading blocks as needed.

Uploaded by

bunty da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views113 pages

06 Memory

The document discusses memory management in operating systems. It covers the goals of memory management which are to subdivide memory for multiple processes and ensure a reasonable supply of ready processes. It describes preparing programs for execution through program transformations, logical to physical address binding, and different memory partitioning schemes like fixed and variable partitions. The key requirements of memory management are relocation, protection, sharing, and the logical versus physical organization of memory. Address binding can be static at programming time or dynamic at execution time. The fundamental memory management problem is how to manage applications larger than available memory through partitioning and loading blocks as needed.

Uploaded by

bunty da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 113

Memory Management

Raju Pandey
Department of Computer Sciences
University of California, Davis
Spring 2011
Overview
• Goals of memory management:
ƒ Subdividing memory to accommodate multiple processes
ƒ Memory needs to be allocated to ensure a reasonable supply of
ready processes to consume available processor time
• Preparing a Program for Execution
ƒ Program Transformations
ƒ Logical-to-Physical Address Binding
• Memory Partitioning Schemes
ƒ Fixed Partitions
ƒ Variable Partitions
• Allocation Strategies for Variable Partitions
• Dealing with Insufficient Memory

ECS 150 (Operating Systems) Memory Management, 2


The External View of the Memory Manager

Application
Program

VirtualAlloc()
exec() VMQuery()
shmalloc() sbrk() VirtualLock() VirtualFree()
getrlimit() ZeroMemory()

Memory Mgr

Memory Mgr
Process Mgr

Process Mgr
Device Mgr

Device Mgr
File Mgr

File Mgr
UNIX Windows

Hardware

ECS 150 (Operating Systems) Memory Management, 3


Memory Management Requirements
• Relocation
ƒ Programmer does not know
where the program will be
placed in memory when it is
executed
ƒ While the program is
executing, it may be swapped
to disk and returned to main
memory at a different location
(relocated)
ƒ Memory references must be
translated in the code to
actual physical memory
address

ECS 150 (Operating Systems) Memory Management, 4


Memory Management Requirements
• Protection
ƒ Processes should not be able to reference memory locations in another
process without permission
ƒ Impossible to check absolute addresses at compile time
ƒ Must be checked at run time
ƒ Memory protection requirement must be satisfied by the processor
(hardware) rather than the operating system (software)
o Operating system cannot anticipate all of the memory references a program
will make

• Sharing
ƒ Allow several processes to access the same portion of memory
ƒ Better to allow each process access to the same copy of the program
rather than have their own separate copy

ECS 150 (Operating Systems) Memory Management, 5


Memory Management Requirements
• Logical Organization
ƒ Programs are written in modules
ƒ Modules can be written and compiled independently
ƒ Different degrees of protection given to modules (read-only,
execute-only)
ƒ Share modules among processes

• Physical Organization
ƒ Memory available for a program plus its data may be
insufficient
o Overlaying allows various modules to be assigned the same region
of memory

ƒ Programmer does not know how much space will be available

ECS 150 (Operating Systems) Memory Management, 6


Preparing Program for Execution
• Program Transformations
ƒ Translation (Compilation)
ƒ Linking
ƒ Loading

ECS 150 (Operating Systems) Memory Management, 7


A Sample Code Segment

...
static int gVar;
...
int proc_a(int arg){
...
gVar = 7;
put_record(gVar);
...
}

ECS 150 (Operating Systems) Memory Management, 8


The Relocatable Object module

Code Segment
Relative
Address Generated Code
0000 ...
... Data Segment
0008 entry proc_a Relative
... Address Generated variable space
0220 load =7, R1 ...
0224 store R1, 0036 0036 [Space for gVar variable]
0228 push 0036 ...
0232 call ‘put_record’ 0049 (last location in the data segment)
...
0400 External reference table
...
0404 ‘put_record’ 0232
...
0500 External definition table
...
0540 ‘proc_a’ 0008
...
0600 (symbol table)
...
0799 (last location in the code segment)

ECS 150 (Operating Systems) Memory Management, 9


The Absolute Program

Code Segment
Relative
Address Generated Code
0000 (Other modules)
...
1008 entry proc_a
Data Segment
...
Relative
1220 load =7, R1
Address Generated variable space
1224 store R1, 0136
...
1228 push 1036
0136 [Space for gVar variable]
1232 call 2334
...
...
1000 (last location in the data segment)
1399 (End of proc_a)
... (Other modules)
2334 entry put_record
...
2670 (optional symbol table)
...
2999 (last location in the code segment)

ECS 150 (Operating Systems) Memory Management, 10


The Program Loaded at Location 4000
Relative
Address Generated Code
0000 (Other process’s programs)
4000 (Other modules)
...
5008 entry proc_a
...
5036 [Space for gVar variable]
...
5220 load =7, R1
5224 store R1, 7136
5228 push 5036
5232 call 6334
...
5399 (End of proc_a)
... (Other modules)
6334 entry put_record
...
6670 (optional symbol table)
...
6999 (last location in the code segment)
7000 (first location in the data segment)
...
7136 [Space for gVar variable]
...
8000 (Other process’s programs)
ECS 150 (Operating Systems) Memory Management, 11
Address Binding

• Assign Physical Addresses = Relocation

• Static binding
ƒ Programming time

ƒ Compilation time

ƒ Linking time

ƒ Loading time

• Dynamic binding
ƒ Execution time

ECS 150 (Operating Systems) Memory Management, 12


Static Address Binding

Static Binding = At Programming, Compilation, Linking,


and/or Loading Time

ECS 150 (Operating Systems) Memory Management, 13


Dynamic Address Binding
Dynamic Binding = At Execution Time

ECS 150 (Operating Systems) Memory Management, 14


Address Binding
• How to implement dynamic binding
ƒ Perform for each address at run time:
pa = address_map(la)

ƒ Simplest form of address_map:


Relocation Register: pa = la + RR

ƒ More general form:


Page/Segment Table

ECS 150 (Operating Systems) Memory Management, 15


Fundamental Memory Management Problem
• How do we manage applications whose size may be larger
than the size of memory available?
ƒ Partition in blocks and load as necessary
• How do we share memory resources among different
processes?
• Achieved by partitioning memory
ƒ Look at several schemes

ECS 150 (Operating Systems) Memory Management, 16


Memory Partitioning Schemes
• Memory sharing schemes:
ƒ Single-program systems: 2 partitions (OS/user)

ƒ Multi-programmed:
o Divide memory into partitions of different sizes

• Fixed partitions: size of partition determined at the time of OS


initialization and cannot be changed
• Limitations of fixed partitions
ƒ Program size limited to largest partition

ƒ Internal fragmentation (unused space within partitions)

• How to assign processes to partitions


ƒ FIFO for each partition: Some partitions may be unused

ƒ Single FIFO: More complex, but more flexible

ƒ Choose the one that fits the best

ECS 150 (Operating Systems) Memory Management, 17


Fixed Partitioning

Fixed partitions:
1 queue per partition vs 1 queue for all partitions

ECS 150 (Operating Systems) Memory Management, 18


Example: Fixed Partitioning

ECS 150 (Operating Systems) Memory Management, 19


Variable Partitions
• Memory not partitioned a priori
• Each request is allocated portion of free space
• Memory = Sequence of variable-size blocks
ƒ Some are occupied, some are free (holes)
ƒ External fragmentation occurs
• Adjacent holes (right, left, or both) must be coalesced to
prevent increasing fragmentation
• Major part of memory management: manage
available partitions

ECS 150 (Operating Systems) Memory Management, 20


Variable Partitions:
Linked List Implementation 1
• All available space tied together through a linked list
• Type/Size tags at the start of each Block
• Holes (must be sorted by physical address)
contain links to predecessor hole and to next hole
• Checking neighbors of released block b (=C below):
ƒ Right neighbor (easy): Use size of b
ƒ Left neighbor (clever): Use sizes to find first hole to b’s right, follow
its predecessor link to first hole on b’s left, and check if it is adjacent
to b.

ECS 150 (Operating Systems) Memory Management, 21


Variable Partitions:
Linked List Implementation 2
• Better solution:
Replicate tags at end of blocks (need not be sorted)
• Checking neighbors of released block b:
ƒ Right neighbor: Use size of b as before
ƒ Left neighbor: Check its (adjacent) type/size tags

ECS 150 (Operating Systems) Memory Management, 22


Bitmap Implementation

• Memory divided into fix-size blocks


• Each block represented by a 0/1 bit in a binary string: the
“bitmap”
• Can be implemented as char or int array
• Operations use bit masks
ƒ Release: B[i] = B[i] & '11011111'
ƒ Allocate: B[i] = B[i] | '11000000'
ƒ Search: Repeatedly, Check left-most bit and
Shift mask right: TEST = B[i] & '10000000'

ECS 150 (Operating Systems) Memory Management, 23


The Buddy System

• Compromise between fixed and variable partitions


• Fixed number of possible hole sizes; typically, 2i
ƒ Each hole can be divided (equally) into 2 buddies.
ƒ Track holes by size on separate lists
• When n bytes requested, find smallest i so that n≤2i
ƒ If hole of this size available, allocate it;
otherwise, consider larger holes.
Recursively split each hole into two buddies
until smallest adequate hole is created
.Allocate it and place other holes on appropriate lists
• On release, recursively coalesce buddies
ƒ Buddy searching for coalescing can be inefficient

ECS 150 (Operating Systems) Memory Management, 24


The Buddy System
• Assume: Memory of 16
allocation units

Sizes: 1, 2, 4, 8, 16
a) 3 blocks allocated
& 3 holes left

b) Block of size 1
allocated

c) Block 12-13 released

ECS 150 (Operating Systems) Memory Management, 25


Allocation Strategies

• Problem: Given a request for n bytes, find hole ≥ n


• Constraints:
ƒ Maximize memory utilization
(minimize “external fragmentation”)
ƒ Minimize search time
• Search Strategies:
ƒ First-fit: Always start at same place. Simplest.
ƒ Next-fit: Resume search. Improves distribution of holes.
ƒ Best-fit: Closest fit. Avoid breaking up large holes.
ƒ Worst-fit: Largest fit. Avoid leaving tiny hole fragments
• First Fit is generally the best choice

ECS 150 (Operating Systems) Memory Management, 26


Dealing with Insufficient Memory
• Memory compaction
ƒ How much and what to move?
• Swapping
ƒ Temporarily move process to disk
ƒ Requires dynamic relocation
• Overlays
ƒ Allow programs large than physical memory
ƒ Programs loaded as needed according to calling structure.

ECS 150 (Operating Systems) Memory Management, 27


Dealing with Insufficient Memory
Memory compaction

Initial Complete Partial Minimal Movement

ECS 150 (Operating Systems) Memory Management, 28


Dealing with Insufficient Memory
Overlays
ƒ Allow programs large than physical memory
ƒ Programs loaded as needed
according to calling structure

ECS 150 (Operating Systems) Memory Management, 29


Paging

• Logical address space of a process can be noncontiguous;


process is allocated physical memory whenever the latter is
available
• Divide physical memory into fixed-sized blocks called
frames (size is power of 2, between 512 bytes and 8192
bytes)
• Divide logical memory into blocks of same size called
pages.
• Keep track of all free frames
• To run a program of size n pages, need to find n free
frames and load program
• Set up a page table to translate logical to physical
addresses
• Internal fragmentation

ECS 150 (Operating Systems) Memory Management, 30


Address Translation Scheme

• Address generated by CPU is divided into:

ƒ Page number (p) – used as an index into a page table


which contains base address of each page in physical
memory

ƒ Page offset (d) – combined with base address to define


the physical memory address that is sent to the
memory unit

ECS 150 (Operating Systems) Memory Management, 31


Paging Example

ECS 150 (Operating Systems) Memory Management, 32


Paging Example

ECS 150 (Operating Systems) Memory Management, 33


Free Frames

Before allocation After allocation

ECS 150 (Operating Systems) Memory Management, 34


Implementation of Page Table

• Page table is kept in main memory


• Page-table base register (PTBR) points to the page
table
• Page-table length register (PRLR) indicates size of
the page table
• In this scheme every data/instruction access
requires two memory accesses. One for the page
table and one for the data/instruction.
• The two memory access problem can be solved by
the use of a special fast-lookup hardware cache
called associative memory or translation look-
aside buffers (TLBs)

ECS 150 (Operating Systems) Memory Management, 35


Associative Memory

• Associative memory – parallel search


Page # Frame #

Address translation (A´, A´´)


ƒ If A´ is in associative register, get frame # out
ƒ Otherwise get frame # from page table in memory

ECS 150 (Operating Systems) Spring 2011 UC Davis


Paging Hardware With TLB

ECS 150 (Operating Systems) Spring 2011 UC Davis


Effective Access Time
• Associative Lookup = ε time unit
• Assume memory cycle time is 1 microsecond
• Hit ratio – percentage of times that a page number is found
in the associative registers; ration related to number of
associative registers
• Hit ratio = α
• Effective Access Time (EAT)
EAT = (1 + ε) α + (2 + ε)(1 – α)
=2+ε–α

ECS 150 (Operating Systems) Spring 2011 UC Davis


Memory Protection

• Memory protection implemented by associating


protection bit with each frame

• Valid-invalid bit attached to each entry in the page


table:
ƒ “valid” indicates that the associated page is in the
process’ logical address space, and is thus a legal page
ƒ “invalid” indicates that the page is not in the process’
logical address space

ECS 150 (Operating Systems) Spring 2011 UC Davis


Valid (v) or Invalid (i) Bit In A Page Table

ECS 150 (Operating Systems) Spring 2011 UC Davis


Page Table Structure

• Hierarchical Paging

• Hashed Page Tables

• Inverted Page Tables

ECS 150 (Operating Systems) Spring 2011 UC Davis


Hierarchical Page Tables

• Break up the logical address space into multiple


page tables

• A simple technique is a two-level page table

ECS 150 (Operating Systems) Spring 2011 UC Davis


Two-Level Paging Example

• A logical address (on 32-bit machine with 4K page size)


is divided into:
ƒ a page number consisting of 20 bits
ƒ a page offset consisting of 12 bits
• Since the page table is paged, the page number is
further divided into:
ƒ a 10-bit page number
ƒ a 10-bit page offset
• Thus, a logical address is as follows:
page number page offset
pi p2 d

10 10 12
where pi is an index into the outer page table, and p2 is the
displacement within the page of the outer page table

ECS 150 (Operating Systems) Spring 2011 UC Davis


Two-Level Page-Table Scheme

ECS 150 (Operating Systems) Spring 2011 UC Davis


Address-Translation Scheme

• Address-translation scheme for a two-level 32-bit


paging architecture

ECS 150 (Operating Systems) Spring 2011 UC Davis


Hashed Page Tables

• Common in address spaces > 32 bits

• The virtual page number is hashed into a page table.


This page table contains a chain of elements hashing
to the same location.

• Virtual page numbers are compared in this chain


searching for a match. If a match is found, the
corresponding physical frame is extracted.

ECS 150 (Operating Systems) Spring 2011 UC Davis


Hashed Page Table

ECS 150 (Operating Systems) Spring 2011 UC Davis


Inverted Page Table

• One entry for each real page of memory


• Entry consists of the virtual address of the page
stored in that real memory location, with information
about the process that owns that page
• Decreases memory needed to store each page table,
but increases time needed to search the table when
a page reference occurs
• Use hash table to limit the search to one — or at
most a few — page-table entries

ECS 150 (Operating Systems) Spring 2011 UC Davis


Inverted Page Table Architecture

ECS 150 (Operating Systems) Spring 2011 UC Davis


Shared Pages

• Shared code
ƒ One copy of read-only (reentrant) code shared among
processes (i.e., text editors, compilers, window
systems).
ƒ Shared code must appear in same location in the
logical address space of all processes

• Private code and data


ƒ Each process keeps a separate copy of the code and
data
ƒ The pages for the private code and data can appear
anywhere in the logical address space

ECS 150 (Operating Systems) Spring 2011 UC Davis


Shared Pages Example

ECS 150 (Operating Systems) Spring 2011 UC Davis


Segmentation

• Memory-management scheme that supports user


view of memory
• A program is a collection of segments. A segment is
a logical unit such as:
main program,
procedure,
function,
method,
object,
local variables, global variables,
common block,
stack,
symbol table, arrays

ECS 150 (Operating Systems) Spring 2011 UC Davis


User’s View of a Program

ECS 150 (Operating Systems) Spring 2011 UC Davis


Logical View of Segmentation

4
1

3 2
4

user space physical memory space

ECS 150 (Operating Systems) Spring 2011 UC Davis


Segmentation Architecture

• Logical address consists of a two tuple:


<segment-number, offset>,
• Segment table – maps two-dimensional physical
addresses; each table entry has:
ƒ base – contains the starting physical address where
the segments reside in memory
ƒ limit – specifies the length of the segment
• Segment-table base register (STBR) points to the
segment table’s location in memory
• Segment-table length register (STLR) indicates
number of segments used by a program;
segment number s is legal if s < STLR

ECS 150 (Operating Systems) Spring 2011 UC Davis


Segmentation Architecture (Cont.)

• Relocation.
ƒ dynamic
ƒ by segment table

• Sharing.
ƒ shared segments
ƒ same segment number

• Allocation.
ƒ first fit/best fit
ƒ external fragmentation

ECS 150 (Operating Systems) Spring 2011 UC Davis


Segmentation Architecture (Cont.)

• Protection. With each entry in segment table


associate:
ƒ validation bit = 0 ⇒ illegal segment
ƒ read/write/execute privileges
• Protection bits associated with segments; code
sharing occurs at segment level
• Since segments vary in length, memory allocation is
a dynamic storage-allocation problem
• A segmentation example is shown in the following
diagram

ECS 150 (Operating Systems) Spring 2011 UC Davis


Segmentation Hardware

ECS 150 (Operating Systems) Spring 2011 UC Davis


Example of Segmentation

ECS 150 (Operating Systems) Spring 2011 UC Davis


Sharing of Segments

ECS 150 (Operating Systems) Spring 2011 UC Davis


Virtual Memory
Virtual Memory
• Background
• Demand Paging
• Process Creation
• Page Replacement
• Allocation of Frames
• Thrashing
• Demand Segmentation
• Operating System Examples

ECS 150 (Operating Systems) Spring 2011 UC Davis


Background
• Virtual memory – separation of user logical memory from
physical memory.
ƒ Only part of the program needs to be in memory for execution.
ƒ Logical address space can therefore be much larger than
physical address space.
ƒ Allows address spaces to be shared by several processes.
ƒ Allows for more efficient process creation.

• Virtual memory can be implemented via:


ƒ Demand paging
ƒ Demand segmentation

ECS 150 (Operating Systems) Spring 2011 UC Davis


Virtual Memory That is Larger Than Physical Memory

ECS 150 (Operating Systems) Spring 2011 UC Davis


Virtual-address Space

ECS 150 (Operating Systems) Spring 2011 UC Davis


Virtual Memory has Many Uses
• It can enable processes to share memory

ECS 150 (Operating Systems) Spring 2011 UC Davis


Demand Paging
• Bring a page into memory only when it is needed
ƒ Less I/O needed
ƒ Less memory needed
ƒ Faster response
ƒ More users

• Page is needed ⇒ reference to it


ƒ invalid reference ⇒ abort
ƒ not-in-memory ⇒ bring to memory

ECS 150 (Operating Systems) Spring 2011 UC Davis


Transfer of a Paged Memory to Contiguous Disk Space

ECS 150 (Operating Systems) Spring 2011 UC Davis


Valid-Invalid Bit
• With each page table entry a valid–invalid bit is
associated
(1 ⇒ in-memory, 0 ⇒ not-in-memory)
• Initially valid–invalid but is set to 0 on all entries
• Example of a page table snapshot:
Frame # valid-invalid bit
1
1
1
1
0
M

0
0
page table

• During address translation, if valid–invalid bit in page


table entry is 0 ⇒ page fault
ECS 150 (Operating Systems) Spring 2011 UC Davis
Page Table When Some Pages Are Not in Main Memory

ECS 150 (Operating Systems) Spring 2011 UC Davis


Page Fault
• If there is ever a reference to a page, first reference will
trap to
OS ⇒ page fault
• OS looks at another table to decide:
ƒ Invalid reference ⇒ abort.
ƒ Just not in memory.
• Get empty frame.
• Swap page into frame.
• Reset tables, validation bit = 1.
• Restart instruction: Least Recently Used
ƒ block move

ƒ auto increment/decrement location

ECS 150 (Operating Systems) Spring 2011 UC Davis


Steps in Handling a Page Fault

ECS 150 (Operating Systems) Spring 2011 UC Davis


What happens if there is no free frame?

• Page replacement – find some page in memory, but not


really in use, swap it out
ƒ algorithm
ƒ performance – want an algorithm which will result in minimum
number of page faults
• Same page may be brought into memory several times

ECS 150 (Operating Systems) Spring 2011 UC Davis


Performance of Demand Paging
• Page Fault Rate 0 ≤ p ≤ 1.0
ƒ if p = 0 no page faults
ƒ if p = 1, every reference is a fault

• Effective Access Time (EAT)


EAT = (1 – p) x memory access
+ p (page fault overhead
+ [swap page out ]
+ swap page in
+ restart overhead)

ECS 150 (Operating Systems) Spring 2011 UC Davis


Demand Paging Example
• Memory access time = 1 microsecond

• 50% of the time the page that is being replaced has been
modified and therefore needs to be swapped out

• Swap Page Time = 10 msec = 10,000 msec


EAT = (1 – p) x 1 + p (15000)
1 + 15000P (in msec)

ECS 150 (Operating Systems) Spring 2011 UC Davis


Process Creation
• Virtual memory allows other benefits during process
creation:

- Copy-on-Write

- Memory-Mapped Files (later)

ECS 150 (Operating Systems) Spring 2011 UC Davis


Copy-on-Write
• Copy-on-Write (COW) allows both parent and child
processes to initially share the same pages in memory

If either process modifies a shared page, only then is the


page copied

• COW allows more efficient process creation as only modified


pages are copied

• Free pages are allocated from a pool of zeroed-out pages

ECS 150 (Operating Systems) Spring 2011 UC Davis


Page Replacement
• Prevent over-allocation of memory by modifying page-fault
service routine to include page replacement

• Use modify (dirty) bit to reduce overhead of page


transfers – only modified pages are written to disk

• Page replacement completes separation between logical


memory and physical memory – large virtual memory can
be provided on a smaller physical memory

ECS 150 (Operating Systems) Spring 2011 UC Davis


Need For Page Replacement
Bring M from Memory -> Replace B

ECS 150 (Operating Systems) Spring 2011 UC Davis


Basic Page Replacement
1. Find the location of the desired page on disk

2. Find a free frame:


- If there is a free frame, use it
- If there is no free frame, use a page replacement
algorithm to select a victim frame

3. Read the desired page into the (newly) free frame. Update
the page and frame tables.

4. Restart the process

ECS 150 (Operating Systems) Spring 2011 UC Davis


Page Replacement

ECS 150 (Operating Systems) Spring 2011 UC Davis


Page Replacement Algorithms
• Want lowest page-fault rate
• Evaluate algorithm by running it on a particular string of
memory references (reference string) and computing the
number of page faults on that string
• In all our examples, the reference string is
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

ECS 150 (Operating Systems) Spring 2011 UC Davis


Graph of Page Faults Versus The Number of Frames

ECS 150 (Operating Systems) Spring 2011 UC Davis


First-In-First-Out (FIFO) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• 3 frames (3 pages can be in memory at a time per
process)
1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
• 4 frames
1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3

• FIFO Replacement – Belady’s Anomaly


ƒ more frames leads to more page faults

ECS 150 (Operating Systems) Spring 2011 UC Davis


FIFO Page Replacement

ECS 150 (Operating Systems) Spring 2011 UC Davis


FIFO Illustrating Belady’s Anomaly

ECS 150 (Operating Systems) Spring 2011 UC Davis


Optimal Algorithm
• Replace page that will not be used for longest period of time
• 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

1 4
2 6 page faults
3

4 5

• How do you know this?


• Used for measuring how well your algorithm performs

ECS 150 (Operating Systems) Spring 2011 UC Davis


Optimal Page Replacement

ECS 150 (Operating Systems) Spring 2011 UC Davis


Least Recently Used (LRU) Algorithm
• Replace the page that has used least recently
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 5
2

3 5 4
4 3
• Counter implementation
ƒ Every page entry has a counter; every time page is referenced through this
entry, copy the clock into the counter
ƒ When a page needs to be changed, look at the counters to determine which are
to change
• Stack implementation – keep a stack of page numbers in a double link
form:
ƒ Page referenced:
o move it to the top
o requires 6 pointers to be changed
ƒ No search for replacement

ECS 150 (Operating Systems) Spring 2011 UC Davis


LRU Page Replacement

ECS 150 (Operating Systems) Spring 2011 UC Davis


Use Of A Stack to Record The Most Recent Page References

ECS 150 (Operating Systems) Spring 2011 UC Davis


LRU Approximation Algorithms
• Reference bit
ƒ With each page associate a bit, initially = 0
ƒ When page is referenced bit set to 1
ƒ Replace the one which is 0 (if one exists). We do not know the
order, however.
• Second chance
ƒ Need reference bit
ƒ Clock replacement
ƒ If page to be replaced (in clock order) has reference bit = 1
then:
o set reference bit 0
o leave page in memory
o replace next page (in clock order), subject to same rules

ECS 150 (Operating Systems) Spring 2011 UC Davis


Second-Chance (clock) Page-Replacement Algorithm

ECS 150 (Operating Systems) Spring 2011 UC Davis


Counting Algorithms
• Keep a counter of the number of references that have been
made to each page

• LFU Algorithm: replaces page with smallest count

• MFU Algorithm: based on the argument that the page with


the smallest count was probably just brought in and has yet
to be used

ECS 150 (Operating Systems) Spring 2011 UC Davis


Allocation of Frames
• Problem:
ƒ Given a set of frames and processes, how does one allocate
frames to pages?
• Each process needs minimum number of pages
• Two major allocation schemes
ƒ fixed allocation
ƒ priority allocation

ECS 150 (Operating Systems) Spring 2011 UC Davis


Fixed Allocation

• Equal allocation – e.g., if 100 frames and 5


processes, give each 20 pages
• Proportional allocation – Allocate according to the
size of process

si = size of process pi
S = ∑ si
m = total number of frames m = 64
s si = 10
ai = allocation for pi = i × m
S s2 = 127
10
a1 = × 64 ≈ 5
137
127
a2 = × 64 ≈ 59
137

ECS 150 (Operating Systems) Spring 2011 UC Davis


Priority Allocation
• Use a proportional allocation scheme using priorities rather
than size

• If process Pi generates a page fault,


ƒ select for replacement one of its frames
ƒ select for replacement a frame from a process with lower
priority number

ECS 150 (Operating Systems) Spring 2011 UC Davis


Global vs. Local Allocation
• Global replacement – process selects a replacement
frame from the set of all frames; one process can take a
frame from another
• Local replacement – each process selects from only its
own set of allocated frames

ECS 150 (Operating Systems) Spring 2011 UC Davis


Thrashing
• If a process does not have “enough” pages, the page-fault
rate is very high. This leads to:
ƒ low CPU utilization
ƒ operating system thinks that it needs to increase the degree of
multiprogramming
ƒ another process added to the system

• Thrashing ≡ a process is busy swapping pages in and out

ECS 150 (Operating Systems) Spring 2011 UC Davis


Thrashing

• Why does paging work?


Locality model
ƒ Process migrates from one locality to another
ƒ Localities may overlap
• Why does thrashing occur?
Σ size of locality > total memory size
ECS 150 (Operating Systems) Spring 2011 UC Davis
Locality In A Memory-Reference Pattern

ECS 150 (Operating Systems) Spring 2011 UC Davis


Working-Set Model
• Δ ≡ working-set window ≡ a fixed number of page references
Example: 10,000 instruction
• WSSi (working set of Process Pi) =
total number of pages referenced in the most recent Δ
(varies in time)
ƒ if Δ too small will not encompass entire locality
ƒ if Δ too large will encompass several localities
ƒ if Δ = ∞ ⇒ will encompass entire program
• D = Σ WSSi ≡ total demand frames
• if D > m ⇒ Thrashing (m = available frames)
• Policy if D > m, then suspend one of the processes

ECS 150 (Operating Systems) Spring 2011 UC Davis


Keeping Track of the Working Set
• Approximate with interval timer + a reference bit
• Example: Δ = 10,000
ƒ Timer interrupts after every 5000 time units
ƒ Keep in memory 2 bits for each page
ƒ Whenever a timer interrupts copy and sets the values of all
reference bits to 0
ƒ If one of the bits in memory = 1 ⇒ page in working set
• Why is this not completely accurate?
• Improvement = 10 bits and interrupt every 1000 time units

ECS 150 (Operating Systems) Spring 2011 UC Davis


Page-Fault Frequency Scheme

• Establish “acceptable” page-fault rate


ƒ If actual rate too low, process loses frame
ƒ If actual rate too high, process gains frame

ECS 150 (Operating Systems) Spring 2011 UC Davis


Memory-Mapped Files
• Memory-mapped file I/O allows file I/O to be treated as
routine memory access by mapping a disk block to a page in
memory

• A file is initially read using demand paging. A page-sized


portion of the file is read from the file system into a physical
page. Subsequent reads/writes to/from the file are treated
as ordinary memory accesses.

• Simplifies file access by treating file I/O through memory


rather than read() write() system calls

• Also allows several processes to map the same file allowing


the pages in memory to be shared

ECS 150 (Operating Systems) Spring 2011 UC Davis


Memory Mapped Files

ECS 150 (Operating Systems) Spring 2011 UC Davis


Other Issues
• Prepaging
ƒ To reduce the large number of page faults that occurs at process
startup
ƒ Prepage all or some of the pages a process will need, before they are
referenced
ƒ But if prepaged pages are unused, I/O and memory was wasted
ƒ Assume s pages are prepaged and α of the pages is used
o Is cost of s * α save pages faults > or < than the cost of prepaging
s * (1- α) unnecessary pages?
o α near zero ⇒ prepaging loses
• Page size selection must take into consideration:
ƒ Fragmentation
o Smaller the size, better the utilization
ƒ table size:
o Smaller the page size, larger the page table size
ƒ I/O overhead
o Larger the page, longer it takes to load the page, however latency time and
seek time dominate the overall time.
ƒ Locality
o Larger page size => lesser # of page faults

ECS 150 (Operating Systems) Spring 2011 UC Davis


Other Issues (Cont.)

• TLB Reach - The amount of memory accessible from the


TLB

• TLB Reach = (TLB Size) X (Page Size)

• Ideally, the working set of each process is stored in the TLB.


Otherwise there is a high degree of page faults.

ECS 150 (Operating Systems) Spring 2011 UC Davis


Other Issues (Cont.)

• Increase the Page Size. This may lead to an increase in


fragmentation as not all applications require a large page
size.

• Provide Multiple Page Sizes. This allows applications that


require larger page sizes the opportunity to use them
without an increase in fragmentation.

ECS 150 (Operating Systems) Spring 2011 UC Davis


Other Issues (Cont.)
• Program structure
ƒ int A[][] = new int[1024][1024];
ƒ Each row is stored in one page
ƒ Program 1 for (j = 0; j < A.length; j++)
for (i = 0; i < A.length; i++)
A[i,j] = 0;
1024 x 1024 page faults

ƒ Program 2 for (i = 0; i < A.length; i++)


for (j = 0; j < A.length; j++)
A[i,j] = 0;

1024 page faults

ECS 150 (Operating Systems) Spring 2011 UC Davis


Other Considerations (Cont.)
• I/O Interlock – Pages must sometimes be locked into
memory

• Consider I/O. Pages that are used for copying a file from a
device must be locked from being selected for eviction by a
page replacement algorithm.

ECS 150 (Operating Systems) Spring 2011 UC Davis


Reason Why Frames Used For I/O Must Be In Memory

ECS 150 (Operating Systems) Spring 2011 UC Davis


Demand Segmentation
• Used when insufficient hardware to implement demand
paging.
• OS/2 allocates memory in segments, which it keeps track of
through segment descriptors
• Segment descriptor contains a valid bit to indicate whether
the segment is currently in memory.
ƒ If segment is in main memory, access continues,
ƒ If not in memory, segment fault.

ECS 150 (Operating Systems) Spring 2011 UC Davis

You might also like