python实现进程调度算法代码1、 优先级进程调度算法 2、 时间片轮转进程调度算法
时间: 2024-06-21 20:02:04 浏览: 106
1、优先级进程调度算法的Python实现代码:
```python
class Process:
def __init__(self, name, priority):
self.name = name
self.priority = priority
process_list = [Process('P1', 2), Process('P2', 1), Process('P3', 3), Process('P4', 1)]
def priority_scheduling(process_list):
process_list.sort(key=lambda x: x.priority, reverse=True)
for process in process_list:
print('Running process:', process.name)
priority_scheduling(process_list)
```
2、时间片轮转进程调度算法的Python实现代码:
```python
class Process:
def __init__(self, name, burst_time):
self.name = name
self.burst_time = burst_time
process_list = [Process('P1', 10), Process('P2', 5), Process('P3', 8)]
def round_robin_scheduling(process_list, time_quantum):
ready_queue = process_list[:]
current_time = 0
while ready_queue:
process = ready_queue.pop(0)
if process.burst_time > time_quantum:
process.burst_time -= time_quantum
current_time += time_quantum
ready_queue.append(process)
else:
current_time += process.burst_time
print('Running process:', process.name, 'at', current_time)
round_robin_scheduling(process_list, 3)
```
以上两段代码只是简单的示例,实际应用中还需要考虑进程的状态转换、就绪队列和阻塞队列等问题。
阅读全文