-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFIFO.py
108 lines (82 loc) · 3.23 KB
/
FIFO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Python3 implementation of FIFO Page Replacement Algorithm in Operating Systems.
import pandas as pd
from queue import Queue
# This is the function used to perform the FIFO Page Replacement Algorithm
def FIFO(pages, n, capacity):
#To store the current frame
#"N/A" represent empty frame
frame = ["N/A"]*capacity
#To store the pages in FIFO manner
indexes = Queue()
#To store the time
time = 0
#To store the output
output = pd.DataFrame(index=range(0,capacity))
# Start from initial page
page_faults = 0
for i in range(n):
# Check if the set can hold more pages
if (frame.count("N/A") > 0):
# Insert it into set if not present
# already which represents page fault
if (pages[i] not in frame):
frame[frame.index("N/A")] = pages[i]
# increment page fault
page_faults += 1
# Push the current page into
# the queue
indexes.put(pages[i])
#Put in the dataframe
output[time] = frame
# If the set is full then need to perform FIFO
# i.e. remove the first page of the queue from
# frame and queue both and insert the current page
else:
# Check if current page is not already present in frames
if (pages[i] not in frame):
# Pop the first page from the queue
val = indexes.queue[0]
indexes.get()
# Replace the indexes page
frame[frame.index(val)] = pages[i]
# push the current page into
# the queue
indexes.put(pages[i])
# Increment page faults
page_faults += 1
#Put in the dataframe
output[time] = frame
else:
temp = ["N/A"]*capacity
temp[frame.index(pages[i])] = "hit"
output[time] = temp
time += 1
print_graph(output, pages,capacity, page_faults)
# This is the function used to print out the simulated graph in a nice way
def print_graph(output, pages,capacity, page_faults):
print("This is the simulated graph using FIFO page replacement algorithm:")
for page in pages:
print(str(page), end = '\t')
print('\n')
for c in range(0,capacity):
for col in output:
if(output[col][c] != "N/A"):
print(output[col][c], end='\t')
else:
print(" ", end='\t')
print('\n')
print("\nNumber of page fault: " + str(page_faults))
# Driver code
if __name__ == '__main__':
print('\n')
print("Input the reference string reference string.\n")
#print("Please input the reference string separated by comma. (e.g. 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1)")
#pages_input = input()
#pages = pages_input.split(',')
#pages = [int(i) for i in pages]
pages = [1,4,5,6,6,7,3,5,4,4,3,7,9,8,7,4]
print(pages)
capacity = 3
capacity = int(input("Please input the number of frame:\n"))
n = len(pages)
FIFO(pages, n, capacity)