OS HoaMai 22028141

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#Ex 9.

35
import random
# Constants
PAGES = list(range(10))
FRAMES_RANGE = range(1, 8)
PAGE_REFERENCE_LENGTH = 20
# Generate a random page-reference string where page numbers range from 0 to 9
page_reference_string = [random.choice(PAGES) for _ in
range(PAGE_REFERENCE_LENGTH)]
def fifo(pages, frames):
frame_list = []
page_faults = 0
for page in pages:
if page not in frame_list:
page_faults += 1
if len(frame_list) == frames:
frame_list.pop(0) # Remove the first (oldest) page
frame_list.append(page)
return page_faults
def lru(pages, frames):
frame_list = []
page_faults = 0
for page in pages:
if page not in frame_list:
page_faults += 1
if len(frame_list) == frames:
frame_list.pop(0) # Remove least recently used
frame_list.append(page)
else:
# Move the page to the end to mark it as recently used
frame_list.append(frame_list.pop(frame_list.index(page)))
return page_faults
def optimal(pages, frames):
frame_list = []
page_faults = 0
for i, page in enumerate(pages):
if page not in frame_list:
page_faults += 1
if len(frame_list) < frames:
frame_list.append(page)
else:
# Find the page that will not be used for the longest period of
time
farthest = -1
index_farthest = -1
for f in frame_list:
if f not in pages[i:]:
farthest = f
break
else:
index = pages[i:].index(f)
if index > index_farthest:
index_farthest = index
farthest = f
frame_list[frame_list.index(farthest)] = page
return page_faults
# Apply the random page-reference string to each algorithm and record the number of
page faults
faults = {
'FIFO': [],
'LRU': [],
'Optimal': []
}
for frames in FRAMES_RANGE:
faults['FIFO'].append(fifo(page_reference_string, frames))
faults['LRU'].append(lru(page_reference_string, frames))
faults['Optimal'].append(optimal(page_reference_string, frames))
# Print the page-reference string and the number of page faults for each algorithm
print(f"Page-reference string: {page_reference_string}")
print("Page Faults:")
for algo in faults:
print(f"{algo}: {faults[algo]}")
Page-reference string: [4, 8, 2, 9, 8, 3, 8, 1, 9, 3, 1, 3, 8, 5, 1, 3, 3, 5, 6, 0]
Page Faults:
FIFO: [19, 17, 15, 10, 9, 9, 9]
LRU: [19, 17, 14, 9, 9, 9, 9]
Optimal: [19, 13, 10, 9, 9, 9, 9]
1
2
3
4
5
6
7
8
9
10
11
12
# Ex 9.8
page_reference_string = ['1', '2', '3', '4', '2', '1', '5', '6', '2', '1', '2',
'3', '7', '6', '3', '2', '1', '2', '3',
for frames in FRAMES_RANGE:
faults['FIFO'].append(fifo(page_reference_string, frames))
faults['LRU'].append(lru(page_reference_string, frames))
faults['Optimal'].append(optimal(page_reference_string, frames))
# Print the page-reference string and the number of page faults for each algorithm
print(f"Page-reference string: {page_reference_string}")
print("Page Faults:")
for algo in faults:
print(f"{algo}: {faults[algo]}")
Page-reference string: ['1', '2', '3', '4', '2', '1', '5', '6', '2', '1', '2', '3',
'7', '6', '3', '2', '1', '2', '3',
Page Faults:
FIFO: [19, 17, 15, 10, 9, 9, 9, 20, 18, 16, 14, 10, 10, 7]
LRU: [19, 17, 14, 9, 9, 9, 9, 20, 18, 15, 10, 8, 7, 7]
Optimal: [19, 13, 10, 9, 9, 9, 9, 20, 15, 11, 8, 7, 7, 7]
1
2
3
4
5
6
7
8
9
10
11
12
# Ex 9.21
page_reference_string = ['7', '2', '3', '1', '2', '5', '3', '4', '6', '7', '7',
'1', '0', '5', '4', '6', '2', '3', '0',
for frames in FRAMES_RANGE:
faults['FIFO'].append(fifo(page_reference_string, frames))
faults['LRU'].append(lru(page_reference_string, frames))
faults['Optimal'].append(optimal(page_reference_string, frames))
# Print the page-reference string and the number of page faults for each algorithm
print(f"Page-reference string: {page_reference_string}")
print("Page Faults:")
for algo in faults:
print(f"{algo}: {faults[algo]}")
Page-reference string: ['7', '2', '3', '1', '2', '5', '3', '4', '6', '7', '7', '1',
'0', '5', '4', '6', '2', '3', '0',
Page Faults:
FIFO: [19, 17, 15, 10, 9, 9, 9, 20, 18, 16, 14, 10, 10, 7, 19, 19, 17, 17, 12, 12,
8]
LRU: [19, 17, 14, 9, 9, 9, 9, 20, 18, 15, 10, 8, 7, 7, 19, 19, 18, 17, 17, 14, 10]
Optimal: [19, 13, 10, 9, 9, 9, 9, 20, 15, 11, 8, 7, 7, 7, 19, 16, 13, 11, 10, 9, 8]

You might also like