如何在Python 3的FIFO CPU调度模拟器中读取Excel进程数据?
Python 3 FIFO CPU调度模拟器:从Excel读取数据实现方案
1. 安装依赖包
要读取xlsx格式的Excel文件,用pandas库最方便,它需要openpyxl作为底层引擎支持,执行下面的命令安装:
pip install pandas openpyxl
2. 读取Excel数据并转换格式
假设你的cpu-scheduling.xlsx里的工作表(默认取第一个)包含Process ID、Arrival Time、Instruction Load三列,用下面的代码读取并转换成调度逻辑需要的进程结构:
import pandas as pd def read_processes_from_excel(file_path): try: # 只读取需要的三列,避免无关数据干扰 df = pd.read_excel(file_path, usecols=["Process ID", "Arrival Time", "Instruction Load"]) except FileNotFoundError: print(f"错误:找不到文件 {file_path}") return [] except KeyError: print("错误:Excel文件缺少指定列(Process ID/Arrival Time/Instruction Load)") return [] # 转换成字典列表,适配大多数FIFO调度逻辑的结构 processes = [] for _, row in df.iterrows(): processes.append({ 'pid': row['Process ID'], 'arrival_time': row['Arrival Time'], 'burst_time': row['Instruction Load'] # 指令负载对应FIFO中的执行时长,可根据你的代码调整键名 }) return processes
提示:如果你的现有代码用类来定义进程,把上面的字典改成类实例即可,比如
Process(row['Process ID'], row['Arrival Time'], row['Instruction Load'])
3. 集成到现有FIFO调度代码
把原来手动定义进程的代码块,替换成读取Excel的函数调用:
# 替换前(手动定义) # processes = [{'pid':1, 'arrival_time':0, 'burst_time':10}, ...] # 替换后(从Excel读取) processes = read_processes_from_excel('cpu-scheduling.xlsx')
4. 完整可运行示例代码
下面是包含Excel读取+FIFO调度逻辑的完整代码,直接就能用:
import pandas as pd def read_processes_from_excel(file_path): try: df = pd.read_excel(file_path, usecols=["Process ID", "Arrival Time", "Instruction Load"]) except FileNotFoundError: print(f"错误:找不到文件 {file_path}") return [] except KeyError: print("错误:Excel文件缺少指定列(Process ID/Arrival Time/Instruction Load)") return [] processes = [] for _, row in df.iterrows(): processes.append({ 'pid': row['Process ID'], 'arrival_time': row['Arrival Time'], 'burst_time': row['Instruction Load'] }) return processes def fifo_scheduling(processes): if not processes: print("没有可调度的进程") return # FIFO核心:按到达时间排序 sorted_processes = sorted(processes, key=lambda x: x['arrival_time']) current_time = 0 total_waiting = 0 total_turnaround = 0 print("进程ID | 到达时间 | 指令负载 | 完成时间 | 周转时间 | 等待时间") print("-----------------------------------------------------------") for p in sorted_processes: # 处理进程到达晚于当前时间的情况 if current_time < p['arrival_time']: current_time = p['arrival_time'] completion_time = current_time + p['burst_time'] turnaround_time = completion_time - p['arrival_time'] waiting_time = turnaround_time - p['burst_time'] total_waiting += waiting_time total_turnaround += turnaround_time print(f"{p['pid']:^7} | {p['arrival_time']:^9} | {p['burst_time']:^11} | {completion_time:^10} | {turnaround_time:^10} | {waiting_time:^9}") current_time = completion_time avg_waiting = total_waiting / len(sorted_processes) avg_turnaround = total_turnaround / len(sorted_processes) print(f"\n平均等待时间:{avg_waiting:.2f}") print(f"平均周转时间:{avg_turnaround:.2f}") if __name__ == "__main__": processes = read_processes_from_excel('cpu-scheduling.xlsx') fifo_scheduling(processes)
额外注意事项
- 确保
cpu-scheduling.xlsx和代码在同一目录,或者在函数中传入完整文件路径(比如C:/data/cpu-scheduling.xlsx) - 如果Excel不是用默认工作表,在
pd.read_excel中添加sheet_name参数指定,比如sheet_name="调度数据" - 若指令负载需要转换为CPU执行时间(比如每条指令对应0.5个时间单位),在读取数据时加转换逻辑即可,比如
burst_time = row['Instruction Load'] * 0.5
内容的提问来源于stack exchange,提问作者Sayantan Chowdhury
相关产品推荐
相关产品推荐

