我不打算延伸这个,将直奔主题,这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
def fifo_page_replacement(num_frames, reference_string):
frame_list = [] # List to store the currently allocated page frames
fault_count = 0 # Counter for page faults
for page in reference_string:
if page not in frame_list:
fault_count += 1
if len(frame_list) == num_frames:
# Remove the oldest page from the page frames
frame_list.pop(0)
# Add the new page to the page frames
frame_list.append(page)
return fault_count
# Sample data
# Sample data
num_frames = 3 # Number of page frames to test
reference_string = [
1,
2,
3,
4,
2,
1,
5,
6,
2,
1,
2,
3,
7,
6,
3,
2,
1,
2,
3,
6,
] # Reference string
# Generate x-axis values
x_values_1 = np.arange(1, num_frames + 1)
x_values_2 = np.arange(1, num_frames + 2)
x_values_3 = np.arange(1, num_frames + 3)
# Calculate page faults using FIFO algorithm for the given number of frames
page_faults_1 = [fifo_page_replacement(frame, reference_string) for frame in x_values_1]
page_faults_2 = [fifo_page_replacement(frame, reference_string) for frame in x_values_2]
page_faults_3 = [fifo_page_replacement(frame, reference_string) for frame in x_values_3]
# Plotting
plt.plot(x_values_1, page_faults_1, label="Line 1", marker="o")
plt.plot(x_values_2, page_faults_2, label="Line 2", marker="o")
plt.plot(x_values_3, page_faults_3, label="Line 3", marker="o")
plt.xlabel("Number of Page Frames")
plt.ylabel("Number of Page Faults")
plt.title("FIFO Page Replacement Algorithm")
plt.grid(True)
plt.legend()
plt.show()
我想要一个三线图,基本上三个不同的图。fifo_paging_algorithm
按预期返回值。另外,我不希望三个不同的图,只需要一个图。
我期待着上述结果。
1条答案
按热度按时间0md85ypi1#
你的代码工作正常,你的数据是坏的,因此你只得到第3行,它隐藏了第3行的子集。如果你给予
matplotlib
不同的数据,你的代码就可以正常工作。更改代码以生成以下数据:
在一个图中生成所需的3条线: