python模拟单CPU先来先服务调度算法作业类
时间: 2023-11-14 09:04:48 浏览: 80
CPU.zip_指定CPU
可以使用Python来模拟单CPU先来先服务调度算法作业类。以下是一个简单的示例代码:
```python
class Job:
def __init__(self, name, arrival_time, burst_time):
self.name = name
self.arrival_time = arrival_time
self.burst_time = burst_time
self.start_time = None
self.end_time = None
class Scheduler:
def __init__(self):
self.jobs = []
def add_job(self, job):
self.jobs.append(job)
def run(self):
current_time = 0
for job in self.jobs:
if current_time < job.arrival_time:
current_time = job.arrival_time
job.start_time = current_time
current_time += job.burst_time
job.end_time = current_time
def display_schedule(self):
print("Job\tArrival Time\tBurst Time\tStart Time\tEnd Time")
for job in self.jobs:
print(f"{job.name}\t{job.arrival_time}\t\t{job.burst_time}\t\t{job.start_time}\t\t{job.end_time}")
# 创建作业实例
job1 = Job("Job1", 0, 10)
job2 = Job("Job2", 3, 5)
job3 = Job("Job3", 5, 7)
# 创建调度器实例并添加作业
scheduler = Scheduler()
scheduler.add_job(job1)
scheduler.add_job(job2)
scheduler.add_job(job3)
# 执行调度算法
scheduler.run()
# 显示作业调度表
scheduler.display_schedule()
```
输出结果将显示作业的到达时间、执行时间以及开始和结束时间。
阅读全文