Python模拟操作系统进程调度

参考自CSDN:https://blog.csdn.net/qq_40877371/article/details/103753581

侵删

在原作基础上稍有改动,以及仅保留了先来先服务(FCFS)、短作业优先(SJF)、时间片轮转(RR)三种调度算法。

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
'''
参考链接:https://blog.csdn.net/qq_40877371/article/details/103753581
'''

#1. 先来先服务 FCFS
#2. 短作业优先 SJF
#3. 时间片轮转 RR

import copy
import random

class PCB:

def __init__(self, pid, name, priority, in_time, run_time):
self.pid = pid #进程pid
self.name = name #进程名称
self.priority = priority #进程优先级
self.in_time = in_time #进程到达时间
self.run_time = run_time #进程运行时间

self.dy_priority = priority #动态优先级
self.prialter_time = 999 #动态优先级上次修改时间

self.in_times = 0 #进入过cpu的次数
self.remain_time = run_time #工作剩余时间
self.cpu_time = 0 #占用cpu的工作时间
self.start_time = 999 # 进程开始运行时间
self.out_time = 999 # 进程运行结束时间
self.turnaround_time = 999 #周转时间
self.Wturnaround_time = 999 #带权周转时间

def pre_output(self): #运行前进程表
print("PID "+str(self.pid) +"\t"+ "名称:" + self.name +'\t'+"优先级:"+ str(self.priority) +'\t'+"到达时间:"+str(self.in_time) +'\t'+"运行时间:"+ str(self.run_time))

def run_output(self):
print("PID " + str(self.pid) +'\t'+"剩余时间:"+ str(self.remain_time) +'\t'+ "已工作时间:" + str(self.cpu_time))

def fin_output(self):
# turnaround_time = self.out_time - self.in_time #周转时间
# Wturnaround_time = turnaround_time / self.run_time #带权周转时间

print("PID "+ str(self.pid) +"\t"+ "名称:" + self.name +"\t"+ "优先级:" + str(self.priority) +"\t"+ "到达时间:" + str(self.in_time) +"\t"+ "运行时间:" + str(self.cpu_time) +"\t"+ "开始时间:" + str(self.start_time) +"\t"+ "结束时间:" + str(self.out_time) +"\t"+ "周转时间:" + str(self.turnaround_time)+ "\t"+ "带权周转时间:" + str(self.Wturnaround_time))


def FCFS(pcb_list):
time = 0
finish_list = [] #完成后的进程组
while pcb_list:
print("-"*20)
print("当前时间:", time)
if time >= pcb_list[0].in_time:
if pcb_list[0].in_times == 0:
print("进程" + str(pcb_list[0].pid) + " 开始运行")
pcb_list[0].start_time = time
pcb_list[0].in_times += 1
pcb_list[0].cpu_time += 1
pcb_list[0].run_output()
if pcb_list[0].run_time == pcb_list[0].cpu_time:
print("进程" + str(pcb_list[0].pid) + " 运行结束")
pcb_list[0].out_time = time + 1
pcb_list[0].turnaround_time = pcb_list[0].out_time - pcb_list[0].in_time
pcb_list[0].Wturnaround_time = round(pcb_list[0].turnaround_time / pcb_list[0].run_time, 2)
finish_list.append(pcb_list[0])
pcb_list.remove(pcb_list[0])
time += 1
'''
for i in finish_list:
AvgTurnaround_time += i.turnaround_time
AvgWTurnaround_time += i.Wturnaround_time
'''
return finish_list


def SJF(pcb_list):
time = 0
finish_list = [] #完成后的进程组
min_time = 0
is_run = False

def ListSort_SJF(sortlist):
ready_num = 0
for i in sortlist:
if time >= i.in_time:
ready_num += 1
if i.run_time < min_time:
temp = i
sortlist.remove(i)
sortlist.insert(0, temp)
if ready_num > 1:#防止到达时间偏后的排列在前
for j in range(ready_num - 1):
for k in range(j+1, ready_num):
if pcb_list[j].run_time > pcb_list[k].run_time:
pcb_list[j].run_time, pcb_list[k].run_time = pcb_list[k].run_time, pcb_list[j].run_time
return sortlist


while pcb_list:
print("-"*20)
print("当前时间:", time)
if is_run == False:
pcb_list = ListSort_SJF(pcb_list)
if time >= pcb_list[0].in_time:
is_run = True
if pcb_list[0].in_times == 0:
print("进程" + str(pcb_list[0].pid) + " 开始运行")
pcb_list[0].start_time = time
pcb_list[0].in_times += 1
pcb_list[0].cpu_time += 1
pcb_list[0].run_output()
if pcb_list[0].run_time == pcb_list[0].cpu_time:
print("进程" + str(pcb_list[0].pid) + " 运行结束")
pcb_list[0].out_time = time + 1
pcb_list[0].turnaround_time = pcb_list[0].out_time - pcb_list[0].in_time
pcb_list[0].Wturnaround_time = round(pcb_list[0].turnaround_time / pcb_list[0].run_time, 2)
finish_list.append(pcb_list[0])
pcb_list.remove(pcb_list[0])
is_run = False
time += 1

return finish_list


def RR(pcb_list):
time = 0
finish_list =[]
remove_time = pcb_list[0].in_time - 1

def ListSort_RR(sortlist):
ready_num = 0
pcb_newlist = []

for i in sortlist:
if time >= i.in_time:
ready_num += 1
if i.in_times == 0:
pcb_newlist.append(i.pid)
if ready_num > 1:
if time - 1 != remove_time:
temp = sortlist.pop(0)
sortlist.insert(ready_num-1 ,temp)
if pcb_newlist:
pcb_newlist.clear()
return sortlist

while pcb_list:
print("-"*20)
print("当前时间:", time)
pcb_list = ListSort_RR(pcb_list)
if time >= pcb_list[0].in_time:
if pcb_list[0].in_times == 0:
print("进程" + str(pcb_list[0].pid) + " 开始运行")
pcb_list[0].start_time = time
pcb_list[0].in_times += 1
pcb_list[0].cpu_time += 1
pcb_list[0].remain_time -= 1
if pcb_list[0].dy_priority:
pcb_list[0].dy_priority -= 1
pcb_list[0].prialter_time = time
pcb_list[0].run_output()
if pcb_list[0].remain_time == 0:
print("进程" + str(pcb_list[0].pid) + " 运行结束")
pcb_list[0].out_time = time + 1
pcb_list[0].turnaround_time = pcb_list[0].out_time - pcb_list[0].in_time
pcb_list[0].Wturnaround_time = round(pcb_list[0].turnaround_time / pcb_list[0].run_time, 2)
finish_list.append(pcb_list[0])
pcb_list.remove(pcb_list[0])
remove_time = time
time += 1

return finish_list


def ListSort(pcb_list):
for i in range(len(pcb_list)-1):
for j in range(i+1, len(pcb_list)):
if pcb_list[i].pid > pcb_list[j].pid:
pcb_list[i],pcb_list[j] = pcb_list[j],pcb_list[i]
for i in pcb_list:
if i.start_time == 999:
if int(i.pid)==0: print("-"*20)
i.pre_output()
else:
if int(i.pid)==0: print("-"*20)
i.fin_output()
return pcb_list


def NewList_random(num):
pcb_list = []
name_list = ["分配主存空间", "分配磁带机", "分配打印机", "用户输入数据", "刷新信息"]
for i in range(num):
# self, pid, name, priority, in_time, run_time
pcb_list.append(PCB(str(i), name_list[random.randint(0, 4)], random.randint(1, 9), random.randint(0, 10), random.randint(1, 9)))
for i in range(len(pcb_list)-1):
for j in range(i+1, len(pcb_list)):
if pcb_list[i].in_time > pcb_list[j].in_time:
pcb_list[i],pcb_list[j] = pcb_list[j],pcb_list[i]
return pcb_list


def NewList_record(num):
pcb_list = []
#pid, name, priority, in_time, run_time
for i in range(num):
name = input("请输入%d号进程的名称:" % i)
priority = int(input("请输入%d号进程的优先级:" % i))
in_time = int(input("请输入%d号进程的到达时间:" % i))
run_time = int(input("请输入%d号进程的运行时间:" % i))
pcb_list.append(PCB(str(i), name, priority, in_time, run_time))
for i in range(len(pcb_list) - 1):
for j in range(i + 1, len(pcb_list)):
if pcb_list[i].in_time > pcb_list[j].in_time:
pcb_list[i], pcb_list[j] = pcb_list[j], pcb_list[i]
return pcb_list


def ScheduleSlect(list1):
templist = copy.deepcopy(list1)

def avg_output(list):
AvgTurnaround_time = 0
AvgWTurnaround_time = 0
for i in list:
AvgTurnaround_time += i.turnaround_time
AvgWTurnaround_time += i.Wturnaround_time
print("平均周转时间: "+ str(round(AvgTurnaround_time / len(list), 2))+"\t"+"平均带权周转时间: "+ str(round(AvgWTurnaround_time / len(list), 2)))

while True:
list1 = copy.deepcopy(templist)
print("-"*20)
sch_num = input("1. 先来先服务 FCFS\n2. 短作业优先 SJF\n3. 时间片轮转 RR\n4. 重新输入数据\n请选择调度算法:")
if sch_num == "1":
list2 = copy.deepcopy(list1)
ListSort(list2)
list1 = FCFS(list1)
ListSort(list1)
avg_output(list1)
elif sch_num == "2":
list2 = copy.deepcopy(list1)
ListSort(list2)
list1 = SJF(list1)
ListSort(list1)
avg_output(list1)
elif sch_num == "3":
list2 = copy.deepcopy(list1)
ListSort(list2)
list1 = RR(list1)
ListSort(list1)
avg_output(list1)
elif sch_num == '4':
break
else:
print("输入错误,请重新输入!")


if __name__ == "__main__":
while True:
pcb_num = int(input("请输入进程个数(输入0退出):"))
if pcb_num < 1 or pcb_num > 999:
print("请输入正确的数据!")
break
input_type = int(input("1、随机生成\n2、手工输入\n请选择数据的输入方式:"))
if input_type == 1:
list1 = NewList_random(pcb_num)
ScheduleSlect(list1)
elif input_type == 2:
list1 = NewList_record(pcb_num)
ScheduleSlect(list1)
else:
print("输入有误,请重新输入!")

Hello World //写在全站开头

之前用过WordPress和Typecho,但苦于国内域名备案,最后选择Github Pages + Hexo.

1
printf("Hello Hexo!");
1
print("Hello Hexo!")