今日作业:
参考教程:
https://blog.csdn.net/weixin_43824521/article/details/84621155
解析
优先级变化规则:
- 就绪队列中的进程经过一个时间片,priority+1;
- 阻塞中经过一个时间片,不变;
- 进程运行一个时间片,priority-3;
参数示例:
以下是结构体的结构
struct pcb
{
int ID;
int PRIORITY; //优先权
int CPUTIME; //已占用的cpu时间片
int ALLTIME; //还需要的时间
int STARTBLOCK; //运行多少个时间片之后开始进入BLOCKTIME
int BLOCKTIME; //阻塞的时间
int STATE; // 状态 0为就绪 1为阻塞 2为完成
};
队列:
vector<pcb> Ready_queue; //就绪队列
vector<pcb> Block_queue; //阻塞队列
vector<pcb> Finish_queue; //结束队列
输入进程的各个参数:
struct pcb process[] =
{
{
0, 9, 0, 3, 2, 3, 3, 0},
{
1, 38, 0, 3, 1, 1, 0},
{
2, 30, 0, 6, 4, 2, 0},
{
3, 29, 0, 3, 1, 1, 0},
{
4, 0, 0, 4, 1, 2, 0}
};
对这五个进程按照优先级进行排列:
if (Ready_queue.empty() && Block_queue.empty())
return; //当就绪和阻塞的队伍都为空时表示进程调度已经完成
else
{
for (int i = 0; i < Ready_queue.size() - 1; i++)
{
for (int j = i + 1; j < Ready_queue.size(); j++) //Ready_queue[i]在前,Ready_queue[j]在后
{
if (Ready_queue[i].PRIORITY < Ready_queue[j].PRIORITY) //从小到大按优先级对就绪队列进行排序
{
swap(Ready_queue[i], Ready_queue[j]);
}
}
}
}
输出变化后的各个队列:
if (!Ready_queue.empty())
{
cout << "就绪队列" << endl;
cout << "ID\tPRIORITY\tCPUTIME\tALLTIME\tSTARTBLOCK\tBLOCKTIME\tSTATE" << endl; //输出标题
for (int i = 0; i < Ready_queue.size(); i++)
{
cout << Ready_queue[i].ID << "\t" << Ready_queue[i].PRIORITY << "\t" << Ready_queue[i].CPUTIME << "\t" << Ready_queue[i].ALLTIME; //输出就绪队列中每一个进程的所有信息
cout << "\t" << Ready_queue[i].STARTBLOCK << "\t" << Ready_queue[i].BLOCKTIME << "\t" << Ready_queue[i].STATE << endl;
}
}
if (!Block_queue.empty(