操作系统-进程调度算法设计与实现实验报告
- 格式:docx
- 大小:312.58 KB
- 文档页数:4
实验报告
课程名称操作系统实验名称进程调度算法设计与实现姓名学号专业班级实验日期成绩指导教师(①实验目的②实验设备和环境③实验内容与步骤④实验结果与分析⑤总结,问题及建议)
一、内容:设计一个简单的进程调度算法,模拟OS中的进程调度过程
二、要求:
①进程数不少于5个;
②进程调度算法任选;
最好选用动态优先数法,每运行一个时间片优先数减3
③用C++(或C)语言编程;
④程序运行时显示进程调度过程。
三、步骤:
①设计PCB及其数据结构:
进程标识数:ID
进程优先数:PRIORITY(优先数越大,优先级越高)
进程已占用时间片:CPUTIME
进程尚需时间片:ALLTIME(一旦运行完毕,ALLTIME为0)
进程队列指针:NEXT,用来将PCB排成队列
进程状态:STATE(一般为就绪,不用)
②设计进程就绪队列及数据结构;
③设计进程调度算法,并画出程序流程图;
④设计输入数据和输出格式;
结构格式:当前正运行的进程:0
当前就绪队列:2,1,3,4
⑤编程上机,验证结果。
四、分析
①以时间片为单位调度运行;
②每次总是从ALLTIME中不为0,且PRIORITY最大的进程调度运行一个时间片;
③上述进程运行后其优先数减3,再修改其CPUTIME和ALLTIME,重复②,③
④直到所有进程的ALLTIME均变为0。
五、代码
#include<iostream>
#include<string>
#include<queue>
using namespace std;
typedef struct pcb {
string pName;//进程名
int priorityNumber;//优先数
float serviceTime;//服务时间
float estimatedRunningtime;//估计运行时间
char state;//状态
bool operator<(const struct pcb &a)const {
return priorityNumber > a.priorityNumber || priorityNumber == a.priorityNumber&&estimatedRunningtime > a.estimatedRunningtime;
}
}PCB;
void createProcess(priority_queue<PCB> &p, int n) {//创建n个进程,带头结点cout << endl << endl << "创建进程" << endl;
PCB r;//工作结点
for (int i = 0; i<n; i++) {
cout << "请输入第" << i + 1 << "个进程的名字、优先数、服务时间(例如:A 12 8 ):";
cin >> r.pName;
cin >> r.priorityNumber;
cin >> r.serviceTime;
r.estimatedRunningtime = r.serviceTime;
r.state = 'R';
p.push(r);
}
cout << endl;
}
void printProcess(priority_queue<PCB> p) {
PCB s;
cout << "进程名\t优先数服务时间已运行时间还剩运行时间" << endl;
while (p.size() != 0) {
s = p.top();
cout << s.pName << "\t" << s.priorityNumber << "\t " << s.serviceTime << "\t ";
cout << s.serviceTime - s.estimatedRunningtime << "\t " << s.estimatedRunningtime << endl;
p.pop();
}
cout << endl;
}
void runProcess(priority_queue<PCB> &p) {//运行进程
PCB s;
while(p.size()!=0){
s = p.top();
p.pop();
cout << "正在运行的进程" << endl;
cout << "进程名\t优先数服务时间已运行时间还剩运行时间" << endl;//输出当前进程
cout << s.pName << "\t" << s.priorityNumber << "\t " << s.serviceTime << "\t ";
cout << s.serviceTime - s.estimatedRunningtime << "\t " << s.estimatedRunningtime << endl;
s.priorityNumber++;//优先数加1
s.estimatedRunningtime--;//估计运行时间减1
if (s.estimatedRunningtime == 0) {
s.state = 'C';
}
else
p.push(s);
cout << "进程" << s.pName << "执行一次之后就绪队列中的进程" << endl;
printProcess(p);
}
cout << endl;
}
int main() {
priority_queue<PCB> p;
int n;
cout << "请输入进程的个数:";
cin >> n;
createProcess(p, n);
runProcess(p);
getchar();
getchar();
return 0;
}
六、结果。