(1)加深對處理機調度的做用和工做原理的理解。算法
(2)進一步認識併發執行的實質。併發
2、實驗要求:
本實驗要求用高級語言,模擬在單處理器狀況下,採用多個調度算法,對N個進程進行進程調度。語言自選。app
並完成實驗報告。性能
3、實驗內容:spa
在採用多道程序設計的系統中,每每有若干個進程同時處於就緒狀態。設計
當就緒狀態進程個數大於處理器數時,就必須依照某種策略來決定哪些進程優先佔用處理器。code
- 進程及進程隊列的表示。
- 處理器調度算法:FCFS,SJF,RR,HRRN,MLFQ等
- 跟蹤進程狀態的轉化
- 輸出:系統中進程的調度次序,計算CPU利用率,平均週轉時間和平均帶權週轉時間
4、實驗過程與結果blog
1.先來先服務調度算法(FCFS)排序
1.1算法思想隊列
該算法採用非剝奪策略,算法按照進程提交或進程變爲就緒狀態的前後次序,分派 CPU。當前進程佔用CPU,直到執行完或阻塞,纔出讓CPU(非搶佔方式)。在進程喚醒後(如I/O 完成),並不當即恢復執行,一般等到當前進程出讓CPU。這是最簡單的調度算法,比較有利於長進程,而不利於短進程,有利於CPU 繁忙的進程,而不利於I/O 繁忙的進程。
1.2算法設計
1.3算法實現代碼
class Process: def __init__ (self,name,arrive_time,serve_time): self.name=name self.arrive_time=arrive_time #進入進程順序 self.serve_time=serve_time #服務時間 self.finish_time=0 #完成時間 self.cycling_time=0 #週轉時間 self.w_cycling_time=0 #帶權週轉時間 process_list=[] running_time=0 A = Process('A',5,4) B = Process('B',4,3) C = Process('C',3,4) D = Process('D',1,2) E = Process('E',2,4) process_list.append(A) process_list.append(B) process_list.append(C) process_list.append(D) process_list.append(E) i=int (0) p=process_list[i] y=len(process_list) print("進程進行排序") for j in range(len(process_list)-1): for k in range(len(process_list)-1): if process_list[k].arrive_time>process_list[k+1].arrive_time: text=process_list[k+1] process_list[k+1]=process_list[k] process_list[k]=text print("進程號 到達順序 服務時間") for p in process_list: print(p.name, "\t" ,p.arrive_time,"\t" ,p.serve_time) for p in process_list: running_time +=p.serve_time p.finish_time=running_time p.cycling_time=p.finish_time-p.arrive_time p.w_cycling_time=p.cycling_time/p.serve_time print("進程號 到達時間 完成時間 週轉時間 帶權週轉時間") for p in process_list: print(p.name ,"\t\t",p.arrive_time,"\t\t\t\t",p.finish_time,"\t\t",p.cycling_time,"\t",p.w_cycling_time)
1.4運行結果
2.短做業優先算法(SJF)
2.1算法思想
該算法也採用非剝奪策略,對預計執行時間短的進程優先分派處理機。一般後來的短進程不搶先正在執行的進程。相比FCFS 算法,該算法可改善平均週轉時間和平均帶權週轉時間,縮短進程的等待時間,提升系統的吞吐量。缺點是對長進程很是不利,可能長時間得不到執行,且未能依據進程的緊迫程度來劃分執行的優先級,以及難以準確估計進程的執行時間,從而影響調度性能。
2.2算法設計
2.算法實現代碼
class Process: def __init__ (self,name,arrive_time,serve_time): self.name=name self.arrive_time=arrive_time #進入進程順序 self.serve_time=serve_time #服務時間 self.finish_time=0 #完成時間 self.cycling_time=0 #週轉時間 self.w_cycling_time=0 #帶權週轉時間 process_list=[] running_time=0 A = Process('A',5,4) B = Process('B',4,3) C = Process('C',3,4) D = Process('D',1,2) E = Process('E',2,4) process_list.append(A) process_list.append(B) process_list.append(C) process_list.append(D) process_list.append(E) i=int (0) p=process_list[i] y=len(process_list) print("進程進行排序") for j in range(len(process_list)-1): for k in range(len(process_list)-1): if process_list[k].serve_time>process_list[k+1].serve_time: text=process_list[k+1] process_list[k+1]=process_list[k] process_list[k]=text print("進程調度順序 服務時間") for p in process_list: print(p.name, "\t\t" ,p.serve_time) for p in process_list: running_time +=p.serve_time p.finish_time=running_time p.cycling_time=p.finish_time p.w_cycling_time=p.cycling_time/p.serve_time print("進程號 服務時間 週轉時間 帶權週轉時間") for p in process_list: print(p.name ,"\t\t",p.serve_time,"\t\t",p.cycling_time,"\t",p.w_cycling_time)
2.4運行結果
3.輪轉調度算法(RR)
3.1 算法思想:
CPU時間劃分爲時間片,例如100ms
時間片調度:調度程序每次把CPU分配給就緒隊列首進程使用一個時間片,就緒隊列中的每一個進程輪流地運行一個時間片。當這個時間片結束時,強迫一個進程讓出處理器,讓它排列到就緒隊列的尾部,等候下一輪調度
3.2 算法設計:(採用描述或程序流程圖)
Ø進程排序
Ø隊列不爲空時循環:
Ø到達?
Ø剩餘服務時間>時間片
Ø運行時間
Ø剩餘服務時間
Ø剩餘服務時間<=時間片
Ø運行時間
Ø剩餘服務時間、完成時間、週轉時間、加權週轉時間
Ø保存
Ø從隊列刪除進程
3.3 算法實現代碼
class Process: def __init__ (self,name,arrive_time,serve_time): self.name=name self.arrive_time=arrive_time self.serve_time=serve_time #服務時間 self.left_serve_time=serve_time #剩餘服務時間 self.finish_time=0 #完成時間 self.cycling_time=0 #週轉時間 self.w_cycling_time=0 #帶權週轉時間 process_list=[] A = Process('A',0,4) B = Process('B',1,3) C = Process('C',2,4) D = Process('D',3,2) E = Process('E',4,4) process_list.append(A) process_list.append(B) process_list.append(C) process_list.append(D) process_list.append(E) print("進程號 到達時間 服務時間") for p in process_list: print(p.name, "\t" ,p.arrive_time,"\t" ,p.serve_time) index=0 running_time=int (0) q=2 pf=[] #記錄完成的進程 while len(process_list) >0: p=process_list[index] if p.arrive_time>running_time: running_time=p.arrive_time if p.left_serve_time>q: print(p.name,p.left_serve_time) running_time+=q p.left_serve_time-=q else: print("進程號 剩餘服務時間") print(p.name,"\t",p.left_serve_time) running_time +=p.left_serve_time p.left_serve_time=0 p.finish_time=running_time p.cycling_time=p.finish_time-p.arrive_time p.w_cycling_time=p.cycling_time/p.serve_time print("-進程號 到達時間 服務時間 剩餘服務時間") print('-',p.name,"\t",p.arrive_time,"\t",p.serve_time,"\t",p.left_serve_time ) print("\n") pf.append(p) process_list.remove(p) index -=1 index +=1 if index >=len(process_list): index=0 print("進程號 到達時間 剩餘服務時間 完成時間 週轉時間 帶權週轉時間") for p in pf: print(p.name ,"\t",p.arrive_time,"\t\t",p.left_serve_time,"\t",p.finish_time,"\t",p.cycling_time,"\t",p.w_cycling_time)
3.4 運行結果