(操作系统)C++ 高响应比优先调度

该文章提供了一个C++程序,用于模拟和实现操作系统的进程调度策略——高响应比优先(HighResponseRatioNext,HRRN)算法。程序首先读取进程的到达时间和运行时间,然后根据进程的到达时间排序并计算响应比,按响应比的降序进行调度,输出进程的开始时间、结束时间、周转时间和带权周转时间等信息。

【问题描述】编程实现进程调度中的高响应比优先调度算法。(括号内为相应的数字说明,不需要实现)
【输入形式】
【输出形式】
【样例输入】

3(表示进程个数)

process1   0    4(分别表示进程名称,到达时间和运行时间)

process2    2    7

process3    1   8   
【样例输出】(按照调度顺序输出,下列各列分别表示进程名,到达时间,开始时间,运行时间,完成时间,周转时间,带权周转时间)

(每列之间用一个空格分割)

process1 0 0 4 4 4 1
process3 1 4 8 12 11 1.375
process2 2 12 7 19 17 2.42857

10.6667(表示平均周转时间)

1.60119(表示平均带权周转时间)

#include <vector>
#include <stack>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>

using namespace std;

class PCB {
public:
    PCB() {
        cin >> pname >> arrivetime >> runtime;
        begintime = 0;
        endtime = 0;
        turntime = 0;
        weight_turn = 0;
        remain_time = runtime; // 设置剩余时间
        flag = false; // 设置执行标志为false,表示还未执行完毕
    }
    int get_arrivetime() {
        return arrivetime;
    }
    int get_runtime() {
        return runtime;
    }
    int get_begintime() {
        return begintime;
    }
    int get_endtime() {
        return endtime;
    }
    int get_turntime() {
        return turntime;
    }
    int get_remaintime() {
        return remain_time;
    }
    int get_flag() {
        return flag;
    }
    string get_pname() {
        return pname;
    }
    float get_weighturn() {
        return weight_turn;
    }
    void setbegintime(int begintime) {
        this->begintime = begintime;
    }
    void setendtime(int endtime) {
        this->endtime = endtime;
    }
    void setturntime(int turntime) {
        this->turntime = turntime;
    }
    void setweight(float weight_turn) {
        this->weight_turn = weight_turn;
    }
    void set_flag() {
        this->flag = true;
    }
    void setremaintime(int remain_time) {
        this->remain_time = remain_time;
    }
    float get_ratio() {
        return ratio;
    }

    void set_ratio(int time) { // 类型改为 void
        ratio = 1 + (float)(time - arrivetime) / runtime; // 直接修改 ratio 的值即可
    }
private:
    string pname;
    int arrivetime;//到达时间
    int begintime;//开始时间
    int runtime;//运行时间
    int endtime;//完成时间
    int turntime;//周转时间
    float weight_turn;//带权周转时间
    int remain_time;
    bool flag;
    float ratio;
};

bool compareByArr(PCB& p1, PCB& p2) {
    int t1 = p1.get_arrivetime();
    int t2 = p2.get_arrivetime();
    return t1 < t2;
}

bool compareByRatio(PCB& p1, PCB& p2) {
    float t1 = p1.get_ratio(); // 改为比较浮点数
    float t2 = p2.get_ratio();
    return t1 > t2; // 注意是高响应比优先,需要按照降序排列
}

void H(vector<PCB>* p) {
    sort(p->begin(), p->end(), compareByArr);
    queue<PCB> Ready_queue;//就绪队列
    queue<PCB> Coming_queue;//到达队列
    queue<PCB> Finish_queue;//完成队列
    for (vector<PCB>::iterator it = p->begin(); it != p->end(); it++) {
        Coming_queue.push(*it);
    }
    int i = 1, time = 0;
    while (Coming_queue.empty() == false || Ready_queue.empty() == false) {
        while (Coming_queue.empty() == false && time >= Coming_queue.front().get_arrivetime()) {
            Ready_queue.push(Coming_queue.front());
            Coming_queue.pop();
        }
        if (Ready_queue.empty()) {
            time++;
            continue;
        }
        else {
            //std::cout << "第" << i << "次调度执行进程:" << Ready_queue.front().get_pname() << "\t时间:" << time << endl;
            PCB temp = Ready_queue.front();
            Ready_queue.pop();
            float remain_time = temp.get_remaintime();
            if (remain_time == temp.get_runtime())
                temp.setbegintime(time);
            while (remain_time > 0) {
                time++;
                while (Coming_queue.empty() == false && time >= Coming_queue.front().get_arrivetime()) {
                    Ready_queue.push(Coming_queue.front());
                    Coming_queue.pop();
                }
                remain_time--;
                temp.setremaintime(remain_time);
                if (remain_time == 0) {
                    temp.set_flag();
                    temp.setendtime(time);
                    Finish_queue.push(temp);
                    break;
                }
            }
            vector<PCB>* temp1=new vector<PCB>[Ready_queue.size()];
            while(!Ready_queue.empty()){
                PCB r=Ready_queue.front();
                Ready_queue.pop();
                r.set_ratio(time);
                temp1->push_back(r);
            }
            if(Finish_queue.size()<p->size()){
                sort(temp1->begin(),temp1->end(),compareByRatio);
                for(vector<PCB>::iterator it=temp1->begin();it!=temp1->end();it++){
                    Ready_queue.push(*it);
                }
            }
        }
        i++;
    }
//打印输出结果
    float avgturn=0.0,  weighturn=0.0;
    //std::cout << "进程名\t    到达时间 开始时间 运行时间 完成时间 周转时间 带权周转时间\n";
    while (!Finish_queue.empty()) {
        float round_time = Finish_queue.front().get_endtime() - Finish_queue.front().get_arrivetime();
        float weight_round = round_time / Finish_queue.front().get_runtime();
        Finish_queue.front().setturntime(round_time);
        Finish_queue.front().setweight(weight_round);
        avgturn += round_time;
        weighturn += weight_round;
        std::cout << Finish_queue.front().get_pname()
                  << ' ' << Finish_queue.front().get_arrivetime()
                  << ' ' << Finish_queue.front().get_begintime()
                  << ' ' << Finish_queue.front().get_runtime()
                  << ' ' << Finish_queue.front().get_endtime()
                  << ' ' << Finish_queue.front().get_turntime()
                  << ' ' << Finish_queue.front().get_weighturn() << endl;
        Finish_queue.pop();
    }
    std::cout <<avgturn/p->size()<< endl;
    std::cout <<weighturn / p->size() << endl;
}

int main()
{
    vector<PCB>*p;
    int n;
    cin>>n;//输入进程数
    p=new vector<PCB>[n];
    for(int i=0;i<n;i++){
        PCB temp;
        p->push_back(temp);
    }
    H(p);
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值