操作系统处理器调度算法c++模拟

来源:互联网 发布:centos 7 ifconfig 编辑:程序博客网 时间:2024/05/19 03:45

本文实现了操作系统的调度算法的模拟,使用c++语言实现,主要有先到先服务算法,最短作业优先,最高相应比,循环轮转算法。对于刚学操作系统的同学十分有帮助。

#include<iostream>

#include<string>
#include<vector>
#include<list>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<utility>
#include<functional>
using namespace std;


const int constCpuChip = 1;//时间片
int dynamicCpuChip = 1;//动态CPU事件片


//class process;


//寄存器信息
class registerInfo{
public:
int psw;//程序状态字
int pc;//程序计数器
//others
public:
registerInfo(){}//default
registerInfo(int psw, int pc){//constructor
this->psw = psw;
this->pc = pc;
}
void set(int psw, int pc){//set info
this->psw = psw;
this->pc = pc;
}
void switchRegister(registerInfo&p){
cout << "register is being switched..." << endl;
cout << "current CPU register info is:" << endl;
cout << *this;
this->psw = p.psw;
this->pc = p.pc;
cout << "register finished switch." << endl;
cout << "current CPU register info is:" << endl;
cout << *this;
}
friend ostream& operator<<(ostream&cout, registerInfo&r){
cout << "psw:" << r.psw << endl;
cout << "pc: " << r.pc << endl;
return cout;
}
};
registerInfo cpuRegister(0,0);//设置一个cpu寄存器用来交换


class userProgram{
public:
string userCode;//用户代码
string userData;//用户数据
public:
userProgram(string userCode="userCode",string userData="userData"){
this->userCode = userCode;
this->userData = userData;
}
friend ostream& operator<<(ostream&cout, userProgram&u){
cout << "userCode:" << u.userCode << endl;
cout << "userData:" << u.userData << endl;
return cout;
}
void set(string userCode, string userData){
this->userCode = userCode;
this->userData = userData;
}
};


class process{
public:
class PCB{
public:
int processID;//进程ID
int priority;//优先级
string processStatus;//进程状态
registerInfo processRegister;//寄存器级上下文
userProgram processProgram;//用户级上下文
}processPCB;
public:
int arriveTime;//进程到达时间
int burstTime;//进程阵发时间
int turnaroundTime;//一个进程的周转时间
int remainigTime;//剩余时间
public:
process(string s){}
process(int processID=0,int priority=0,string processStatus="ready",int psw=0,int pc=0,string userCode="userCode",string userData="userData",int arriveTime=0,int burstTime=0){

processPCB.processID = processID;
processPCB.priority = priority;
processPCB.processStatus = processStatus;
processPCB.processRegister.set(psw, pc);
processPCB.processProgram.set(userCode, userData);
this->arriveTime = arriveTime;
this->burstTime = burstTime;
this->remainigTime = burstTime;//创建进程的时候剩余时间就是阵发时间
this->turnaroundTime = 0;
cout << "=======================================================================================================================" << endl;
cout << "create a process..." << endl;
cout << "process is created." << endl;
cout << "process info is:" << endl;
cout << *this;
}
friend ostream& operator<<(ostream&cout, process&p){
cout << "processID:" << p.processPCB.processID << endl;
cout << "priority:" << p.processPCB.priority << endl;
cout << "processStatus:" << p.processPCB.processStatus << endl;
cout << "processRegister:" << endl;
cout << p.processPCB.processRegister;
cout << "processProgram:" << endl;
cout << p.processPCB.processProgram;
cout << "arriveTime:" << p.arriveTime << endl;
cout << "burstTime:" << p.burstTime << endl;
cout << "turnaroundTime:" << p.turnaroundTime << endl;
cout << "remainingTime:" << p.remainigTime << endl;
return cout;
}
};


template<char T,int U>//T表示大小,U标志以睡为依据
class greaterOrLess{
public:
bool operator()(process&t1, process&t2){
int a = 0, b = 0;


switch (U)
{
default:
break;
case 0://按照进程创建时间的先后
a = t1.arriveTime;
b = t2.arriveTime;
break;
case 1://按照进程阵发时间的大小
a = t1.burstTime;
b = t2.burstTime;
break;
case 2://按照进程优先级的大小
a = t1.processPCB.priority;
b = t2.processPCB.priority;
break;
}


switch (T)
{
default:
break;
case '<':
return a < b;
case '>':
return a > b;
}
}
};




class dispath{//调度类
public:
list<process>readyProcess;//就绪队列
list<process>forCheck;//一个临时的检查队列
float averageTurnaroundTime;//平均周转时间(T1+T2+..)/n
float averageTurnaroundTimeWithWeight;//平均带权周转时间(T1//R1+T2/R2+...)/n
public:
void setForCheck(){
process *p = NULL;
forCheck.clear();
for (list<process>::iterator i = readyProcess.begin(); i != readyProcess.end(); i++){
p = new process("");
*p = *i;
forCheck.push_back(*p);
}
}
void setForReadyProcess(){
averageTurnaroundTime = 0;
averageTurnaroundTimeWithWeight = 0;
process *p = NULL;
readyProcess.clear();
for (list<process>::iterator i = forCheck.begin(); i != forCheck.end(); i++){
p = new process("");
*p = *i;
readyProcess.push_back(*p);
}
}
void sortPair(pair<list<process>::iterator, list<process>::iterator>&p,int i){//i=0为按照最短作业优先,i=1为按照最短剩余时间
list<process>::iterator VI = p.first, VII = p.second, VIII = p.first;
process VV("");
for (;VI != VII;){//VI作为一个控制变量,VII作为一个终止变量
VI++;
for (VIII = VI; VIII != VII;VIII++){//VIII作为一个控制变量
if (i == 0){//按照最短作业优先
if (VI->burstTime > VIII->burstTime){
VV = *VI;//VV作为一个交换变量
*VI = *VIII;
*VIII = VV;
}
}
else if(i==1){//按照最短剩余时间
if (VI->remainigTime > VIII->remainigTime){
VV = *VI;//VV作为一个交换变量
*VI = *VIII;
*VIII = VV;
}
}
}
}
}
bool Find(int ID){//查找ID是否已经存在
for (list<process>::iterator i = readyProcess.begin(); i != readyProcess.end(); i++){
if (i->processPCB.processID == ID)return true;
}
return false;
}
list<process>::iterator findFirstNot(int firstArriveTime){//查找一个段的分界点
for (list<process>::iterator i = readyProcess.begin(); i != readyProcess.end(); i++){
if (i->arriveTime != firstArriveTime){
return i;
}
}
return readyProcess.end();
}
dispath(int n=10){//n为随机产生的进程的数量,默认为10
process*p = NULL;
int id = 0;
srand((unsigned)(time(NULL) * 10000000));
for (int i = 0; i < n; i++){
id = rand();
while (Find(id))id = rand();//保证id的唯一性
p = new process(id, rand() % 5, "ready", 0, 0, "userCode", "userData", rand() % 5, 10 + rand() % 20);
readyProcess.push_back(*p);
}
this->averageTurnaroundTime = 0;
this->averageTurnaroundTimeWithWeight = 0;
}
/*0*/void dispathFCFS(){//先到先服务First Come First Service
setForCheck();
cout << "=======================================================================================================================" << endl;
cout << "cpu is dispatching processes..." << endl;
readyProcess.sort(greaterOrLess<'>',0>());//按arriveTime从小到大排列时间
int countWaitTime = 0;
int count = 0;
while (readyProcess.size()>0){//当就绪队列里面还有进程等待调度的时候
count++;
cpuRegister.switchRegister(readyProcess.front().processPCB.processRegister);//恢复上升进程,没有保存下降进程是因为先到先服务不需要保存下降进程
readyProcess.front().remainigTime = 0;//由于先到先服务,所以没有时间片
readyProcess.front().processPCB.processStatus = "running";
readyProcess.front().turnaroundTime = countWaitTime + readyProcess.front().burstTime;//计算周转时间
averageTurnaroundTime += readyProcess.front().turnaroundTime;//统计中的周转时间,用于计算平均周转时间
averageTurnaroundTimeWithWeight += 1.0*readyProcess.front().turnaroundTime / readyProcess.front().burstTime;//为了计算平均带权周转时间
countWaitTime += readyProcess.front().burstTime;//countWaitTime加该进程的阵发事件作为下一个进程的等待时间


//输出当前统计信息,动态显示cpu调度的情况
cout << "already processed " << count << " processes" << endl;
cout <<count<< "th process " << readyProcess.begin()->processPCB.processID << " is dispatched." << endl;
cout << "current average turnaround time is:" << averageTurnaroundTime / count << endl;
cout << "current average turnaround time with weight is:" << averageTurnaroundTimeWithWeight / count << endl;
cout << endl;
readyProcess.pop_front();//cpu调度完成,清除该进程
}
cout << "all the process is dispatched." << endl;
setForReadyProcess();
}
/*1*/void dispathSJF(){//最短作业优先算法Shortest Job First,该算法是不可剥夺的最短剩余时间优先算法
setForCheck();
cout << "=======================================================================================================================" << endl;
cout << "cpu is dispatching processes..." << endl;
//在最短作业优先算法中,需要先按照时间排序,找第一段其中一个作业最短的作业最先被处理完成
//假如在改作业被处理完成之前没有进程产生的话,删除该进程之后,继续调度第一段的最短作业处理并且完成
//如果在改作业被处理完成之前已经产生了新的一段,改作业被处理之后,删除该进程后,将原来的那一段进程的出现时间修改为后一段的出现时间再重新排序
//重复执行上面的步骤
int countWaitTime = 0;
int count = 0;
int tmpTime = 0;
pair<list<process>::iterator, list<process>::iterator> tmpair;
while (readyProcess.size() > 0){
count++;
readyProcess.sort(greaterOrLess<'>', 0>());//按时间排序
tmpair.first = readyProcess.begin();
tmpair.second = findFirstNot(readyProcess.front().arriveTime);//获取一对区间
//对一个pair进行排序
sortPair(tmpair,0);
//排序完毕,恢复上升现场
cpuRegister.switchRegister(tmpair.first->processPCB.processRegister);//恢复上升进程,没有保存下降进程是因为先到先服务不需要保存下降进程
tmpair.first->remainigTime = 0;
tmpair.first->processPCB.processStatus = "running";
tmpair.first->turnaroundTime = countWaitTime + tmpair.first->burstTime;//周转时间=等待时间+阵发时间
averageTurnaroundTime += tmpair.first->turnaroundTime;//计算中的周转时间
averageTurnaroundTimeWithWeight += 1.0*tmpair.first->turnaroundTime / tmpair.first->burstTime;
countWaitTime += tmpair.first->burstTime;
//第一个节点的信息已经被计算和统计完了,并且输出了一个统计信息
cout << "already processed " << count << " processes" << endl;
cout << count << "th process " << readyProcess.begin()->processPCB.processID << " is dispatched." << endl;
cout << "current average turnaround time is:" << averageTurnaroundTime / count << endl;
cout << "current average turnaround time with weight is:" << averageTurnaroundTimeWithWeight / count << endl;
cout << endl;
//统计完了之后,将所有的时间更改一下之后删除第一个进程
if (tmpair.second != readyProcess.end()&&tmpair.second->arriveTime<tmpair.first->arriveTime + tmpair.first->burstTime){//说明之后还有第二阶段进程
tmpTime = tmpair.second->arriveTime;
for (list<process>::iterator i = tmpair.first; i != tmpair.second; i++){
i->arriveTime = tmpTime;
}
}
//删除第一个进程
readyProcess.pop_front();
}
cout << "all the process is dispatched." << endl;
setForReadyProcess();
}
/*2*/void dispathSRTN(){//最短剩余时间优先算法Shotest Remaining Time Next,该算法实际上是可剥夺的最短作业优先算法
cout << "=======================================================================================================================" << endl;
cout << "cpu is dispatching processes..." << endl;
int countWaitTime = 0;
int count = 0;
int smallTime = 0;//一小段执行时间
int flag = 0;
int tmpTime = 0;
float tmpAverage = 0;
float tmpAverageWithWeight = 0;
pair<list<process>::iterator, list<process>::iterator> tmpair;
while (readyProcess.size() > 0){


tmpAverage = 0;
tmpAverageWithWeight = 0;
flag = 0;
readyProcess.sort(greaterOrLess<'>', 0>());//按照到达时间排序
tmpair.first = readyProcess.begin();
tmpair.second = findFirstNot(readyProcess.front().arriveTime);//获取一对区间
//对一个pair进行排序
sortPair(tmpair, 1);//按照最短剩余时间排序
//排序完毕,恢复上升现场
cpuRegister.switchRegister(tmpair.first->processPCB.processRegister);//恢复上升进程


if (tmpair.second != readyProcess.end()){//如果不是最后一段时间放
if (tmpair.first->arriveTime + tmpair.first->remainigTime <= tmpair.second->arriveTime){//如果在下一段时间出现之前该进程的最后一部分可以完成的话就完成
smallTime = tmpair.first->remainigTime;
count++;
flag = 1;
}
else{
smallTime = tmpair.second->arriveTime - tmpair.first->arriveTime;//否则的话一段时间则为下次时间段减去这次的到达时间
flag = 0;
}
}
else{//如果是最后一段时间
smallTime = tmpair.first->remainigTime;//一段时间就是剩余时间
count++;
flag = 1;
}


//计算上一次的周转时间和带权平均时间
tmpAverage = tmpair.first->turnaroundTime;
if (tmpair.first->burstTime != tmpair.first->remainigTime){
tmpAverageWithWeight = 1.0*tmpair.first->turnaroundTime / (tmpair.first->burstTime - tmpair.first->remainigTime);
}
else{
tmpAverageWithWeight = 0;
}
averageTurnaroundTime -= tmpAverage;//减去上次
averageTurnaroundTimeWithWeight -= tmpAverageWithWeight;//减去上次


tmpair.first->remainigTime -= smallTime;//剩余时间减去smalltime
tmpair.first->processPCB.processStatus = "running";//设置进程状态
tmpair.first->turnaroundTime = countWaitTime + smallTime;//周转时间=等待时间+分段阵发时间
averageTurnaroundTime += tmpair.first->turnaroundTime;//计算中的周转时间
averageTurnaroundTimeWithWeight += 1.0*tmpair.first->turnaroundTime / (tmpair.first->burstTime - tmpair.first->remainigTime);
countWaitTime += smallTime;//计算新的等待时间

//第一个节点的信息已经被计算和统计完了,并且输出了一个统计信息
cout << "already processed " << count << " processes" << endl;
cout << count << "th process " << readyProcess.begin()->processPCB.processID << " is dispatched." << endl;
cout << "current average turnaround time is:" << averageTurnaroundTime / count << endl;
cout << "current average turnaround time with weight is:" << averageTurnaroundTimeWithWeight / count << endl;
cout << endl;


if (tmpair.first->remainigTime == 0){//该进程结束,就不用管了
tmpair.first->processPCB.processStatus = "dead";
//删除第一个进程
readyProcess.pop_front();
}
else{//判断是否需要进行进程切换,如果要切换就进行排序
if (flag == 0){//需要进行排序
tmpair.first->processPCB.processStatus = "ready";
tmpTime = tmpair.second->arriveTime;
for (list<process>::iterator i = tmpair.first; i != tmpair.second; i++){
cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
i->arriveTime = tmpTime;
}
}
}
}
cout << "all the process is dispatched." << endl;
}
/*3*/void dispathHRN(){//最高相应比优先算法Highest Response ratio Next


}
/*4*/void dispathHPF(){//最高优先数优先算法Highest Priority First


}
/*5*/void dispathRR(){//循环轮转算法Round Robin


}
/*6*/void dispathFB(){//反馈排队算法


}
void show(){
cout << "请选择:" << endl;
cout << "0:先到先服务算法." << endl;
cout << "1:最短作业优先算法." << endl;
cout << "2:最短剩余时间优先算法." << endl;
cout << "3:最高相应比优先算法." << endl;
cout << "4:最高优先级优先算法." << endl;
cout << "5:循环轮转算法." << endl;
cout << "6:反馈排队算法." << endl;
cout << "..." << endl;
}
};




void main(){
dispath dis;
//dis.dispathFCFS();
cout << "==================================================================" << endl;
dis.dispathSJF();
cout << "==================================================================" << endl;
dis.dispathSRTN();
cout << "==================================================================" << endl;
}
0 0
原创粉丝点击