2019 Lecture 9-VM
2019 Lecture 9-VM
INFORMATION
TECHNOLOGY
VIRTUAL MEMORY
LECTURE 9 / FIT2100 / SEMESTER 2 2019
WEEK 10
VIRTUAL MEMORY
INTRODUCTION
LEARNING OUTCOMES
• Understand why virtualised memory is useful
• Discuss the differences between paging and segmentation
• Understand how to translate a logical memory address into a
physical address
• Describe the most common page-replacement algorithms.
READING
• Stallings: Chapter 7, Chapter 8.
2
WHAT IS VIRTUAL
MEMORY?
3
LET’S RECAP…
WHAT IS PHYSICAL MEMORY?
LOGICAL ADDRESSING
• Instead of dealing with physical memory addresses directly, the OS
gives programs logical addresses to work with.
• Logical memory addresses are then translated into physical
addresses.
5
BUT WHY?
WHY VIRTUALISE THE ADDRESS SPACE? (1/3)
6
BUT WHY?
WHY VIRTUALISE THE ADDRESS SPACE? (2/3)
WHAT IS SWAPPING?
• Moving chunks of data between main memory and secondary
(disk) storage.
• Process data which is not currently needed can be moved out
to the disk
• Moved back into physical memory when needed
IS IT USEFUL?
• Allows more complete utilisation of main memory
• But secondary storage is very S L O W
• Need to avoid swapping too often.
• Thrashing: a condition where the time spent of swapping data
exceeds time spent executing instructions.
TWO APPROACHES
TO VIRTUAL MEMORY ALLOCATION
PAGING
Memory is partitioned into fixed sized chunks
SEGMENTATION
Memory is managed as multiple segments of different sizes
PAGED VIRTUAL 01010000 01101100
MEMORY
01100101 01100001
01110011 01100101
00100000 01100111
01101001 01110110
01100101 00100000
01100111 01101111
01101111 01100100
00100000 01010011
01000101 01010100
01010101
FRAMES AND PAGES
PAGED VIRTUAL MEMORY
• The OS kernel maintains a page table to keep track of which pages are in which frames.
FRAGMENTATION
PAGED VIRTUAL MEMORY 01001110
01110110
01100101
01100101
01110010 00100000
01100111 01101111
01101110 01101110
01100001 01000111 01001001
INTERNAL FRAGMENTATION 01010110 01000101
• No. All pages are equally sized, so any empty frame in 01110000
01101111
01100101
01110010
01100011
01110011
memory can always be allocated a whole page. 01110011
• ➔ no external fragmentation
01101000 01100101
00100000 01100101
01111000 01100001
01101101
The most significant bits in the logical address are used for the page number. The remaining
bits are an offset from the start of the page (i.e. position inside the page)
The address is structured so the number of offset bits matches the addressable range of the
page size. Different systems may use a different page size.
• It is not possible to specify an offset that goes ‘out of bounds’ of the specified page.
14
ADDRESS TRANSLATION (PAGING)
Change (logical) page number to (physical) frame number → physical address.
15
WHAT IS A PAGE FAULT?
PAGED VIRTUAL MEMORY
WHAT HAPPENS IF A PAGE DOES NOT HAVE A FRAME IN THE PAGE TABLE?
• It means the page does not have a physical address in
memory
‘PAGE FAULT’!
• The requested logical memory address is valid, but the
page is in secondary storage. Must be loaded in to a frame.
17
FIRST IN FIRST OUT (FIFO)
SIMPLEST REPLACEMENT POLICY TO IMPLEMENT
EXAMPLE
REPLACE THE PAGE THAT HAS NOT BEEN REFERENCED FOR THE LONGEST TIME
By the principle of locality, pages that have been referenced recently are likely
to be referenced again in near future.
A lot of overhead to maintain/check the time since last reference for every page.
EXAMPLE
PERFORMANCE
PAGE FAULTS COMPLEXITY
FIFO 9 Simplest
LRU 7 Complex
OPT 6 ‘Impossible’
PAGE REPLACEMENT ALGORITHMS ARE NOT PERFECT
• Page replacement is a trade-off between minimising page faults and
minimising computational complexity.
• Page replacement can happen very often (whenever data is swapped in).
Identifying a page to replace must to be done in as few instructions as
possible while still giving a ‘good enough’ result.
• So that the program’s instructions can be executed instead!
21
SEGMENTED VIRTUAL
MEMORY
22
SEGMENTS
SEGMENTED VIRTUAL MEMORY
WHAT IS A SEGMENT?
• A process is allocated one or more logical segments of memory.
• A segment can be swapped in or out, just like a page.
• Segments can vary in size, up to a defined maximum size.
• There are no fixed ‘frames’ in physical memory: segments are
placed where space is available.
• Swapping out a segment leaves a ‘hole’ where another segment
may fit.
FRAGMENTATION
SEGMENTED VIRTUAL MEMORY
NO INTERNAL FRAGMENTATION
Hole between
• A segment may grow or shrink dynamically depending on segments
required size. There is no ‘wasted’ space within a segment
that cannot be used.
EXTERNAL FRAGMENTATION Hole between
segments
• Can space between segments be wasted? YES.
• A small segment may be swapped out (or a small
process may exit the system).
• The hole left behind may be too small to
reasonably fit another segment
• Even though the space is available, it is not used.
• Compaction is the technique of periodically re-arranging
segments to remove gaps. Computationally expensive.
24
PLACEMENT ALGORITHMS
SEGMENTED VIRTUAL MEMORY
25
LOGICAL ADDRESSES
SEGMENTED VIRTUAL MEMORY
THE MEMORY ADDRESS CONTAINS A SEGMENT NUMBER AND AN OFFSET (Just like paging)
Example:
0000000001100000000000000010001
Seg number 12 Offset 17 bytes
The most significant bits in the logical address are used for the segment number. The
remaining bits are an offset from the start of the segment in physical memory.
Segments can be different sizes. If the offset goes past the end of a segment, the logical
address is invalid.
‘SEGMENTATION FAULT’!!! Process is usually terminated.
26
ADDRESS TRANSLATION (SEGMENTATION)
27
SHARED MEMORY