一、实验目的:
了解并掌握动态高优先权优先调度算法的理论,掌握动态优先权的设置方式。
二、实验内容:
设置进程体:进程名,进程的到达时间,服务时间,初始优先权,进程状态(W——等待,R——运行,F——完成),进程间的链接指针;
进程初始化:由用户输入进程名、服务时间、初始优先权进行初始化,同时,初始化进程的状态为W;
显示函数:在进程调度前、调度中和调度后进行显示;
排序函数:对就绪状态的进程按照优先权排序。优先权相同时进入等待队列时间早的进程在前,注意考虑到达时间;
调度函数:每次从等待队列队首调度优先权最高的进程执行,状态变化。并在执行一个时间单位后优先权变化,服务时间变化,状态变化。当服务时间为0时,状态变为F;
删除函数:撤销状态为F的进程。
三、实验代码
#include
#include
#include
#include
using namespace std;
const int jobNum_Max = 10000;
struct jobInfo
{
int uid;//进程号
int arrival_time;//到达时间
int service_time;//服务时间
int ready_time;//进入就绪队列时间
int remaining_time;//剩余服务时间
int finish_time;//完成时间
int weight;//权重
char status;//状态
friend bool operator < (jobInfo a, jobInfo b)
{
if(a.weight == b.weight)
return a.ready_time > b.ready_time;//时间小的优先
return a.weight < b.weight;//权值大的优先
}
} job[jobNum_Max];
int in_it();//初始化,返回作业总数
bool cmp(jobInfo a, jobInfo b);
void solve();
void updateTojob(jobInfo t);
priority_queue<jobInfo> updateToWaitList();
void Show_Before();//初始信息
priority_queue<jobInfo> Show_Ing(jobInfo t);//每个时刻作业的情况
void Show_After();//调度图
int TotalNum, CurrentTime;//作业总数量、模拟时间片前进
int WeightChange;
int record[jobNum_Max];//每个时间片处理的作业名
priority_queue<jobInfo> WaitList;
int main()
{
TotalNum = in_it();
Show_Before();
solve();
Show_After();
}
int in_it()
{
int flag;
printf("请选择输入方式(控制台输入选择 1,文件输入选择 2): ");
scanf("%d", &flag);
while(flag != 1 && flag != 2)
{
printf("输入错误,请重新输入:\n");
scanf("%d", &flag);
}
int n = 0;
if(flag == 1)
{
printf("请按照格式输入:“进程ID 进程到达时间 进程服务时间 进程初始权重”,ctrl+z 结束输入\n");
while(scanf("%d%d%d%d", &job[n].uid, &job[n].arrival_time, &job[n].service_time, &job[n].weight) != EOF)
{
job[n].status = 'W';
job[n].remaining_time = job[n].service_time;
job[n].ready_time = 0;
n++;
}
}
else
{
ifstream date;
date.open("date.txt");
while(!date.eof())
{
date >> job[n].uid >> job[n].arrival_time >> job[n].service_time >> job[n].weight;
job[n].status = 'W';
job[n].remaining_time = job[n].service_time;
job[n].ready_time = 0;
n++;
}
printf("数据读取成功!\n");
}
sort(job, job+n, cmp);
printf("请输入每过一个时间单位权值的变化:");
scanf("%d", &WeightChange);
return n;
}
bool cmp(jobInfo a, jobInfo b)
{
return a.arrival_time < b.arrival_time;
}
void solve()
{
int InJobNum = 0;
jobInfo Running;
Running.uid = -1;
while(InJobNum != TotalNum || !WaitList.empty())
{
for(int i=InJobNum; i<TotalNum; i++)
{
if(job[i].arrival_time == CurrentTime)
{
job[i].ready_time = CurrentTime;
WaitList.push(job[i]);
InJobNum++;
}
else
break;
}
if(!WaitList.empty())
{
Running = WaitList.top();
WaitList.pop();
Running.status = 'R';
WaitList = Show_Ing(Running);
Running.remaining_time--;
record[CurrentTime++] = Running.uid;
if(!Running.remaining_time)
{
Running.finish_time = CurrentTime;
Running.status = 'F';
updateTojob(Running);
}
else
{
Running.weight -= WeightChange;
Running.ready_time = CurrentTime;
Running.status = 'W';
WaitList.push(Running);
updateTojob(Running);
}
}
else
{
Running.uid = -1;
WaitList = Show_Ing(Running);
record[CurrentTime++] = -1;
}
//WaitList = updateToWaitList();
}
Running.uid = -1;
WaitList = Show_Ing(Running);
}
void updateTojob(jobInfo t)
{
for(int i=0; i<TotalNum; i++)
{
if(job[i].uid == t.uid)
{
job[i] = t;
return;
}
}
}
priority_queue<jobInfo> updateToWaitList()
{
priority_queue<jobInfo> t;
while(!WaitList.empty())
{
jobInfo a = WaitList.top();
WaitList.pop();
a.weight += WeightChange;
t.push(a);
updateTojob(a);
}
return t;
}
void Show_Before()
{
putchar('\n');
printf("%s\n", "进程信息");
printf("%s\t%s\t%s\t%s\n", "Id", "arrival", "service", "weight");
for(int i=0; i<TotalNum; i++)
printf("%d\t%d\t%d\t%d\n", job[i].uid, job[i].arrival_time, job[i].service_time, job[i].weight);
}
priority_queue<jobInfo> Show_Ing(jobInfo t)
{
priority_queue<jobInfo> Temp;
putchar('\n');
printf("%s\t%s\t%s\t%s\t%s\t%s=%d\n", "Id", "remain", "status", "weight", "ready", "Time", CurrentTime);
if(t.uid != -1)
printf("%d\t%d\t%c\t%d\t%d\n", t.uid, t.remaining_time, t.status, t.weight, t.ready_time);
while(!WaitList.empty())
{
jobInfo a = WaitList.top();
WaitList.pop();
printf("%d\t%d\t%c\t%d\t%d\n", a.uid, a.remaining_time, a.status, a.weight, a.ready_time);
Temp.push(a);
}
for(int i=0; i<TotalNum; i++)
if(job[i].status == 'F')
printf("%d\t%d\t%c\t%d\n", job[i].uid, job[i].remaining_time, job[i].status, job[i].weight);
return Temp;
}
void Show_After()
{
putchar('\n');
printf("结果详情\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\n", "Name", "Arrival", "Finish", "Service", "cycling", "W=T/Ts");
for(int i=0; i<TotalNum; i++)
{
jobInfo t = job[i];
int zz = t.finish_time - t.arrival_time;
printf("%d\t%d\t%d\t%d\t%d\t%.2lf\n", t.uid, t.arrival_time, t.finish_time, t.service_time, zz, 1.0*zz/t.service_time);
}
putchar('\n');
printf("动态 高优先权优先调度算法 时间图\n\t|");
for(int i=0; i<=CurrentTime; i++)
printf("%-4d", i);
putchar('\n');
for(int i=0; i<TotalNum; i++)
{
printf("%d\t|", job[i].uid);
for(int j=0; j<CurrentTime; j++)
{
if(job[i].uid == record[j])
printf("——");
else
printf(" ");
}
putchar('\n');
}
}
/*
1 0 5 3
2 1 4 3
3 2 1 5
4 4 2 4
5 5 1 5
1 0 5 3
2 1 3 5
3 2 1 3
4 3 1 4
5 4 2 2
*/