用Python写进程调度算法FCFS
时间: 2024-03-30 13:32:23 浏览: 34
在Python中实现先来先服务(FCFS)调度算法的代码如下所示[^1]:
```python
class Process:
def __init__(self, pid, burst_time):
self.pid = pid
self.burst_time = burst_time
def fcfs_scheduling(processes):
waiting_time = 0
total_waiting_time = 0
turnaround_time = 0
total_turnaround_time = 0
for process in processes:
waiting_time += process.burst_time
turnaround_time = waiting_time
total_waiting_time += waiting_time
total_turnaround_time += turnaround_time
avg_waiting_time = total_waiting_time / len(processes)
avg_turnaround_time = total_turnaround_time / len(processes)
print("Average Waiting Time:", avg_waiting_time)
print("Average Turnaround Time:", avg_turnaround_time)
# 示例用法
processes = [Process(1, 10), Process(2, 5), Process(3, 8)]
fcfs_scheduling(processes)
```
请注意,上述代码是一个示例,其中`Process`类表示进程,`pid`表示进程ID,`burst_time`表示进程的执行时间。`fcfs_scheduling`函数实现了先来先服务调度算法,计算了平均等待时间和平均周转时间。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)