Vous êtes sur la page 1sur 30

discover yourself

inspire posterity

CHAPTER 4
Memory Management Issues
Lecture 22

22/10/2019 1
discover yourself
inspire posterity

Buddy System
1. In buddy systems memory can be divided into
sections, with each location being a buddy of
another location.
2. Whenever possible the buddies are combined to
create a larger memory location.
3. If smaller memory needs to be allocated the
buddies are divided, and then reunited (if possible)
when the memory is returned.
4. In reality the memory is combined and only
broken down when requested.
discover yourself
inspire posterity
discover yourself
inspire posterity
discover yourself
inspire posterity
discover yourself
inspire posterity
discover yourself
inspire posterity
discover yourself
inspire posterity
discover yourself
inspire posterity
discover yourself
inspire posterity
discover yourself
inspire posterity
Buddy System in 1MB memory
discover yourself
inspire posterity

Sequence of Requests
• Program A requests memory 34K..64K in size
• Program B requests memory 66K..128K in size
• Program C requests memory 35K..64K in size
• Program D requests memory 67K..128K in size
• Program C releases its memory
• Program A releases its memory
• Program B releases its memory
• Program D releases its memory
discover yourself
inspire posterity

If memory is to be allocated
• Look for a memory slot of a suitable size
– If it is found, it is allocated to the program
– If not, it tries to make a suitable memory slot. The system
does so by trying the following:
• Split a free memory slot larger than the requested memory size
into half
• If the lower limit is reached, then allocate that amount of memory
• Go back to step 1 (look for a memory slot of a suitable size)
• Repeat this process until a suitable memory slot is found
discover yourself
inspire posterity
A binary buddy heap before allocation

A binary buddy heap after allocating a 8 KB block

A binary buddy heap after allocating a 10 KB block; note the 6 KB wasted because of
rounding up
discover yourself
inspire posterity

If memory is to be freed
• Free the block of memory
• Look at the neighbouring block - is it free too?
• If it is, combine the two, and go back to step 2 and repeat this
process until either the upper limit is reached (all memory is
freed).
discover yourself
inspire posterity

• Unfortunately with Buddy Systems there can be


significant internal fragmentation.
– Case ‘Program A requests 34k Memory’ – but was assigned
64 bit memory.
• The sequence of block sizes allowed is;
– 1,2,4,8,16…2m
discover yourself
inspire posterity

Page Replacement Algorithms


Decide which memory page to swap out (write to disk) from
memory to allocate a new page to memory

The OS has to choose a page to remove from memory to make room


for the page that has to be swapped in.
discover yourself
inspire posterity

• What if there is no free frame?


• Page replacement –find some page in memory, but not
really in use, swap it out
• In this case, same page may be brought into memory
several times
discover yourself
inspire posterity

Basic Page Replacement


discover yourself
inspire posterity
discover yourself
inspire posterity

PAGE REPLACEMENT ALGORITHMS


1- FIFO Page Replacement Algorithm
2- Optimal Page Replacement Algorithm
3- The Least Recently Used
4- Not Recently/frequently Used
discover yourself
inspire posterity
Page Replacement Algorithms

FIFO Page Replacement Algorithm


1. The simplest page-replacement algorithm is a first-in, first-out
(FIFO) algorithm.
2. The operating system keeps track of all the pages in memory in a
queue, with the most recent arrival at the back, and the earliest
arrival in front.
3. A FIFO replacement algorithm associates with each page the
time when that page was brought into memory.
4. When a page needs to be replaced, the page at the front of the
queue (the oldest page) is selected.
discover yourself
inspire posterity
Page Replacement Algorithms

Treats page frames allocated to a process as a circular


buffer:
When the buffer is full, the oldest page is replaced. Hence
first-in, first-out:

10/22/201
9 23
discover yourself
inspire posterity
FIFO Page Replacement Algorithms

10/22/201
9 24
discover yourself
inspire posterity
Page Replacement Algorithms

1. The first three references (7, 0, 1) cause page faults and


are brought into these empty frames.
2. The next reference (2) replaces page 7, because page 7
was brought in first.
3. Since 0 is the next reference and 0 is already in
memory, we have no fault for this reference.
4. The first reference to 3 results in replacement of page
0, since it is now first in line.
5. Because of this replacement, the next reference, to 0,
will fault.
6. Page 1 is then replaced by page O. This process
continues…

10/22/201
9 25
discover yourself
inspire posterity
Page Replacement Algorithms

EXERCISE-1
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

10/22/201
9 26
discover yourself
inspire posterity

When a page must be replaced, the oldest page is


chosen

• In all our examples, the reference string is


1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• 3 frame (9 page faults)
• 4 frame (10 page faults)

Notice that the number of faults for 4 frames is greater than the number of faults
for 3 frames!! This unexpected result is known as Belady’s anomaly
discover yourself
inspire posterity

EXERCISE-2
Apply FIFO, Using 3 frames
discover yourself
inspire posterity

FIFO Illustrating Belady’s Anomaly


discover yourself
inspire posterity
Modeling Page Replacement Algorithms
Belady's
Anomaly
-It might seem that more
page frames, fewer page
faults a program will get.
- Belady’s discovered
that more page frames
more will be the page
faults.

• FIFO with 3 page frames


• FIFO with 4 page frames
• P's show which page references show page faults
10/22/2019 30

Vous aimerez peut-être aussi