调度算法
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
struct Node {
char name;
double Tarrive;//到达时间
double Tservice;//服务时间
double Tservice2; //服务时间副本
double Tsurplus;//剩余时间
double Tstart;//开始时间
double Taccomplish ;//完成时间
int num;//进程个数
}job[100];
void Arrive_sort(int num)//按到达时间排序
{
double temp1,temp2,temp3,temp4;
for (int i = 0; i < num; i++)
{
for (int j = i + 1; j < num; j++)
{
if (job[i].Tarrive > job[j].Tarrive)
{
temp1 = job[j].name;
job[j].name = job[i].name;
job[i].name = temp1;
temp2 = job[j].Tarrive;
job[j].Tarrive = job[i].Tarrive;
job[i].Tarrive = temp2;
temp3 = job[j].Tservice;
job[j].Tservice = job[i].Tservice;
job[i].Tservice = temp3;
temp4 = job[j].Tservice2;
job[j].Tservice2 = job[i].Tservice2;
job[i].Tservice2 = temp4;
}
}
}
}
void Service_sort(int num)//按服务时间排序
{
double temp1,temp2, temp3,temp4;
for (int i = 1; i < num; i++)
{
for (int j = i + 1; j < num; j++)
{
if (job[i].Tservice > job[j].Tservice)
{
temp1 = job[j].name;
job[j].name = job[i].name;
job[i].name = temp1;
temp2 = job[j].Tarrive;
job[j].Tarrive = job[i].Tarrive;
job[i].Tarrive = temp2;
temp3 = job[j].Tservice;
job[j].Tservice = job[i].Tservice;
job[i].Tservice = temp3;
temp4 = job[j].Tservice2;
job[j].Tservice2 = job[i].Tservice2;
job[i].Tservice2 = temp4;
}
}
}
}
void Arrive_Short_sort(int num)//如果到达时间相等,服务时间按从小到大排序
{
double temp1,temp2, temp3,temp4;
for (int i = 0; i < num; i++)
{
for (int j = i + 1; j < num; j++)
{
if (job[i].Tarrive >= job[j].Tarrive)
{
if (job[i].Tarrive > job[j].Tarrive)
{
temp1 = job[j].name;
job[j].name = job[i].name;
job[i].name = temp1;
temp2 = job[j].Tarrive;
job[j].Tarrive = job[i].Tarrive;
job[i].Tarrive = temp2;
temp3 = job[j].Tservice;
job[j].Tservice = job[i].Tservice;
job[i].Tservice = temp3;
temp4 = job[j].Tservice2;
job[j].Tservice2 = job[i].Tservice2;
job[i].Tservice2 = temp4;
}
else
{
if (job[i].Tservice > job[j].Tservice)
{
temp1 = job[j].name;
job[j].name = job[i].name;
job[i].name = temp1;
temp2 = job[j].Tarrive;
job[j].Tarrive = job[i].Tarrive;
job[i].Tarrive = temp2;
temp3 = job[j].Tservice;
job[j].Tservice = job[i].Tservice;
job[i].Tservice = temp3;
}
}
}
}
}
}
void fcfs(int num)//先来先服务
{
for (int i = 0; i < num; i++)
{
job[i].Tstart = job[i - 1].Taccomplish;//上一个作业结束时间
if (job[i].Tstart < job[i].Tarrive)
{
job[i].Tstart = job[i].Tarrive;
}
else
{
job[i].Tstart = job[i - 1].Taccomplish;
}
job[i].Taccomplish = job[i].Tstart + job[i].Tservice;
}
}
void sjf(int num)//短作业优先
{
Service_sort(num);
for (int i = 0; i < num; i++)
{
job[i].Tstart = job[i - 1].Taccomplish;//上一个作业结束时间
if (job[i].Tstart < job[i].Tarrive)//该作业的开始时间小于到达时间
{
job[i].Tstart = job[i].Tarrive;
}
else
{
job[i].Tstart = job[i - 1].Taccomplish;
}
job[i].Taccomplish = job[i].Tstart + job[i].Tservice;
}
}
void RR(int num)//RR算法
{
double q;
cout << "请输入时间片长度:" << endl;
cin >> q;
double addfuwu = 0;
int count = num;
int count1=0;
while(count != 0){
count = num - count1;
for(int i=0;i<num;i++){
if(job[i].Tservice <= q){
if(job[i].Taccomplish!=0){
addfuwu=addfuwu+0;//算进去累计时间加0
}
else{
addfuwu=addfuwu+job[i].Tservice; //计算服务时间
job[i].Taccomplish = addfuwu; //完成时间等于服务时间
count1++;
}
}
else{
job[i].Tservice = job[i].Tservice - q; //服务时间减去时间片
addfuwu=addfuwu+q; //服务总时间加上时间片长度
}
}
}
}
void print(int n,int num)//输出函数
{
cout << "进程名" << "\t" << "到达时间" << "\t";
if(n==1){cout<< "开始时间" << "\t"; }
cout<< "服务时间" << "\t" << "完成时间" << "\t" << "周转时间"<< "\t" <<"带权周转时间"<< endl;
double job_Ttrans = 0;
double job_ave = 0;
for (int i = 0; i < num; i++)
{
cout<<job[i].name<<"\t"<<job[i].Tarrive<<"\t\t";
if(n==1){cout<< job[i].Tstart <<"\t\t";}
cout<<job[i].Tservice2<<"\t\t"<<job[i].Taccomplish
<<"\t\t"<<job[i].Taccomplish - job[i].Tarrive<<"\t\t"<<(job[i].Taccomplish - job[i].Tarrive)/job[i].Tservice2<<"\t\t"<<endl;
job_Ttrans += (job[i].Taccomplish - job[i].Tarrive);
job_ave +=(job[i].Taccomplish - job[i].Tarrive)/job[i].Tservice2;
}
cout<<"平均周转时间:"<<(job_Ttrans / num)<<endl;
cout<<"平均带权周转时间:"<<(job_ave / num)<<endl<<endl;
}
void display(int num)
{
int ch = 0;
cout << "-------------------------" << endl;
cout << "----------1、FCFS算法 ---------" << endl;
cout << "----------2、SJF算法----------" << endl;
cout << "----------3、RR算法 ----------" << endl;
cout << "----------4、退出 -----------" << endl;
cout << "-------------------------" << endl;
cout << "请选择你想要的算法:" << endl;
cin >> ch;
switch (ch) {
case 1:
cout << "----------1、FCFS算法演示 ---------" << endl;
Arrive_sort(num);
fcfs(num);
print(1,num);
break;
case 2:
Arrive_Short_sort(num);
cout << "----------2、SJF算法演示----------" << endl;
sjf(num);
print(1,num);
break;
case 3:
cout << "----------3、RR算法演示 ----------" << endl;
Arrive_sort(num);
RR(num);
print(2,num);
break;
case 4:
exit;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
int main()
{
int num;
int accomplish;
while(1){
cout << "请输入进程个数,或者输入999退出" << endl;
cin >> num;
if(num == 999)break;
for (int i = 0; i < num; i++)
{
cout << "请输入进程名、到达时间、服务时间" << endl;
cin >> job[i].name>> job[i].Tarrive>>job[i].Tservice;
job[i].Tservice2 = job[i].Tservice;
job[i].Tsurplus = 0;//RR调用前的初始化
job[i].Tstart = 0;//RR调用前的初始化
job[i].Taccomplish = 0;//RR调用前的初始化
}
display(num);
}
return 0;
}
置换算法
#include <iostream>
#include <stdlib.h>
using namespace std;
void OPT_Agorithm();
void FIFO_Agorithm();
void LRU_Agorithm();
double Page_Loss_Rate(int, int);
int Find_Exist(int*, int, int);
int Find_LeastInteviewTime(int, int, int*, int); //OPT最佳页面替换算法调用
void Update_InHereTime(int*, int, int); //FIFO先进先出算法调用
int Find_LeastNotUseTime(int, int, int*); //LRU最近最少使用算法调用
void Print_Frame(int*, int);
void Print_Menu();
int main()
{
int choice;
do
{
Print_Menu();
cout << "请选择要实现的算法:";
cin >> choice;
switch (choice)
{
case 1:
OPT_Agorithm();
break;
case 2:
FIFO_Agorithm();
break;
case 3:
LRU_Agorithm();
break;
case 0:
break;
}
system("pause");
system("cls");
} while (choice);
return 0;
}
/用于遍历 save_Frame[] 的 n 个存储页框, 是否有 “待定地址 -> addr”
如果有就返回 ture, 否则返回 false/
int Find_Exist(int* save_Frame, int n, int addr)
{
for (int i = 0; i < n; i++)
{
if (save_Frame[i] == addr)
{
return i;
}
}
return -1;
}
void Print_Menu()
{
/* 输入模块 */
cout << "+---------------------------------------+" << endl;
cout << "|\t***算法清单***\t\t\t|" << endl;
cout << "|\t1.最佳置换算法(OPT)\t\t|" << endl << "|\t2.先进先出算法(FIFO)\t\t|" << endl;
cout << "|\t3.最近最久未使用算法(LRU)\t|" << endl;
cout << "|\t0.退出\t\t\t\t|" << endl;
cout << "+---------------------------------------+" << endl;
}
void Print_Frame(int* save_Frame, int n)
{
cout << "\t";
for (int i = 0; i < n; i++)
{
if (i == 0)
{
if (save_Frame[i] == -999)
cout << "/ /";
else
cout << "/" << save_Frame[i] << "/";
}
else
{
if (save_Frame[i] == -999)
cout << " /";
else
cout << save_Frame[i] << "/";
}
}
cout << endl;
}
void Init(int* n, int* len, int*& save_Frame, int*& interview_Array)
{
cout << "请输入物理页框数 :";
cin >> *n;
save_Frame = new int[*n];
for (int i = 0; i < *n; i++)
save_Frame[i] = -999;
cout << "请输入地址走向的长度:";
cin >> *len;
cout << "请输入地址走向:";
interview_Array = new int[*len];
for (int i = 0; i < *len; i++)
cin >> interview_Array[i];
}
/*
* 缺页中断率:
* 假设进程 P 在运行中成功的内存访问次数为 s
* 不成功的访问次数为 F,则缺页中断率为 R = F/(S+F)
*/
double Page_Loss_Rate(int S, int F)
{
double ans = 1.0 * F / (1.0 * S + 1.0 * F) * 100;
return ans;
}
int Find_LeastInteviewTime(int sta, int addr, int* interview_Array, int len) //OPT最佳页面替换算法调用
{
for (int i = sta; i < len; i++)
{
if (interview_Array[i] == addr)//从当前位置向后找,找到最近位置与物理页框中相同的就返回
{
return i - sta; //返回距离当前位置的数字,越大的越越晚用到,会被替换出去
}
}
return 99999;
}
void OPT_Agorithm() //OPT最佳页面替换算法
{
cout << "欢迎使用 OPT " << endl;
int n, len, * save_Frame = NULL, * interview_Array = NULL;
Init(&n, &len, save_Frame, interview_Array);
//测试样例: 1 3 12 2 3 2 1 5 2 4 5 3 2 5 2
int addr;
int cnt = 0;
int score = 0;
int fail_time = 0;
int iter = 0;
while (iter < len)
{
addr = interview_Array[iter];
iter++;
cout << endl << "第" << iter << "轮:";
if (cnt < n)
{
if (Find_Exist(save_Frame, cnt, addr) != -1)
{
score++;
cout << "\"" << addr << "\" 被命中了\t\t------->";
Print_Frame(save_Frame, n);
}
else // 未命中,但有空间," << "\" << addr << "\" 被装入
{
fail_time++;
cout << "未命中," << "\"" << addr << "\" 被装入 \t------->";
save_Frame[cnt] = addr;
Print_Frame(save_Frame, n);
cnt++;
}
}
else
{
if (Find_Exist(save_Frame, n, addr) != -1)
{
score++;
cout << "\"" << addr << "\" 被命中了\t\t------->";
Print_Frame(save_Frame, n);
}
else // 未命中,但没了空间
{
fail_time++;
int* least_Time = new int[n];
int max_Time = 0;
int index;
for (int i = 0; i < n; i++)
{
least_Time[i] = Find_LeastInteviewTime(iter, save_Frame[i], interview_Array, len);
if (least_Time[i] > max_Time)
{
max_Time = least_Time[i];
index = i;
}
}
cout << "\"" << addr << "\" 替换了 \"" << save_Frame[index] << "\"\t\t------->";
save_Frame[index] = addr;
Print_Frame(save_Frame, n);
delete[] least_Time;
}
}
}
cout << endl;
cout << "缺页次数为:" << fail_time << endl;
cout << "缺页中断率 R = " << Page_Loss_Rate(score, fail_time) << "%" << endl;
delete[] save_Frame;
delete[] interview_Array;
}
void Update_InHereTime(int* in_HereTime, int n, int ind) //FIFO先进先出算法调用
{
for (int i = 0; i < n; i++)
{
in_HereTime[i]++;
}
if (ind != -1)
in_HereTime[ind] = 0;
}
void FIFO_Agorithm() //FIFO先进先出算法
{
int n, len, * save_Frame = NULL, * interview_Array = NULL;
Init(&n, &len, save_Frame, interview_Array);
int* in_HereTime = new int[n];
for (int i = 0; i < n; i++)
in_HereTime[i] = 0; // 初始化都为零
//测试样例: 2 3 12 2 3 2 1 5 2 4 5 3 2 5 2
int addr;
int cnt = 0;
int score = 0;
int fail_time = 0;
int iter = 0;
while (iter < len)
{
cout << endl << "第" << iter << "轮:";
addr = interview_Array[iter];
iter++;
if (cnt < n)
{
if (Find_Exist(save_Frame, cnt, addr) != -1)
{
score++;
cout << "\"" << addr << "\" 被命中了\t\t------->";
Print_Frame(save_Frame, n);
Update_InHereTime(in_HereTime, cnt, -1); //前面进来停留的时间都加1
}
else // 未命中,但有空间
{
fail_time++;
cout << "未命中," << "\"" << addr << "\" 被装入 \t------->";
save_Frame[cnt] = addr;
Print_Frame(save_Frame, n);
Update_InHereTime(in_HereTime, cnt, cnt); //前面进来停留的时间都加1,这次进来的时间清0
cnt++;
}
}
else
{
if (Find_Exist(save_Frame, n, addr) != -1)
{
score++;
cout << "\"" << addr << "\" 被命中了\t\t------->";
Print_Frame(save_Frame, n);
Update_InHereTime(in_HereTime, n, -1); //全部停留时间加1
}
else // 未命中,但没了空间
{
fail_time++;
int max_Time = 0;
int index;
for (int i = 0; i < n; i++)
{
if (in_HereTime[i] > max_Time)
{
max_Time = in_HereTime[i];
index = i;
}
}
cout << "\"" << addr << "\" 替换了 \"" << save_Frame[index] << "\"\t\t------->";
save_Frame[index] = addr; //找到时间最长的被替换出
Print_Frame(save_Frame, n);
//int ind = Find_Exist(save_Frame, n, addr);
Update_InHereTime(in_HereTime, n, index); //新进来的物理页框停留时间清0
}
}
}
cout << endl;
cout << "缺页次数为:" << fail_time << endl;
cout << "缺页中断率 R = " << Page_Loss_Rate(score, fail_time) << "%" << endl;
delete[] save_Frame;
delete[] interview_Array;
delete[] in_HereTime;
return;
}
int Find_LeastNotUseTime(int end, int addr, int* interview_Array) //LRU最近最少使用算法调用
{
for (int i = end - 1; i >= 0; i--)
{
if (interview_Array[i] == addr) //从最近进来的开始往回找,找到最近进来的物理页框的进来序号就返回
{
// cout << " i = " << i << endl;
return end - i; //从当前位置找到最近进来的序号,end-i就是正向进来的序号,序号越大,停留时间越长
}
}
return 99999;
}
//n:物理页框长度 len:进来数据总长度,save_Frame:物理框存放的数据bufer interview_Array:实际进来的数据存放bufer
void LRU_Agorithm() //LRU最近最少使用算法
{
int n, len, * save_Frame = NULL, * interview_Array = NULL;
Init(&n, &len, save_Frame, interview_Array);
//测试样例: 3 3 12 2 3 2 1 5 2 4 5 3 2 5 2
int addr;
int cnt = 0;
int score = 0;
int fail_time = 0;
int iter = 0;
while (iter < len)
{
addr = interview_Array[iter];
iter++;
cout << endl << "第" << iter << "轮:";
if (cnt < n) //没装满
{
if (Find_Exist(save_Frame, cnt, addr) != -1)
{
score++;
cout << "\"" << addr << "\" 被命中了\t\t------->";
Print_Frame(save_Frame, n);
}
else // 未命中,但有空间
{
fail_time++;
cout << "未命中," << "\"" << addr << "\" 被装入 \t------->";
save_Frame[cnt] = addr;
Print_Frame(save_Frame, n);
cnt++;
}
}
else //装满了
{
if (Find_Exist(save_Frame, n, addr) != -1)
{
score++;
cout << "\"" << addr << "\" 被命中了\t\t------->";
Print_Frame(save_Frame, n);
}
else // 未命中,但没了空间
{
fail_time++;
int* Not_UseTime = new int[n];
int max_Time = 0;
int index;
for (int i = 0; i < n; i++)
{
Not_UseTime[i] = Find_LeastNotUseTime(iter, save_Frame[i], interview_Array);
if (Not_UseTime[i] > max_Time)
{
max_Time = Not_UseTime[i];
index = i;
}
}
cout << "\"" << addr << "\" 替换了 \"" << save_Frame[index] << "\"\t\t------->";
save_Frame[index] = addr;
Print_Frame(save_Frame, n);
delete[] Not_UseTime;
}
}
}
cout << endl;
cout << "缺页次数为:" << fail_time << endl;
cout << "缺页中断率 R = " << Page_Loss_Rate(score, fail_time) << "%" << endl;
delete[] save_Frame;
delete[] interview_Array;
}

被折叠的 条评论
为什么被折叠?



