<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#Here we will implement three page replacement algorithms;
#FIFO, OPT, LRU. The memory has frame_num frames available
#and its contents are represented by the list mem_list.
#The reference string is saved in the ref_str list.
#We will implement the LRU seeing the memory as a Stack.
#Fill free to add variables and code at positions that 
#do not have the "implement me" prompt.

#Reference list
ref_str = [0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4, 0, 2, 1, 3, 5, 1, 7, 1, 3, 4, 7, 6, 5, 0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3 , 4]

#memory
mem_list = []

pagefault_counter = 0

#number of available frames in the memory
frame_num = 4

#policy = 'fifo' or 'lru' or 'opt'
policy = 'opt'


def replace(policy, page):
    if policy == 'fifo':
        #implement me :)
    elif policy == 'opt':
        #implement me :)
    elif policy == 'lru':
        #implement me :)

def rearrange(policy, page):
	if policy == 'lru':
		#implement me :)

#main program
#for every memory reference
for i in range(len(ref_str)):
	#check if it is already in the memory
    if not ref_str[i] in mem_list:
		#if it is not, increase the page fault counter
        pagefault_counter += 1
		#check if the memory has free space
        if len(mem_list) &lt; frame_num:
			#if it has, simply insert the page
            mem_list.insert(0, ref_str[i])
        else:
			#else apply the replace algorithm
            replace(policy, ref_str[i])
    else:
		#else rearrange the current pages, if the algorithm needs it 
        rearrange(policy, ref_str[i])
    print (mem_list)

print ('Page faults = ' + str(pagefault_counter);)

# vim:expandtab:tabstop=8:shiftwidth=4:softtabstop=4</pre></body></html>