Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me start this assignment Dy lhl piogiall. Beeause the job dictates t

ID: 3698530 • Letter: P

Question

Please help me start this assignment Dy lhl piogiall. Beeause the job dictates the page order, this order can't be changed by the system, although the size of main memory can be changed. However, buying more memory may not always be the best solution-especially when you have many users and each one wants an unlimited amount of memory. In other words, as explained later in this chapter, there is no guarantee that buying more memory will always result in better performance. cestet even though it may be used the most often and has to be shortly swapped back in. (figure 3.8) Page Requested: A Using a FIFO policy this page trace analysis shows how each page requested is swapped into the two available page frames. When the program is ready to be rocessed, all four pages are in secondary stor age. When the program calls a page that isn't already in memory, a age interrupt is issued, as shown by the gray xes and asterisks. This ogram resulted in nine Frame 1 Frame 2 (empty) Interrupt: 2 3 4 5 6 7 89 10 11 page interrupts. Time Snapshot: 1

Explanation / Answer

Please find the python code which has the two algorithms implemented. I have ensured that the program will prompt the user to give inputs(Reference string, number of frames and which algorithm to run).

Python 2.7 code starts from next line. Write to me if you have any problem.

a = []

#Function to accept reference string and frame size.
def accept():
global a,n,m
n = input(" Enter the size of reference string : ")
print "PLEASE ENTER ONE BY ONE THE PAGES ASKED BY THE PROGRAM"
for i in range(n):
a.append(input(" Enter [%2d] : " % (i+1)))
m = input(" Enter page frame size : ")

#First In First Out Page Replacement Algorithm
def __fifo():
global a,n,m
f = -1
page_faults = 0
page = []
for i in range(m):
page.append(-1)

for i in range(n):
flag = 0
for j in range(m):
if(page[j] == a[i]):
flag = 1
break

if flag == 0:
f=(f+1)%m
page[f] = a[i]
page_faults+=1
print " %d ->" % (a[i]),
for j in range(m):
if page[j] != -1:
print page[j],
else:
print "-",
else:
print " %d -> No Page Fault" % (a[i]),
  
print " Total page faults : %d." % (page_faults)

#Least Recently Used Page Replacement Algorithm
def __lru():
global a,n,m
x = 0
page_faults = 0
page = []
for i in range(m):
page.append(-1)

for i in range(n):
flag = 0
for j in range(m):
if(page[j] == a[i]):
flag = 1
break
  
if flag == 0:
if page[x] != -1:
min = 999
for k in range(m):
flag = 0
j = i
while j>=0:
j-=1
if(page[k] == a[j]):
flag = 1
break
if (flag == 1 and min > j):
min = j
x = k

page[x] = a[i]
x=(x+1)%m
page_faults+=1
print " %d ->" % (a[i]),
for j in range(m):
if page[j] != -1:
print page[j],
else:
print "-",
else:
print " %d -> No Page Fault" % (a[i]),
  
print " Total page faults : %d." % (page_faults)

#Optimal Page Replacement Algorithm
def __optimal():
global a,n,m
x = 0
page_faults = 0
page = []
for i in range(m):
page.append(-1)

for i in range(n):
flag = 0
for j in range(m):
if(page[j] == a[i]):
flag = 1
break
  
if flag == 0:
if page[x] != -1:
max = -1
for k in range(m):
flag = 0
j = i
while j<n:
j+=1
if(page[k] == a[j]):
flag = 1
break
if (flag == 1 and min < j):
max = j
x = k

page[x] = a[i]
x=(x+1)%m
page_faults+=1
print " %d ->" % (a[i]),
for j in range(m):
if page[j] != -1:
print page[j],
else:
print "-",
else:
print " %d -> No Page Fault" % (a[i]),
  
print " Total page faults : %d." % (page_faults)

  

#Displaying the menu and calling the functions.   
while True:
print " ****************STARTING THE SIMULATION: PLEASE GIVE INPUT TO THE PROGRAM************************ "
accept()
print " SIMULATION OF PAGE REPLACEMENT ALGORITHM"
print " Menu:"
print " 1. FIFO."
print " 2. LRU."
print " 3. Optimal."
print " 4. Exit."
  
ch = input(" Select : ")
if ch == 1:
__fifo()
if ch == 2:
__lru()
if ch == 3:
__optimal()
if ch == 4:
break