INTRODUCTION
TO
OPERATING SYSTEMS
Lecture 12: MEMORY MANAGEMENT (3)
CHRIS STAFF
Dept. of Computer Science and Artificial Intelligence
University of Malta
Lecture Outline
Aims and Objectives
Allocating Memory
Allocation Policies
Placement Strategies
Replacement Strategies
Fetch Policies
Aims and Objectives
- Memory Allocation Policies
- Placement
- Fetch
- Replacement
- Working Set Model
Allocating Memory
Issues
- What happens when a page or segment needs to be retrieved from secondary
storage and placed in PM?
- What happens if there are no free page frames in a paged system?
- What if there is no free space in a segmented system?
- What if there is enough overall space to accommodate a segment, but the space
is fragmented?
Allocation Policies
- Need to tackle issues identified above
- Three categories
- Replacement - to choose a page to move out of PM
- Fetch - whether pages / segments should be loaded on
demand or in advance
- Placement - where in PM to place the page / segment
Placement Strategies
Placement Strategies for Non-Paged Systems
- After some time, space in PM becomes unevenly distributed. Many unequally
sized holes. Need to find a hole to accommodate incoming segment.
- Four common strategies
Best Fit
- Holes are held in a list of increasing sizes,
x1 <= x2 ... <= xn
- Segment placed in smallest hole possible
- Can leave many small holes
Worst Fit
- Holes listed in decreasing size
- Segment placed in largest hole available
- Will leave large holes that can be filled later
First Fit
- Holes listed in increasing Base Address
- First hole that can hold segment is used
- Tends to group segments together towards the start of memory
- No overhead of sorting blocks of free space
Buddy
- Segment sizes are integer powers of 2
- Free Segments of the same size are held in separate lists
- Incoming segment size is rounded up to next integer power of 2
- If a free block of that size is available, then it is allocated and removed
from the free list.
- Otherwise, next available larger segment is split into two buddies of equal
size. The first half is allocated, the second is added to the appropriate free
list.
- When a block is freed, the state of its buddy is checked. If the buddy is
free, they are recombined to form a larger free block, and added to the
appropriate list.
- Compaction is used to reduce external fragmentation. All free blocks
are moved down in memory to form a larger free block.
Placement Strategies for Paged Systems
- All page frames are the same size
- An incoming page can be placed in any free page frame
Replacement Strategies
Replacement Strategies for Paged Systems
- When there is no room in PM, a page needs to be swapped out
- The best to swap out is the page which is not going to be referenced for
longest
- OS doesn't know the future, so an approximation algorithm must be used
Least Recently Used (LRU)
- Based on heuristic that the page that has not been used for longest will not
be used in the near future
- List maintained where each time a page is referenced, it is moved to the head
of the list. Last page in list is least recently used page, and can be
replaced
- But OS is unpredictable
Least Frequently Used (LFU)
- Keeps a count of the number of times a page has been referenced.
- Page that has been referenced least will be replaced
- What about a page that has just been swapped in?
First In First Out (FIFO)
- Replace the page that has been resident longest, but this could be the most
heavily used page
Not Used Recently (NUR)
- Improvement on LRU, with hardware support
- Whenever a page is referenced, a reference bit is set to 1.
- A modified bit (dirty bit) is set to 1 if the page has been modified. If a
page has been modified, then replacing it means writing it to secondary
storage. An unmodified page does not have to be written to SS
- Following order of priority used to select page to replace
1. Unreferenced / unmodified
2. Unreferenced / modified
3. Referenced / unmodified
4. Referenced / modified
- 2 looks strange, but periodically referenced bit is reset to zero
Replacement Strategies for Non-Paged Systems
- Same as for Paged Systems, but segments occupy different amounts of space.
- Best segment to replace, is the segment that with surrounding free space is
large enough to accommodate the incoming segment and is the best to replace
according to the selected strategy
Fetch Policies
- Apply to both Paged/Non-Paged Systems
Fetch on Demand
- Easiest to implement
- Only fetch a page/segment if it is referenced
- Process is unrunnable until block is in memory
- Process may need to reference several pages before it can continue, but first
page could be replaced before last page is fetched. (Doesn't apply to
segments)
Anticipatory Fetch
- OS predicts what blocks a process is likely to refer to
+ Process doesn't always need to be suspended
- If 100% correct, process would never be suspended, an OS would give
impression of infinite memory
- Behaviour of process is unpredictable, so OS could pre-load redundant
blocks
Anticipating Future Behaviour
The Working Set Model
- The pages that a process needs to work effectively
- If the working set is not in PM, process will display Thrashing
- As the Degree of Multiprogramming increases, resource utilisation increases.
If the DofM increases beyond the number of working sets that can be comfortably
accommodated in memory, then OS performance degrades
Working out which pages form the Working Set
- The working set of a process W(t,w) at time t, is the set of pages referenced
during the process time interval t-w to t. w, the working set window
size, is the distance into the past that the algorithm is to look.
- The window size can be small, because there are almost the same pages in the
working set over a short or a long time period.
- Working sets change as the process executes. Usually, changes are minor, but
occasionally, changes are dramatic
- The General Rule is that a process should not be made runnable until its
working set is in PM, and a page that belongs to a process' working set should
not be replaced in PM.
Next Lecture...
The Filing System (1)