代码实现时间片轮转调度、优先数调度、最短进程优先、最短剩余时间优先四种调度方法
时间: 2023-09-26 18:08:00 浏览: 110
时间片轮转调度:
```python
class Process:
def __init__(self, name, arrival_time, burst_time):
self.name = name
self.arrival_time = arrival_time
self.burst_time = burst_time
self.remaining_time = burst_time
def execute(self, quantum):
if self.remaining_time > quantum:
self.remaining_time -= quantum
return quantum
else:
time_executed = self.remaining_time
self.remaining_time = 0
return time_executed
def schedule_rr(processes, quantum):
time = 0
num_processes = len(processes)
queue = processes[:]
completed_processes = []
current_process = None
while len(completed_processes) < num_processes:
# add arriving processes to the queue
for p in processes:
if p.arrival_time == time and p not in queue and p not in completed_processes:
queue.append(p)
# get next process to execute
if not current_process or current_process.remaining_time == 0:
if queue:
current_process = queue.pop(0)
else:
time += 1
continue
# execute current process for a quantum
time_executed = current_process.execute(quantum)
time += time_executed
# check if process has completed
if current_process.remaining_time == 0:
completed_processes.append(current_process)
return completed_processes
```
优先数调度:
```python
class Process:
def __init__(self, name, arrival_time, burst_time, priority):
self.name = name
self.arrival_time = arrival_time
self.burst_time = burst_time
self.remaining_time = burst_time
self.priority = priority
def execute(self):
self.remaining_time -= 1
def schedule_priority(processes):
time = 0
num_processes = len(processes)
queue = processes[:]
completed_processes = []
current_process = None
while len(completed_processes) < num_processes:
# add arriving processes to the queue
for p in processes:
if p.arrival_time == time and p not in queue and p not in completed_processes:
queue.append(p)
# get next process to execute
if not current_process or current_process.remaining_time == 0:
if queue:
queue.sort(key=lambda p: p.priority)
current_process = queue.pop(0)
else:
time += 1
continue
# execute current process
current_process.execute()
time += 1
# check if process has completed
if current_process.remaining_time == 0:
completed_processes.append(current_process)
return completed_processes
```
最短进程优先:
```python
class Process:
def __init__(self, name, arrival_time, burst_time):
self.name = name
self.arrival_time = arrival_time
self.burst_time = burst_time
self.remaining_time = burst_time
def execute(self):
self.remaining_time -= 1
def schedule_sjf(processes):
time = 0
num_processes = len(processes)
queue = processes[:]
completed_processes = []
current_process = None
while len(completed_processes) < num_processes:
# add arriving processes to the queue
for p in processes:
if p.arrival_time == time and p not in queue and p not in completed_processes:
queue.append(p)
# get next process to execute
if not current_process or current_process.remaining_time == 0:
if queue:
queue.sort(key=lambda p: p.burst_time)
current_process = queue.pop(0)
else:
time += 1
continue
# execute current process
current_process.execute()
time += 1
# check if process has completed
if current_process.remaining_time == 0:
completed_processes.append(current_process)
return completed_processes
```
最短剩余时间优先:
```python
class Process:
def __init__(self, name, arrival_time, burst_time):
self.name = name
self.arrival_time = arrival_time
self.burst_time = burst_time
self.remaining_time = burst_time
def execute(self):
self.remaining_time -= 1
def schedule_srt(processes):
time = 0
num_processes = len(processes)
queue = processes[:]
completed_processes = []
current_process = None
while len(completed_processes) < num_processes:
# add arriving processes to the queue
for p in processes:
if p.arrival_time == time and p not in queue and p not in completed_processes:
queue.append(p)
# get next process to execute
if not current_process or current_process.remaining_time == 0:
if queue:
queue.sort(key=lambda p: p.remaining_time)
current_process = queue.pop(0)
else:
time += 1
continue
# execute current process
current_process.execute()
time += 1
# check if process has completed
if current_process.remaining_time == 0:
completed_processes.append(current_process)
return completed_processes
```
阅读全文