0% found this document useful (0 votes)
2 views

OS

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

OS

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Operating System

Memory Management

Sunday, January 19, 2025 08:50:44 PM


Virtual Memory

• Separation of user logical memory from physical memory.


• Allows for large virtual memory to be provided for programmers
even though physical memory is small.
• Programmers do not have to worry about space and writing
overlay codes.
• Virtual memory implemented using demand paging or demand
segmentation.

Slide 5 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Paging

• Physical memory is broken into fixed-sized blocks called frames.

• Logical memory is also broken into blocks of the same size called
pages.
• When a process is executed, its pages are loaded into any available
memory frame from the backing store.

Slide 6 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Paging

page 0 0 1 0

page 1 1 4 1 page 0

page 2 2 3 2

page 3 3 7 3 page 2
logical memory page table
4 page 1

7 page 3
physical memory

Slide 7 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Demand Paging

• A process resides in secondary memory, usually a


disk.
• When a process needs to be executed, it is paged to
memory.
• The whole process is not paged, only pages that are
needed are brought in.
• A pager decides which pages to bring into memory.
• Advantages:-
• avoid reading pages that will not be used
• decreases paging time
• decreases physical memory needed

Slide 8 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Demand Paging
Memory

Secondary
Storage

Program A Swap Out


0 1 2 3

4 5 6 7

8 9 10 11
Program B
12 13 14 15
Swap In
16 17 18 19

Slide 9 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Demand Segmentation

• What is Segmentation?
• Segmentation is a memory-management scheme that supports a user view
of memory.
• A logical address space is a collection of segments. Each segment has a
name and a length.
• The addresses specify both the segment name and the offset within the
segment.

Slide 10 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Demand Segmentation

• Most efficient virtual memory storage method.


• Needs significant amount of hardware requirements.
• Not all paging systems use segmentation.
• Instead of allocating pages in memory, allocate segments.
• Segment descriptors keeps track of segments.

Slide 11 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Demand Segmentation
Figure 8.24 pg 275 0000

stack
segment 3 1400
Limit Base segment 0
0 1000 1400
symbol 2400
subroutine 1 400 6300
table 3200
2 400 4300
segment 0 segment 4
3 1100 3200 segment 3
4 1000 4700 4300
main segment 2
SQRT Segment table 4700
program
segment 4
segment 1 segment 2
5700

6300
Physical memory 6700 segment 1
Logical address space Slide 12 (of 19)
Sunday, January 19, 2025 08:50:44 PM
Shared Segmentation
0
Limit Base
editor 43062
0 25286 43062
segment 0
1 4425 68348
editor
data 1 segment table process P1
segment 1 68348
data 1
72773
logical memory process P1

editor Limit Base


segment 0 90003
0 25286 43062 data 2
1 8850 90003 98553
data 2 segment table process P2
1298553
segment 1
physical memory
logical memory process P2
Sharing of segments in a segmented memory system
Slide 13 (of 19)
Sunday, January 19, 2025 08:50:44 PM
Quick Review Questions

1.How does demand paging work?


2.How does demand segmentation work?

Slide 14 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Follow Up Assignment

1.What is the difference between demand paging and demand


segmentation?

Slide 16 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Page Fault

• A page fault occurs when:-


• a process tries to use a page that is not in memory.
• an operating system interrupt occurs as a result of trying to access a missing
page.

Slide 5 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Steps in Handling Page Fault
operating 3 locate page on backing store
system

reference
1 2 trap

i backing
load M store
6 page table Free
restart
frame
instruction
5 4
reset bring in
page memory
table page

program
physical
execution
Slide 6 (of 29)
memory
Sunday, January 19, 2025 08:50:44 PM
Page Fault

• Steps in handling a page fault:-


• locate the missing page
• find free memory frame
• copy into physical memory
• reset the page table
• restarts the instructions

Slide 7 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Page Replacement

• What if there are no free frames?


• find a frame that is not currently being used and free it.
• use a page replacement algorithm to find a free frame.

Slide 8 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Page Replacement

• How to free a frame?


• find a frame not currently used.
• copy its contents to disk.
• change the page table to indicate that the page is no longer in memory.
• newly free frame can be used to store the page that resulted in the page
fault.

Slide 9 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Page Replacement

• The string of memory reference is called a reference string.


• For a given page, we need to consider only the page number, not the
entire address.

Slide 10 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Page Replacement

• For example, if we trace a particular process, we might record the


following address sequence:-
0100, 0432, 0101, 0612, 0102, 0103, 0104
0101, 0611, 0102, 0103, 0104, 0101, 0610
0102, 0103, 0104, 0102, 0609, 0102, 0105
• which at 100 bytes per page is reduced to the following reference
string
1, 4, 1, 6, 1, 6, 1, 6, 1, 6, 1

Slide 11 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Page Replacement
frame valid-invalid
bit
swap
2
change to 1out
O i invalid victim
page O
F v 4
F victim
reset page
3 F
table for new
page swap
desired backing
page in store
page table
physical memory
Slide 12 (of 29)
Sunday, January 19, 2025 08:50:44 PM
Page Replacement

• In the previous example page O was previously loaded into memory.


• Page O is now no longer needed.
• Page F needs to be brought into physical memory.
• As page O is no longer needed it becomes the victim page and is
replaced with page F.

Slide 13 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Page Replacement Algorithms

• Every operating system has its own unique page replacement


algorithm.
• In selecting a particular algorithm we want the one with the lowest
page fault rate.
• Types of page replacement algorithms:-
• First In First Out
• Optimal
• Least Recently Used

Slide 14 (of 29)


Sunday, January 19, 2025 08:50:44 PM
First In First Out

• Simplest page replacement algorithm.


• Easily implemented.
• Algorithm uses the age of the page as the
criteria to perform page replacement.
• The age of the page is the amount of time
the page has spent in memory.
• Oldest page is the victim.
• All other pages are lined up in the order of
highest page to be sacrificed.

Slide 15 (of 29)


Sunday, January 19, 2025 08:50:44 PM
First-in-first-out (FIFO)
reference string
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1

Number of Page Faults : 15

Perform an Analysis of the First-In-First-Out (FIFO) Algorithm for the following


string of memory reference addresses. Assume that there are three frames available
within the memory for holding pages.Slide 16 (of 29)
Sunday, January 19, 2025 08:50:44 PM
First-in-first-out (FIFO)

• The disadvantage of this technique is that the oldest page


could be very much in demand.
• When the oldest page is replaced there will be an immediate
page fault.
• FIFO suffers from BELADY’S ANOMALY.

Slide 17 (of 29)


Sunday, January 19, 2025 08:50:44 PM
BELADY’S ANOMALY

• BELADY was one of the pioneers of modern operating systems


who did extensive studies on Page Replacement schemes.
• On the basis of logical thinking, as the number of memory
frames are increased, the number of page faults decreases.
• But BELADY, after conducting extensive experiments, found
that for certain memory reference strings, as the number of
memory frames are increased the number of page faults
increases.
• BELADY’S observations contradicted the commonly accepted
dogma, and instead of ‘burning him on the stake’, his results
were classified as BELADY’S ANOMALY.

Slide 18 (of 29)


Sunday, January 19, 2025 08:50:44 PM
First-in-first-out (FIFO)

16

14

12
number of page faults

10
8

0 1 2 3 4 5 6 7
number of frames
page-fault curve for FIFO replacement on a reference string
Slide 19 (of 29)
Sunday, January 19, 2025 08:50:44 PM
Optimal

• Results from Belady’s anomaly resulted in a search for a new


algorithm.
• An algorithm that provides the lowest page fault with a fixed
number of frames.
• Does not suffer from Belady’s anomaly.
• Replaces the page that will not be used for the longest
period of time.
• Difficult to implement, requires future knowledge of the
reference string.

Slide 20 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Optimal Algorithm

reference string
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 7
0 0 0 0 0 0 4 4 4 0 0 0 0 0 0 0 0 0 0
1 1 1 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1

Replace the page that will not be


Number of Page Faults: 9 used for the longest period of time

Perform an Analysis of the Optimal Algorithm for the following string of memory
reference addresses. Assume that there are three frames available within the
memory for holding pages. Slide 21 (of 29)
Sunday, January 19, 2025 08:50:44 PM
Least Recently Used

• Associates each page with time of when the page was last
used.
• When a page needs to be replaced, the page that has not been
used the for the longest period will be chosen.
• Technique of looking backwards.

Slide 22 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Least Recently Used
reference string
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 4 4 4 0 0 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0
1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 7 7 7

Number of Page Faults: 12

Perform an Analysis of the Least recently used for the following string of memory
reference addresses. Assume that there are three frames available within the
memory for holding pages. Slide 23 (of 29)
Sunday, January 19, 2025 08:50:44 PM
Thrashing

• A condition where there is high page replacement


activity.
• A process spends more time replacing pages than processing.
• Causes CPU utilisation to drop.
• System throughput reduces and page fault rate increases.

Slide 24 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Thrashing
• A process is thrashing if it is spending more
time paging than executing.
CPU Utilisation

degree of multiprogramming
Slide 25 (of 29)
Sunday, January 19, 2025 08:50:44 PM
Question

1. You are given the following memory references:-


1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6
How many page faults would occur for the 3 different
replacement algorithms, assuming the system has various
settings of three, four and five frames?

Slide 26 (of 29)


Sunday, January 19, 2025 08:50:44 PM
Summary of Main Teaching Points

• Virtual memory system is implemented using a


technique called demand paging or demand
segmentation, this allows for a process larger then
the system physical memory to be executed.
• Demand paging only brings in pages i that are needed
into memory.
• Demand segmentation allocated memory in
segments.
• Demand paging and demand segmentation takes the
responsibility of from the programmer to code using
techniques for overlays, fixed or variable partitions.

Slide 17 (of 19)


Sunday, January 19, 2025 08:50:44 PM
Summary of Main Teaching Points

• Page faults occur when a page that is needed is found in memory.


• Page replacements are based on trying to find free frames.
• Page replacement algorithms are used when there exists no free
frames.
• The page replacement algorithms are first in first out, optimal and
least recently used.
• First in first out replaces the oldest page.
• Optimal replaces the page that is least likely to be used.
• Least recently used replaces the page that is has not been used
recently.
• Thrashing occurs when page replacement activities occurs more
then process execution.
Slide 27 (of 29)
Sunday, January 19, 2025 08:50:44 PM
END

Q&A
Sunday, January 19, 2025 08:50:44 PM

You might also like