先来先服务调度算法(FCFS):
```python
def fcfs(processes):
"""
First-Come, First-Served (FCFS) scheduling algorithm
Input:
processes: a list of dictionaries containing process information
Each dictionary should have the following keys:
- 'pid': process ID
- 'arrival_time': arrival time of the process
- 'burst_time': burst time of the process
Output:
a list of tuples containing the order in which the processes are executed
Each tuple should have the following format:
(process ID, start time, end time)
"""
# Sort the processes by arrival time
sorted_processes = sorted(processes, key=lambda x: x['arrival_time'])
# Initialize the start and end times of the first process
start_time = sorted_processes[0]['arrival_time']
end_time = start_time + sorted_processes[0]['burst_time']
# Initialize the order list with the first process
order = [(sorted_processes[0]['pid'], start_time, end_time)]
# Loop through the remaining processes and update the start and end times
for i in range(1, len(sorted_processes)):
if sorted_processes[i]['arrival_time'] > end_time:
# If there is a gap between processes, update the start time
start_time = sorted_processes[i]['arrival_time']
else:
# If the next process arrives before the current process finishes,
# update the end time
start_time = end_time
end_time = start_time + sorted_processes[i]['burst_time']
# Add the process to the order list
order.append((sorted_processes[i]['pid'], start_time, end_time))
return order
```
短进程优先调度算法(SJF):
```python
def sjf(processes):
"""
Shortest-Job-First (SJF) scheduling algorithm
Input:
processes: a list of dictionaries containing process information
Each dictionary should have the following keys:
- 'pid': process ID
- 'arrival_time': arrival time of the process
- 'burst_time': burst time of the process
Output:
a list of tuples containing the order in which the processes are executed
Each tuple should have the following format:
(process ID, start time, end time)
"""
# Sort the processes by arrival time and burst time
sorted_processes = sorted(processes, key=lambda x: (x['arrival_time'], x['burst_time']))
# Initialize the start and end times of the first process
start_time = sorted_processes[0]['arrival_time']
end_time = start_time + sorted_processes[0]['burst_time']
# Initialize the order list with the first process
order = [(sorted_processes[0]['pid'], start_time, end_time)]
# Loop through the remaining processes and update the start and end times
for i in range(1, len(sorted_processes)):
if sorted_processes[i]['arrival_time'] > end_time:
# If there is a gap between processes, update the start time
start_time = sorted_processes[i]['arrival_time']
else:
# If the next process arrives before the current process finishes,
# update the end time
start_time = end_time
end_time = start_time + sorted_processes[i]['burst_time']
# Add the process to the order list
order.append((sorted_processes[i]['pid'], start_time, end_time))
return order
```
时间片轮转调度算法(RR):
```python
def rr(processes, quantum):
"""
Round-Robin (RR) scheduling algorithm
Input:
processes: a list of dictionaries containing process information
Each dictionary should have the following keys:
- 'pid': process ID
- 'arrival_time': arrival time of the process
- 'burst_time': burst time of the process
quantum: the time quantum for the algorithm
Output:
a list of tuples containing the order in which the processes are executed
Each tuple should have the following format:
(process ID, start time, end time)
"""
# Sort the processes by arrival time
sorted_processes = sorted(processes, key=lambda x: x['arrival_time'])
# Initialize the start and end times of the first process
start_time = sorted_processes[0]['arrival_time']
end_time = min(start_time + sorted_processes[0]['burst_time'], sorted_processes[1]['arrival_time']) if len(sorted_processes) > 1 else start_time + sorted_processes[0]['burst_time']
# Initialize the order list with the first process
order = [(sorted_processes[0]['pid'], start_time, end_time)]
# Initialize the queue with the remaining processes
queue = sorted_processes[1:]
# Loop through the queue until all processes are executed
while queue:
# Get the next process in the queue
current_process = queue.pop(0)
# If the process has not arrived yet, skip it
if current_process['arrival_time'] > end_time:
queue.append(current_process)
continue
# Calculate the remaining burst time for the current process
remaining_time = current_process['burst_time']
start_time = end_time
# Loop through the time slices until the process finishes
while remaining_time > 0:
# If the time slice is smaller than the remaining time, update the end time
if remaining_time > quantum:
end_time = start_time + quantum
remaining_time -= quantum
else:
end_time = start_time + remaining_time
remaining_time = 0
# Add the process to the order list
order.append((current_process['pid'], start_time, end_time))
# Update the start time for the next time slice
start_time = end_time
# Check if there are any new processes that have arrived
for process in queue:
if process['arrival_time'] <= end_time:
queue.remove(process)
queue.append(current_process)
current_process = process
remaining_time = current_process['burst_time']
break
return order
```
优先级调度算法(抢占式):
```python
def priority_preemptive(processes):
"""
Priority scheduling algorithm (preemptive)
Input:
processes: a list of dictionaries containing process information
Each dictionary should have the following keys:
- 'pid': process ID
- 'arrival_time': arrival time of the process
- 'burst_time': burst time of the process
- 'priority': priority of the process (higher number = higher priority)
Output:
a list of tuples containing the order in which the processes are executed
Each tuple should have the following format:
(process ID, start time, end time)
"""
# Sort the processes by arrival time and priority
sorted_processes = sorted(processes, key=lambda x: (x['arrival_time'], -x['priority']))
# Initialize the start and end times of the first process
start_time = sorted_processes[0]['arrival_time']
end_time = start_time + sorted_processes[0]['burst_time']
# Initialize the order list with the first process
order = [(sorted_processes[0]['pid'], start_time, end_time)]
# Initialize the queue with the remaining processes
queue = sorted_processes[1:]
# Loop through the queue until all processes are executed
while queue:
# Get the highest-priority process in the queue
current_process = max(queue, key=lambda x: x['priority'])
# If the process has not arrived yet, skip it
if current_process['arrival_time'] > end_time:
queue.remove(current_process)
continue
# Calculate the remaining burst time for the current process
remaining_time = current_process['burst_time']
start_time = end_time
# Loop through the remaining processes to check if there is a higher-priority process
for process in queue:
if process['arrival_time'] <= end_time and process['priority'] > current_process['priority']:
# If there is a higher-priority process, preempt the current process
queue.remove(process)
queue.append(current_process)
current_process = process
remaining_time = current_process['burst_time']
break
# Loop through the time slices until the process finishes
while remaining_time > 0:
# If the next process has a higher priority, preempt the current process
if queue and max(queue, key=lambda x: x['priority'])['priority'] > current_process['priority']:
break
# If the time slice is smaller than the remaining time, update the end time
if remaining_time > 1:
end_time += 1
remaining_time -= 1
else:
end_time += 1
remaining_time = 0
# Add the process to the order list
order.append((current_process['pid'], start_time, end_time))
return order
```
高响应比优先调度算法:
```python
def hrrn(processes):
"""
Highest-Response-Ratio-Next (HRRN) scheduling algorithm
Input:
processes: a list of dictionaries containing process information
Each dictionary should have the following keys:
- 'pid': process ID
- 'arrival_time': arrival time of the process
- 'burst_time': burst time of the process
Output:
a list of tuples containing the order in which the processes are executed
Each tuple should have the following format:
(process ID, start time, end time)
"""
# Sort the processes by arrival time
sorted_processes = sorted(processes, key=lambda x: x['arrival_time'])
# Initialize the start and end times of the first process
start_time = sorted_processes[0]['arrival_time']
end_time = start_time + sorted_processes[0]['burst_time']
# Initialize the order list with the first process
order = [(sorted_processes[0]['pid'], start_time, end_time)]
# Initialize the queue with the remaining processes
queue = sorted_processes[1:]
# Loop through the queue until all processes are executed
while queue:
# Calculate the response ratio for each process in the queue
response_ratios = []
for process in queue:
wait_time = max(0, end_time - process['arrival_time'])
response_ratio = (wait_time + process['burst_time']) / process['burst_time']
response_ratios.append(response_ratio)
# Get the process with the highest response ratio
index = response_ratios.index(max(response_ratios))
current_process = queue.pop(index)
# Calculate the remaining burst time for the current process
remaining_time = current_process['burst_time']
start_time = end_time
# Loop through the time slices until the process finishes
while remaining_time > 0:
# If the next process has a higher response ratio, preempt the current process
response_ratios = []
for process in queue:
wait_time = max(0, end_time - process['arrival_time'])
response_ratio = (wait_time + process['burst_time']) / process['burst_time']
response_ratios.append(response_ratio)
if queue and max(response_ratios) > ((end_time - current_process['arrival_time']) / current_process['burst_time']):
break
# If the time slice is smaller than the remaining time, update the end time
if remaining_time > 1:
end_time += 1
remaining_time -= 1
else:
end_time += 1
remaining_time = 0
# Add the process to the order list
order.append((current_process['pid'], start_time, end_time))
return order
```