操作系统相关实验报告(整理在这里)
处理机调度算法
1)实验内容
设计有关的数据结构与各个功能模块,实现处理机调度算法中的动态优先权算法。画出程序的流程图,编写正确的程序。
2)实验要求
1. 详细描述实验设计思想、程序结构及各模块设计思路;
2. 详细描述程序所用数据结构及算法;
3. 明确给出测试用例和实验结果;
4. 为增加程序可读性,在程序中进行适当注释说明;
5. 认真进行实验总结,包括:设计中遇到的问题、解决方法与收获等;
6. 实验报告撰写要求结构清晰、描述准确逻辑性强;
7. 实验过程中,同学之间可以进行讨论互相提高,但绝对禁止抄袭。
3) 实验方案
- 每一个进程用一个进程控制块表示。
2.输入每个进程的“优先数”和“要求运行时间”。
3.为了调度方便,将五个进程按给定的优先数从大到小连成就绪队列。用一单元指出队列首进程,用指针指出队列的连接情况。
4.处理机调度总是选队首进程运行。采用动态优先数算法,进程每运行一次优先数就减“1”,同时将运行时间减“1”。
5.若某进程运行时间为零,则将其状态置为“结束”,且退出队列。
6.运行所设计程序,显示或打印逐次被选中进程的进程名,以及进程控制块的动态变化过程。
7.进行图形化页面显示
流程图如下:
4) 实验步骤:
class Counter(object):
count = 0
##定义PCB类,其中包括优先级和服务时间
class ProcessPCB(Counter):
def init(self, priority, service_time):
Counter.count += 1
self.pid = Counter.count
self.priority = priority
self.service_time = service_time
def set_priority(self, _priority):
self.priority = _priority
def set_service_time(self, _service_time):
self.service_time = _service_time
##定义运行函数:即运行一次服务时间减少1,优先级减少1
def run(self):
self.service_time -= 1
self.priority -= 1
##将所有的PCB放在一个二维列表里面,方便排序和调度
class ProcessPCBList(object):
def init(self):
self.pcb_list = []
self.count = 0
##PCB排序,其中包括PCB优先级的排序(优先级高的先运行)还有相同优先级情况下根据到达时间排序
def sort(self):
self.pcb_list = sorted(self.pcb_list, key=lambda pcb: pcb[0].priority, reverse=True)
for i in range(len(self.pcb_list) - 1):
if self.pcb_list[i][0].priority == self.pcb_list[i + 1][0].priority:
if self.pcb_list[i][1] < self.pcb_list[i + 1][1]:
temp = self.pcb_list[i]
self.pcb_list[i] = self.pcb_list[i + 1]
self.pcb_list[i + 1] = temp
def put(self, pcb):
self.pcb_list.append([pcb, self.count])
self.count += 1
self.sort()
def get(self):
result = self.pcb_list[0]
self.pcb_list.remove(result)
self.sort()
return result[0]
def empty(self):
if len(self.pcb_list) > 0:
return False
return
###输出每一个时间点的运行状态
def dynamic_priority(pcb_list):
while pcb_list.empty() is False:
temp_pcb = pcb_list.get()
print(’---------------------------------------------------------------------’)
print(‘running:’)
print(“PID: %d Priority: %d Service_time: %d” % (temp_pcb.pid, temp_pcb.priority, temp_pcb.service_time))
print(‘others:’)
for i in pcb_list.pcb_list:
print(“PID: %d Priority: %d Service_time: %d counter: %d” %
(i[0].pid, i[0].priority, i[0].service_time, i[1]))
print(’---------------------------------------------------------------------’)
temp_pcb.run()
if temp_pcb.service_time != 0:
pcb_list.put(temp_pcb)
##主函数调用以上函数
def main():
a = ProcessPCB(4, 9)
b = ProcessPCB(3, 8)
c = ProcessPCB(2, 6)
d = ProcessPCB(1, 7)
pcb_list = ProcessPCBList()
pcb_list.put(a)
pcb_list.put(b)
pcb_list.put©
pcb_list.put(d)
dynamic_priority(pcb_list)
if name == ‘main’:
main()
部分结果如下图:
###图形化界面的实现(利用tkinter)
##导入所需要的库
import time
import tkinter as tk
import Pcb
class Window(tk.Tk):
def init(self):
super().init()
self.pcb_list = Pcb.ProcessPCBList()
self.setup_ui()
def setup_ui(self):
self.title('Process Scheduling')
self.geometry('600x400')
self.label_now_running = tk.Label(self, text='Now Running PCB')
self.label_others = tk.Label(self, text='Other PCBs')
self.priority_label = tk.Label(self, text='Priority')
self.service_time_label = tk.Label(self, text='Service Time')
self.value1 = tk.StringVar()
self.now_label = tk.Label(self, bg='green', textvariable=self.value1, width=46)
self.priority_entry = tk.Entry(self, width=46)
self.service_time_entry = tk.Entry(self, width=46)
self.value2 = tk.StringVar()
self.other_list_box = tk.Listbox(self, height=7, width=46)
self.add_button = tk.Button(self, text='Add', command=self.add)
self.run_button = tk.Button(self, text='Run', command=self.run)
self.label_now_running.place(x=30, y=30)
self.label_others.place(x=30, y=80)
self.now_label.place(x=150, y=30)
self.priority_label.place(x=30, y=240)
self.service_time_label.place(x=30, y=280)
self.priority_entry.place(x=150, y=240)
self.service_time_entry.place(x=150, y=280)
self.other_list_box.place(x=150, y=80)
self.add_button.place(x=510, y=275)
self.run_button.place(x=510, y=75)
def add(self):
v1 = int(self.priority_entry.get())
v2 = int(self.service_time_entry.get())
temp_pcb = Pcb.ProcessPCB(v1, v2)
self.pcb_list.put(temp_pcb)
pcb_string = 'PID: {:<10} Priority: {:<10} Service Time: {:<10}'\
.format(temp_pcb.pid, temp_pcb.priority, temp_pcb.service_time)
self.other_list_box.insert('end', pcb_string)
def run(self):
self.dynamic_priority()
def dynamic_priority(self):
while self.pcb_list.empty() is False:
time.sleep(1)
self.update()
temp_pcb = self.pcb_list.get()
self.value1.set('PID: {:<10} Priority: {:<10} Service Time: {:<10}'
.format(temp_pcb.pid, temp_pcb.priority, temp_pcb.service_time))
self.other_list_box.delete(0, last=len(self.pcb_list.pcb_list))
for each in self.pcb_list.pcb_list:
self.other_list_box.insert('end', 'PID: {:<10} Priority: {:<10} Service Time: {:<10}'
.format(each[0].pid, each[0].priority, each[0].service_time))
temp_pcb.run()
if temp_pcb.service_time != 0:
self.pcb_list.put(temp_pcb)
def main():
windows = Window()
windows.mainloop()
if name == ‘main’:
main()
用户可以在窗口上自己初始化PCB信息,如下图一,以及看到正在运行的PCB状态和其他未运行状态PCB信息
5)实验结论与分析:
(结合理论知识总结实验结果,并对其正确性、创新性进行分析以及本次实验心得体会)
在多道程序设计中,通常是若干个进程处于就绪状态,必须依照某种策略来决定哪种进程优先占有处理机,因而引出进程调度。通过此次试验,进一步理解了处理机调度中的动态优先权算法,加深了对进程调度一些具体细节的理解。