生活随笔
收集整理的這篇文章主要介紹了
请求分页算法 Python实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
操作系統模擬之請求分頁算法。
文件共1份,代碼如下:
import math
import os
import random
import copydef
alo_opt():print("您選擇了OPT算法,執行結果如下:")print("訪問頁面 物理塊 缺頁中斷")temp_queue
= []sum_changed
= 0for i in
range(page_size
):flag_changed
= False
if len(temp_queue
) < temp_queue_size
and temp_queue
.count(page_queue
[i
]) == 0:temp_queue
.append(page_queue
[i
])elif temp_queue
.count(page_queue
[i
]) == 0:appear_position
= []for j in
range(temp_queue_size
):appear_position
.append(page_size
)for j in
range(i
+ 1, page_size
):if temp_queue
.count(page_queue
[j
]) > 0 and appear_position
[temp_queue
.index(page_queue
[j
])] == page_size
:appear_position
[temp_queue
.index(page_queue
[j
])] = jremotest_position
= appear_position
.index(max(appear_position
))temp_queue
[remotest_position
] = page_queue
[i
]sum_changed
= sum_changed
+ 1flag_changed
= Trueoutput_queue
= copy
.deepcopy(temp_queue
)for j in
range(temp_queue_size
- len(temp_queue
)):output_queue
.append("&")print('{} {} {}'.format(page_queue
[i
], output_queue
, flag_changed
))print()print('頁面訪問序列總長{0},OPT算法共發生缺頁中斷{1}次,缺頁率為{1}/{0}'.format(page_size
, sum_changed
))def
alo_fifo():print("您選擇的是先進先出頁面置換算法")print("訪問頁面 物理塊 缺頁中斷")temp_queue
= []sum_changed
= 0appear_time
= []for i in
range(page_size
+1):appear_time
.append(page_size
)for i in
range(page_size
):flag_changed
= False
if len(temp_queue
) < temp_queue_size
and temp_queue
.count(page_queue
[i
]) == 0:temp_queue
.append(page_queue
[i
])appear_time
[page_queue
[i
]] = ielif temp_queue
.count(page_queue
[i
]) == 0:target_index
= 0min_time
= page_size
for j in
range(0, temp_queue_size
):if appear_time
[temp_queue
[j
]] < min_time
:min_time
= appear_time
[temp_queue
[j
]]target_index
= jappear_time
[temp_queue
[target_index
]] = page_sizetemp_queue
[target_index
] = page_queue
[i
]appear_time
[page_queue
[i
]] = isum_changed
= sum_changed
+ 1flag_changed
= Trueoutput_queue
= copy
.deepcopy(temp_queue
)for j in
range(temp_queue_size
- len(temp_queue
)):output_queue
.append("&")print('{} {} {}'.format(page_queue
[i
], output_queue
, flag_changed
))print()print('頁面訪問序列總長{0},FIFO算法共發生缺頁中斷{1}次,缺頁率為{1}/{0}'.format(page_size
, sum_changed
))def
alo_lru():print("您選擇的是最近最久未使用頁面置換算法")print("訪問頁面 物理塊 缺頁中斷")temp_queue
= []sum_changed
= 0recent_used
= []for i in
range(page_size
+1):recent_used
.append(0)for i in
range(page_size
):flag_changed
= False
if len(temp_queue
) < temp_queue_size
and temp_queue
.count(page_queue
[i
]) == 0:temp_queue
.append(page_queue
[i
])elif temp_queue
.count(page_queue
[i
]) == 0:target_index
= 0min_time
= page_size
for j in
range(0, temp_queue_size
):if recent_used
[temp_queue
[j
]] < min_time
:min_time
= recent_used
[temp_queue
[j
]]target_index
= jtemp_queue
[target_index
] = page_queue
[i
]recent_used
[page_queue
[i
]] = isum_changed
= sum_changed
+ 1flag_changed
= True
else:recent_used
[page_queue
[i
]] = ioutput_queue
= copy
.deepcopy(temp_queue
)for j in
range(temp_queue_size
- len(temp_queue
)):output_queue
.append("&")print('{} {} {}'.format(page_queue
[i
], output_queue
, flag_changed
))print()print('最近最久未使用頁面置換算法缺頁{0}次, 缺頁率為{0}/{1}'.format(sum_changed
, page_size
))def
alo_clock():print("您選擇的是時鐘頁面置換算法")print("訪問頁面 物理塊 缺頁中斷")temp_queue
= []sum_changed
= 0flag
= {}for i in
range(0, page_size
+1):flag
[i
] = Falsepoint
= 0for i in
range(page_size
):flag_changed
= False
if len(temp_queue
) < temp_queue_size
and temp_queue
.count(page_queue
[i
]) == 0:temp_queue
.append(page_queue
[i
])flag
[page_queue
[i
]] = Truepoint
= temp_queue
.index(page_queue
[i
]) + 1elif temp_queue
.count(page_queue
[i
]) == 1:point
= temp_queue
.index(page_queue
[i
])flag
[page_queue
[i
]] = Trueelif temp_queue
.count(page_queue
[i
]) == 0:while True
:if not flag
[temp_queue
[point
% temp_queue_size
]]:temp_queue
[point
% temp_queue_size
] = page_queue
[i
]point
= point
+ 1sum_changed
= sum_changed
+ 1flag_changed
= True
breakelse:flag
[temp_queue
[point
% temp_queue_size
]] = Falsepoint
= point
+ 1output_queue
= copy
.deepcopy(temp_queue
)for j in
range(len(temp_queue
)):if flag
[temp_queue
[j
]]:output_queue
[j
] = str(temp_queue
[j
]) + "*"for j in
range(temp_queue_size
- len(temp_queue
)):output_queue
.append("&")print('{} {} {}'.format(page_queue
[i
], output_queue
, flag_changed
))print()print('時鐘頁面置換算法缺頁{0}次, 缺頁率為{0}/{1}'.format(sum_changed
, page_size
))print("歡迎進入操作系統課程演示系統之頁面置換算法!")
print()flag_exit
= False
while True
:if flag_exit
== True
:print("您已成功退出系統!")breakprint("請輸入頁面訪問序列長度(15-25,含端點):")while True
:page_size
= int(input())if page_size
>= 15 and page_size
<= 25:breakprint("您輸入的頁面訪問序列長度超出給定范圍,請重新輸入15-25(含端點)的數字:")print("請輸入物理塊個數(3-5,含端點):")while True
:temp_queue_size
= int(input())if temp_queue_size
>= 3 and temp_queue_size
<= 5:breakprint("您輸入的物理塊個數超出給定范圍,請重新輸入3-5(含端點)的數字:")print()page_queue
= []print("選擇用戶輸入請輸入1")print("選擇隨機生成請輸入2")data_produce_choice
= int(input())if data_produce_choice
== 2:for i in
range(page_size
):page_queue
.append(random
.randint(0, 5))else:while True
:print('請輸入0-5數字組成的無規律字符串作為頁面訪問序列,長度為{}'.format(page_size
))flag_digit
= Truetemp
= input().split(" ")key
= ["0", "1", "2", "3", "4", "5"]for i in temp
:if key
.count(i
) == 0:flag_digit
= False
if not flag_digit
:print("您輸入的序列有誤,請重新輸入")elif
len(temp
) < page_size
:print('您已輸入{}長度的字符串,還需輸入{}長度的字符串'.format(len(temp
), page_size
- len(temp
)))print('請繼續輸入0-5數字組成的無規律字符串作為頁面訪問序列,長度為{}'.format(page_size
- len(temp
)))temp_plus
= input().split(" ")if len(temp_plus
) != page_size
- len(temp
):print("您再次輸入錯誤,序列需要全部重新輸入")continuefor i in temp
:page_queue
.append(int(i
))for i in temp_plus
:page_queue
.append(int(i
))elif
len(temp
) > page_size
:print("您輸入的字符串超長,系統將進行自動截取Y或由用戶重新輸入N(Y/N)")if input() == "Y":for i in
range(page_size
):page_queue
.append(int(temp
[i
]))breakelse:continueelse:for i in temp
:page_queue
.append(int(i
))breakprint()print('頁面訪問序列為:{}'.format(page_queue
))print('物理塊個數為:{}'.format(temp_queue_size
))print()while True
:print("選擇最佳置換算法請輸入1")print("選擇先進先出頁面置換算法請輸入2")print("選擇最近最久未使用頁面置換算法請輸入3")print("選擇時鐘頁面置換算法請輸入4")menu_choice
= int(input())if menu_choice
== 1:alo_opt()elif menu_choice
== 2:alo_fifo()elif menu_choice
== 3:alo_lru()elif menu_choice
== 4:alo_clock()else:print("您的算法選擇錯誤,請重新輸入!")print()print("繼續嘗試其他算法請輸入1")print("更換數據請輸入2")print("退出程序請輸入3")end_choice
= int(input())if end_choice
== 2:breakelif end_choice
== 3:flag_exit
= True
總結
以上是生活随笔為你收集整理的请求分页算法 Python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。