代码
/*
process:时间片轮转
author:lisuhang
time:2022-4-11
*/
#include
#include
using namespace std;
int const N = 100;
int i = 0;
//-------------进程--------------------//
//进程
typedef struct
{
//进程名称
string name;
//到达时间
int arrive_time;
//完成需要时间
int time;
}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
{
//正在执行的进程
process p;
//该进程已经占用CPU的时间
int time = 0;
//等待队列
queue q;
//当前进程状态
int state = 0;
// 时间片轮转时间
int const m = 2;
}pcb;
//输出当前pcb信息
void print_pcb(pcb b)
{
cout << "当前正在执行:" <> p[n].name >> p[n].arrive_time >> p[n].time;
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]);
cout << p[j].name << "进入等待队列" << " ";
j++;
}
update_pcb(b);
cout << endl;
}
return 0;
}
数据
p1 0 5
p2 2 4
p3 4 1
p4 5 6