设计一个按下面三种调度算法实现处理机调度的程序python。 1)先来先服务 2)短进程优先或优先权高优先 3)按时间片轮转
时间: 2023-06-24 18:04:41 浏览: 102
下面是三种处理机调度算法的Python实现:
1. 先来先服务调度算法
```python
# 先来先服务调度算法
def fcfs(processes):
# 按照进程到达时间排序
processes.sort(key=lambda x: x[0])
# 记录每个进程的开始时间、完成时间和周转时间
start_time = [0] * len(processes)
end_time = [0] * len(processes)
turnaround_time = [0] * len(processes)
# 计算每个进程的开始时间、完成时间和周转时间
for i in range(len(processes)):
if i == 0:
start_time[i] = processes[i][0]
end_time[i] = start_time[i] + processes[i][1]
else:
start_time[i] = end_time[i-1]
end_time[i] = start_time[i] + processes[i][1]
turnaround_time[i] = end_time[i] - processes[i][0]
# 计算平均周转时间和平均带权周转时间
avg_turnaround_time = sum(turnaround_time) / len(turnaround_time)
avg_weighted_turnaround_time = sum(turnaround_time[i] / processes[i][1] for i in range(len(processes))) / len(processes)
# 输出结果
print("先来先服务调度算法")
print("进程号\t到达时间\t服务时间\t开始时间\t完成时间\t周转时间\t带权周转时间")
for i in range(len(processes)):
print(f"P{i+1}\t{processes[i][0]}\t\t{processes[i][1]}\t\t{start_time[i]}\t\t{end_time[i]}\t\t{turnaround_time[i]}\t\t{turnaround_time[i]/processes[i][1]}")
print(f"平均周转时间:{avg_turnaround_time}")
print(f"平均带权周转时间:{avg_weighted_turnaround_time}")
```
2. 短进程优先或优先权高优先调度算法
```python
# 短进程优先或优先权高优先调度算法
def sjf(processes, priority=False):
# 按照进程到达时间排序
processes.sort(key=lambda x: x[0])
# 记录每个进程的开始时间、完成时间和周转时间
start_time = [0] * len(processes)
end_time = [0] * len(processes)
turnaround_time = [0] * len(processes)
# 当前时间和已经完成的进程数
current_time = 0
completed = 0
# 计算每个进程的开始时间、完成时间和周转时间
while completed != len(processes):
# 选取当前可运行进程中服务时间最短或优先权最高的进程
runnable_processes = [p for p in processes if p[0] <= current_time and p[3] == 0]
if priority:
runnable_processes.sort(key=lambda x: x[2], reverse=True)
else:
runnable_processes.sort(key=lambda x: x[1])
if runnable_processes:
current_process = runnable_processes[0]
current_process_index = processes.index(current_process)
start_time[current_process_index] = current_time
end_time[current_process_index] = current_time + current_process[1]
turnaround_time[current_process_index] = end_time[current_process_index] - current_process[0]
current_time = end_time[current_process_index]
completed += 1
processes[current_process_index][3] = 1
else:
current_time += 1
# 计算平均周转时间和平均带权周转时间
avg_turnaround_time = sum(turnaround_time) / len(turnaround_time)
avg_weighted_turnaround_time = sum(turnaround_time[i] / processes[i][1] for i in range(len(processes))) / len(processes)
# 输出结果
if priority:
print("短进程优先调度算法")
else:
print("优先权高优先调度算法")
print("进程号\t到达时间\t服务时间\t优先权\t开始时间\t完成时间\t周转时间\t带权周转时间")
for i in range(len(processes)):
print(f"P{i+1}\t{processes[i][0]}\t\t{processes[i][1]}\t\t{processes[i][2]}\t\t{start_time[i]}\t\t{end_time[i]}\t\t{turnaround_time[i]}\t\t{turnaround_time[i]/processes[i][1]}")
print(f"平均周转时间:{avg_turnaround_time}")
print(f"平均带权周转时间:{avg_weighted_turnaround_time}")
```
3. 按时间片轮转调度算法
```python
# 按时间片轮转调度算法
def rr(processes, time_slice):
# 按照进程到达时间排序
processes.sort(key=lambda x: x[0])
# 记录每个进程的开始时间、完成时间和周转时间
start_time = [0] * len(processes)
end_time = [0] * len(processes)
turnaround_time = [0] * len(processes)
# 当前时间和已经完成的进程数
current_time = 0
completed = 0
# 计算每个进程的开始时间、完成时间和周转时间
while completed != len(processes):
# 选取当前可运行进程中服务时间最短或优先权最高的进程
runnable_processes = [p for p in processes if p[0] <= current_time and p[3] < p[1]]
if not runnable_processes:
current_time += 1
continue
current_process = runnable_processes[0]
current_process_index = processes.index(current_process)
if current_process[3] == 0:
start_time[current_process_index] = current_time
time_left = current_process[1] - current_process[3]
if time_left <= time_slice:
end_time[current_process_index] = current_time + time_left
current_time = end_time[current_process_index]
turnaround_time[current_process_index] = end_time[current_process_index] - current_process[0]
completed += 1
processes[current_process_index][3] = current_process[1]
else:
end_time[current_process_index] = current_time + time_slice
current_time = end_time[current_process_index]
processes[current_process_index][3] += time_slice
# 将已经完成时间片的进程移到队列尾部
if current_process[3] == current_process[1]:
processes.append(processes.pop(current_process_index))
# 计算平均周转时间和平均带权周转时间
avg_turnaround_time = sum(turnaround_time) / len(turnaround_time)
avg_weighted_turnaround_time = sum(turnaround_time[i] / processes[i][1] for i in range(len(processes))) / len(processes)
# 输出结果
print(f"时间片长度为{time_slice}的轮转调度算法")
print("进程号\t到达时间\t服务时间\t开始时间\t完成时间\t周转时间\t带权周转时间")
for i in range(len(processes)):
print(f"P{i+1}\t{processes[i][0]}\t\t{processes[i][1]}\t\t{start_time[i]}\t\t{end_time[i]}\t\t{turnaround_time[i]}\t\t{turnaround_time[i]/processes[i][1]}")
print(f"平均周转时间:{avg_turnaround_time}")
print(f"平均带权周转时间:{avg_weighted_turnaround_time}")
```
以上是三种处理机调度算法的Python实现,可以根据需要调用不同的函数来进行测试。
阅读全文