操作系统实验一
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
操作系统实验
实验报告(一)专业:软件工程日期:10月21日
实验名称:高优先级优先进程调度试验
一、代码
#include
#include
#include
#include
#include
using namespace std;
//进程调度
const int N = 5;
struct Process {
//PCB进程控制块
string name;
int priority;
int come, cost, costed;
char state;
bool operator < (const Process &rhs) const {
return priority < rhs.priority;//大根堆
}
};
class Schedule {
public:
Schedule() {
for(int i = 0; i < N; i++) {
Process tmp;
string rhs = "";
for(int j = 0; j < 8; j++) {
//产生随机进程名称
int num = Rand(0, 51);
if(num < 26) rhs += 'A' + num;
else rhs += 'a' + (num-26);
}
= rhs;
//随机优先级
tmp.priority = Rand(0, 99);
e = i;
//随机消耗时间
tmp.cost = Rand(1, 10);
tmp.costed = 0;
//状态置为空
tmp.state = 'N';
FIFO.push(tmp);
}
}
void Interface() {
cout << "优先级进程调度算法!" << endl;
while(!FIFO.empty()) {
for(int i = 0; i < 3; i++) {
//取出若干个作业进入就绪队列!先来先服务
if(FIFO.empty()) break;
//状态改为就绪
FIFO.front().state = 'W';
pq.push(FIFO.front());
show_pq.push(FIFO.front());
FIFO.pop();
}
cout << endl << "打印就绪队列!" << endl;
while(!show_pq.empty()) {
cout << show_pq.top().name << " | " << show_pq.top().priority << " | " << show_pq.top().come << " | " << show_pq.top().cost << " | " << show_pq.top().costed << " | " << show_pq.top().state << endl;
show_pq.pop();
}
cout << endl;
while(!pq.empty()) {
//就绪队列中实行优先级调度最高优先级优先
cout << "当前处理的进程是:" << endl;
cout << pq.top().name << " | " << pq.top().priority << " | " << pq.top().come << " | " << pq.top().cost << " | " << pq.top().costed << " | " << pq.top().state << endl;
Process cur = pq.top();
cur.costed += tp;//分配一个时间片
if(cur.costed >= cur.cost) {
//完成
cout << "该进程已完成!" << endl;
//状态改为完成
cur.state = 'F';
cout << << " | " << cur.priority << " | " << e << " | " << cur.cost << " | " << cur.costed << " | " << cur.state << endl << endl;
pq.pop();
}
else {
//没完成
cout << "该进程未能完成,优先级降低" << endl;
//状态改为运行
cur.state = 'R';
cur.priority --;
cout << << " | " << cur.priority << " | " << e << " | " << cur.cost << " | " << cur.costed << " | " << cur.state << endl << endl;
//没完成的作业重新进入就绪队列
pq.pop();
pq.push(cur);
}
}
}
}
private:
int tp = 4;
priority_queue
queue
int Rand(int l, int r) {
//随机数生成器
return (int)(rand()%(r-l+1)+l);
}
};
int main()
{
srand(time(NULL));
Schedule A;
A.Interface();
return 0;
}
二、结果