操作系统 实验六 进程调度模拟(C++)

今日作业:
在这里插入图片描述
参考教程:
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(
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拔牙不打麻药

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值