进程调度模拟程序python课设
时间: 2023-12-03 17:40:53 浏览: 105
以下是一个简单的Python进程调度模拟程序示例,它使用了先来先服务(FCFS)调度算法:
```python
class PCB:
def __init__(self, name, arrive_time, need_time):
self.name = name
self.arrive_time = arrive_time
self.need_time = need_time
self.used_time = 0
self.status = 'ready'
def run(self):
self.status = 'running'
print('Process', self.name, 'is running...')
self.used_time += 1
if self.used_time == self.need_time:
self.status = 'finished'
print('Process', self.name, 'is finished.')
def fcfs(processes):
time = 0
while processes:
current_process = processes[0]
if current_process.arrive_time <= time:
current_process.run()
if current_process.status == 'finished':
processes.pop(0)
else:
print('No process is running...')
time += 1
if __name__ == '__main__':
processes = [PCB('P1', 0, 3), PCB('P2', 2, 6), PCB('P3', 4, 4)]
fcfs(processes)
```
该程序定义了一个PCB类,表示进程控制块,包含进程名、到达时间、需要运行时间、已用CPU时间和进程状态等信息。程序还定义了一个先来先服务(FCFS)调度算法函数fcfs,它接受一个进程列表作为参数,并模拟进程的运行过程。在主函数中,我们初始化了三个进程,并调用fcfs函数模拟进程调度过程。
阅读全文