Professional Documents
Culture Documents
Operating System 2nd Edition - Topik 10
Operating System 2nd Edition - Topik 10
System
Tim Pengampu MK Sistem Operasi
Universitas Dian Nuswantoro
Replacement Algorithm
• When all processes in main memory are blocked, the OS must
choose which process to replace
• Downsides
• Programmer had to take responsibility for splitting the program into
logical sections
• Time consuming, boring and open to error
Virtual Memory
• Virtual Memory
• Idea is that that the computer is able to run programs even if the amount
of physical memory is not sufficient to allow the program and all its data
to reside in memory at the same time
• At the most basic level we can run a 500K program on a 256K machine
• We can also use virtual memory in a multiprogramming environment. We
can run twelve programs in a machine that could, without virtual memory,
only run four
Virtual Memory
Physical Memory Swapping Disk
• Data Structure
• Frame Page
• Table Page User Page
• Empty Frame Page
List Table Page
• Modified Empty
Empty Frame
Frame Page List Page List Swap Area
• Replacement
Algorithm Table Modified Empty
Frame Page List
• Swap Area
Replacement
Algorithm Table
Paging System
0K – 4K 2 0K – 4K
4K – 8K 1 4K – 8K
8K – 12K 6 8K – 12K
12K – 16K 0 12K – 16K
16K – 20K 4 16K – 20K
20K – 24K 3 20K – 24K
24K – 28K X 24K – 28K
28K – 32K X 28K – 32K
32K – 36K X
Page Frame
36K – 40K 5
40K – 44K X
44K – 48K 7
48K – 52K X
52K – 56K X Virtual Page
56K – 60K X
60K – 64K X
• If a virtual memory address is not on a page boundary (as in the above
example) then the MMU also has to calculate an offset (in fact, there is
always an offset – in the above example it was zero)
• We have not really achieved anything yet as, in effect, we have eight virtual
pages which do not map to a physical page
• Each virtual page will have a present/absent bit which indicates if the virtual
page is mapped to a physical page
What happens if we try to use an unmapped page? For example,
the program tries to access address 24576 (i.e. 24K)
• The MMU will notice that the page is unmapped and will
cause a trap to the operating system
• This trap is called a page fault
• The operating system will decide to evict one of the
currently mapped pages and use that for the page that has
just been referenced
• The page that has just been referenced is copied (from disc)
to the virtual page that has just been freed.
• The virtual page frames are updated.
• The trapped instruction is restarted.
• Example (trying to access address 24576)
• The MMU would cause a trap to the operating system as the virtual page
is not mapped to a physical location
• A virtual page that is mapped is elected for eviction (we’ll assume that
page 11 is nominated)
• Virtual page 11 is mark as unmapped (i.e. the present/absent bit is
changed)
• Physical page 7 is written to disc (we’ll assume for now that this needs to
be done). That is the physical page that virtual page 11 maps onto
• Virtual page 6 is loaded to physical address 28672 (28K)
• The entry for virtual page 6 is changed so that the present/absent bit is
changed. Also the ‘X’ is replaced by a ‘7’ so that it points to the correct
physical page
• When the trapped instruction is re-executed it will now work correctly
• How the MMU Works
Incoming Address
0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 0
0 010 1 Present/Absent
1 001 1 Bits
2 110 1
3 000 1
4 100 1
5 011 1
6 000 0
7 000 0
8 000 0
9 101 1
10 000 0
11 111 1
12 000 0
13 000 0
14 000 0
15 000 0
0 1 1 0 0 0 1 0 1 0 1 0 0 1 0
Outgoing Address
Page Replacement Algorithms
• In fact, we can implement it, but only after running the program to see
which pages we should evict at what point
• We can then use this as a measure to see how other algorithms perform
against this ideal.
Page Replacement Algorithms
4 5
4 4 3
• FIFO Replacement manifests Belady’s Anomaly:
• more frames more page faults
FIFO Illustrating Belady’s Anomaly
• The Second Chance Page Replacement Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 1 1 1 5
2 2 2 2 2
8 page faults
3 5 5 4 4
4 4 3 3 3
Comparison of OPT with LRU
Example: A process of 5 pages with an OS that fixes
the resident set size to 3.
Comparison of FIFO with LRU
1 0 0 0 0 1 0 1 1 1 0 0 1 1 0 0 0 1 0 0 0
2 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 0 1 1 0 1
3 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0
3 1 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0
• When a page fault occurs, the counter with the lowest value
is removed
Exercise (Page Replacement)
Anything to discuss ?