非抢占式
/*
process:优先级调度非抢占式
author:lisuhang
time:2022-4-11
*/
#include<iostream>
#include <fstream>
using namespace std;
int const N = 100;
int i = 0;
//-------------进程--------------------//
//进程
typedef struct
{
//进程名称
string name;
//到达时间
int arrive_time;
//完成需要时间
int time;
//优先级
int grade;
}process;
//更新进程时间
void update_process(process& p)
{
p.time--;
}
//进程是否完成
bool is_finish(process p)
{
if(p.time)
{
return false;
}
return true;
}
void print(process p)
{
cout << "进程" << p.name << "已经完成" <<" ";
}
//-------------进程--------------------//
//--------------等待队列-----------------//
//等待队列
typedef struct
{
process p[N];
int first = 0;
int last = 0;
}queue;
//插入等待队列
void insert(queue& q, process p)
{
q.p[q.last++] = p;
q.last %= N;
}
//离开等待队列
process pop(queue& q)
{
process p = q.p[q.first++];
q.first %= N;
return p;
}
//判断队列是否为空
bool empty(queue q)
{
return q.first == q.last;
}
//--------------等待队列-----------------//
//--------------pcb---------------------//
//pcb
typedef struct
{
//最高优先级数
int const m = 5;
//正在执行的进程
process p;
//该进程已经占用CPU的时间
int time = 0;
//等待队列
queue q[N];
//当前进程状态
int state = 0;
}pcb;
//输出当前pcb信息
void print_pcb(pcb b)
{
cout << "当前正在执行:" <<b.p.name << " ";
}
//时间片更新
void update_pcb(pcb& b)
{
//如果当前正在运行进程
if(b.state)
{
//占用时间
b.time++;
//进程剩余时间
update_process(b.p);
//如果进程完成,主动放弃cpu
if(is_finish(b.p))
{
b.state = 0;
//打印完成信息
print(b.p);
b.time = 0;
}
else
{
print_pcb(b);
}
}
//如果当前没有运行进程
if(!b.state)
{
for(int i = b.m; i> 0; i--)
{
//如果等待队列不为空
if(!empty(b.q[i]))
{
b.p = pop(b.q[i]);
cout << b.p.name << "开始运行" << " ";
b.state = 1;
break;
}
}
//如果等待队列为空
if(!b.state)
{
cout << "当前无可执行进程" << " ";
}
}
}
//--------------pcb---------------------//
//待执行进程总数
int n = 0;
//最多运行的时间
int all = 0;
//进程
process p[N];
//读入进程数据
void data()
{
ifstream readFile("data2.txt");
if(!readFile.is_open())
{
cout << "打开数据文件失败" << endl;
exit(0);
}
while(!readFile.eof())
{
readFile >> p[n].name >> p[n].arrive_time >> p[n].time >> p[n].grade;
if(all > p[n].arrive_time) all += p[n].time;
else all = p[n].arrive_time + p[n].time;
n++;
}
readFile.close();
}
int main()
{
int j = 0;
pcb b;
data();
for(; i <= all; i++)
{
cout << "第" << i << "时刻" << " ";
//进程到达,则进入等待队列
while(j < n && p[j].arrive_time == i)
{
insert(b.q[p[j].grade], p[j]);
cout << p[j].name << "进入等待队列" << " ";
j++;
}
update_pcb(b);
cout << endl;
}
return 0;
}
抢占式
/*
process:优先级调度抢占式
author:lisuhang
time:2022-4-11
*/
#include<iostream>
#include <fstream>
using namespace std;
int const N = 100;
int i = 0;
//-------------进程--------------------//
//进程
typedef struct
{
//进程名称
string name;
//到达时间
int arrive_time;
//完成需要时间
int time;
//优先级
int grade;
}process;
//更新进程时间
void update_process(process& p)
{
p.time--;
}
//进程是否完成
bool is_finish(process p)
{
if(p.time)
{
return false;
}
return true;
}
void print(process p)
{
cout << "进程" << p.name << "已经完成" <<" ";
}
//-------------进程--------------------//
//--------------等待队列-----------------//
//等待队列
typedef struct
{
process p[N];
int first = 0;
int last = 0;
}queue;
//插入等待队列
void insert(queue& q, process p)
{
q.p[q.last++] = p;
q.last %= N;
}
//离开等待队列
process pop(queue& q)
{
process p = q.p[q.first++];
q.first %= N;
return p;
}
//判断队列是否为空
bool empty(queue q)
{
return q.first == q.last;
}
//--------------等待队列-----------------//
//--------------pcb---------------------//
//pcb
typedef struct
{
//最高优先级数
int const m = 5;
//正在执行的进程
process p;
//该进程已经占用CPU的时间
int time = 0;
//等待队列
queue q[N];
//当前进程状态
int state = 0;
}pcb;
//输出当前pcb信息
void print_pcb(pcb b)
{
cout << "当前正在执行:" <<b.p.name << " ";
}
//时间片更新
void update_pcb(pcb& b)
{
//如果当前正在运行进程
if(b.state)
{
//占用时间
b.time++;
//进程剩余时间
update_process(b.p);
//如果进程完成,主动放弃cpu
if(is_finish(b.p))
{
b.state = 0;
//打印完成信息
print(b.p);
b.time = 0;
//完成后优先级置0,方便后续操作
b.p.grade = 0;
}
//查看是否有比自己高等级的进程抢占CPU
for(int i = b.m; i> b.p.grade; i--)
{
//如果等待队列不为空
if(!empty(b.q[i]))
{
insert(b.q[b.p.grade], b.p);
if(b.p.grade) cout << "更高优先级进程插入," << b.p.name << "进入等待队列" << " ";
b.p = pop(b.q[i]);
cout << b.p.name << "开始运行" << " ";
b.state = 1;
break;
}
}
//如果此时还有运行程序打印运行信息
if(b.state) print_pcb(b);
}
//如果当前没有运行进程
if(!b.state)
{
for(int i = b.m; i> 0; i--)
{
//如果等待队列不为空
if(!empty(b.q[i]))
{
b.p = pop(b.q[i]);
cout << b.p.name << "开始运行" << " ";
b.state = 1;
break;
}
}
//如果等待队列为空
if(!b.state)
{
cout << "当前无可执行进程" << " ";
}
}
}
//--------------pcb---------------------//
//待执行进程总数
int n = 0;
//最多运行的时间
int all = 0;
//进程
process p[N];
//读入进程数据
void data()
{
ifstream readFile("data2.txt");
if(!readFile.is_open())
{
cout << "打开数据文件失败" << endl;
exit(0);
}
while(!readFile.eof())
{
readFile >> p[n].name >> p[n].arrive_time >> p[n].time >> p[n].grade;
if(all > p[n].arrive_time) all += p[n].time;
else all = p[n].arrive_time + p[n].time;
n++;
}
readFile.close();
}
int main()
{
int j = 0;
pcb b;
data();
for(; i <= all; i++)
{
cout << "第" << i << "时刻" << " ";
//进程到达,则进入等待队列
while(j < n && p[j].arrive_time == i)
{
insert(b.q[p[j].grade], p[j]);
cout << p[j].name << "进入等待队列" << " ";
j++;
}
update_pcb(b);
cout << endl;
}
return 0;
}
数据
p1 0 7 1
p2 2 4 2
p3 4 1 3
p4 5 4 2