用python写无优先级算法代码
时间: 2023-10-01 18:06:55 浏览: 93
以下是一个简单的无优先级算法的 Python 代码示例:
```python
def fcfs(processes):
"""First-Come, First-Served (FCFS) scheduling algorithm"""
# Sort processes by arrival time
sorted_processes = sorted(processes, key=lambda p: p.arrival_time)
# Set start time to the arrival time of the first process
start_time = sorted_processes[0].arrival_time
# Initialize the waiting time and turnaround time
waiting_time = 0
turnaround_time = 0
# Loop through the sorted processes
for i, process in enumerate(sorted_processes):
# Calculate the waiting time and turnaround time for the current process
wait = start_time - process.arrival_time
turnaround = wait + process.burst_time
# Update the waiting time and turnaround time
waiting_time += wait
turnaround_time += turnaround
# Update the start time for the next process
start_time += process.burst_time
# Calculate the average waiting time and turnaround time
avg_waiting_time = waiting_time / len(processes)
avg_turnaround_time = turnaround_time / len(processes)
return avg_waiting_time, avg_turnaround_time
```
该代码实现了一种简单的先到先服务(FCFS)调度算法。该算法将进程按到达时间进行排序,然后按顺序执行它们。在每个进程执行期间,等待时间和周转时间将被计算,并且将用于计算平均等待时间和平均周转时间。
阅读全文