操作系统实验(FCFS、SJF、PSA、HRRN)

#include<iostream>
#include<fstream>
#include<string>
#include<algorithm> 

using namespace std;
const int MAXJOB = 50;

struct job {
    int number;
    int reach_time;
    int need_time;
    int privilege;
    float excellent;
    int start_time;
    int wait_time;
    int visited;
    bool isreached;
};

job jobs[MAXJOB];
int quantity;

void initial_jobs() {
    for (int i = 0; i < MAXJOB; i++) {
        jobs[i].number = 0;
        jobs[i].reach_time = 0;
        jobs[i].privilege = 0;
        jobs[i].excellent = 0;
        jobs[i].start_time = 0;
        jobs[i].wait_time = 0;
        jobs[i].visited = 0;
        jobs[i].isreached = 0;
    }
    quantity = 0;
}

void reset_jinfo() {
    for (int i = 0; i < MAXJOB; i++) {
        jobs[i].start_time = 0;
        jobs[i].wait_time = 0;
        jobs[i].visited = 0;
    }
}

void readJobdata() {
    ifstream infile;
    int i;

    infile.open("job.txt", ios::in);
    if (!infile) {
        cerr << "open error!" << endl;
        exit(1);
    }
    else {
        while (!(infile.eof())) {
            infile >> jobs[quantity].number ;

            if(jobs[quantity].number == 0) continue;

            infile >> jobs[quantity].reach_time >> jobs[quantity].need_time
                >> jobs[quantity].privilege;
            quantity++;
        }

        cout << "output the origin job data" << endl;
        cout << "------------------------------------------------" << endl;
        cout << "\tjobID" << "\treachtime" << "\tneedtime" << "\tprivilege" << endl;

        for (i = 0; i < quantity; i++) {
            cout << "\t" << jobs[i].number << "\t" << jobs[i].reach_time << "\t\t" << jobs[i].need_time
                << "\t\t" << jobs[i].privilege << endl;
        }
    }
}

int findminjob(job jobs[], int count)

{
    int minjob = -1;//=jobs[0].need_time;
    int minloc = -1;
    for (int i = 0; i < count; i++)
    {
        if (minloc = -1)
        {
            if (jobs[i].isreached == 1 && jobs[i].visited == 0)
            {
                minjob = jobs[i].need_time;
                minloc = i;
            }
        }
        else if (minjob > jobs[i].need_time && jobs[i].visited == 0 && jobs[i].isreached == 1)
        {
            minjob = jobs[i].need_time;
            minloc = i;
        }
    }
    return minloc;
}

int findearlyjob(job jobs[], int count) {
    int rearlyloc = -1;
    int rearlyjob = -1;

    for (int i = 0; i < count; i++) {
        if (rearlyloc == -1) {
            if (jobs[i].visited == 0) {
                rearlyloc = i;
                rearlyjob = jobs[i].reach_time;
            }
        }
        else if (rearlyjob > jobs[i].reach_time && jobs[i].visited == 0) {
            rearlyjob = jobs[i].reach_time;
            rearlyloc = i;
        }
    }
    return rearlyloc;
}





//      2023-12-14
//这个实验让我认识到,GPT的技术还有待提高(可能我用的是免费的吧),在编码上很大程度会有错误,
//问了二十多遍,一次次整改,到最后花了2个多小时还没解决,对GPT感到很失望,也可能有部分是我文字描述不太清楚的问题吧
//结果,还得是自己慢慢看,不断调试,才解决

//任务调度的顺序应该是对的,其他的。。。

//短作业优先 find_job_SJF
int find_job_SJF(job jobs[], int count, int current)
{
    int id = -1, min_need_time = 0x3f3f3f3f;

    if (current == 0) {
        id = findearlyjob(jobs, count);
        /*cout << id + 1 << endl;
        puts("");*/
        return id;
    }

    for (int i = 0; i < count; i++)
    {
        if (jobs[i].reach_time > current)
            continue;
        if (jobs[i].visited)
            continue;
        if (jobs[i].need_time < min_need_time)
        {
            min_need_time = jobs[i].need_time;
            id = i;
        }
    }

    if (id == -1) {
        id = findearlyjob(jobs, count);
      /*  cout << id + 1 << endl;
        puts("");*/
    }

    return id;
}
void SJF() {
    int i;
    int current_time = 0;
    int loc;
    int total_waitime = 0;
    int total_roundtime = 0;

    reset_jinfo();

    cout << "SJF算法作业流" << endl;
    cout << "---------------------------------------------------------------" << endl;
    cout << "\tjobID" << "\treachtime" << "\tstarttime" << "\twaittime" << "\troundtime" << endl;

    for (i = 0; i < quantity; i++) {
        loc = find_job_SJF(jobs, quantity, current_time);

       // if (loc == -1) break;

        if (current_time > jobs[loc].reach_time)
            jobs[loc].wait_time = current_time - jobs[loc].reach_time;
        else jobs[loc].wait_time = 0;

        jobs[loc].start_time = max(current_time, jobs[loc].reach_time);

        cout << "\t" << jobs[loc].number << "\t" << jobs[loc].reach_time << "\t\t" << jobs[loc].start_time
            << "\t\t" << jobs[loc].wait_time << "\t\t"
            << jobs[loc].wait_time + jobs[loc].need_time << endl;

        jobs[loc].visited = 1;

        if (jobs[loc].wait_time == 0 && jobs[loc].reach_time > current_time)
            current_time = jobs[loc].reach_time;
        current_time += jobs[loc].need_time;

        total_waitime += jobs[loc].wait_time;
        total_roundtime = total_roundtime + jobs[loc].wait_time + jobs[loc].need_time;
    }

    cout << "总等待时间" << total_waitime << ',' << "总周转时间" << total_roundtime << ',';
    cout << "平均等待时间" << (float)total_waitime / quantity << ',' << "平均周转时间"
        << (float)total_roundtime / quantity << endl;
}

//先来先服务
void FCFS()

{

    int i;

    int current_time = 0;

    int loc;

    int total_waitime = 0;

    int total_roundtime = 0;

    //获得最早到达的作业

    loc = findearlyjob(jobs, quantity);

    //输出作业流

    cout << "FCFS算法作业流" << endl;

    cout << "---------------------------------------------------------------" << endl;

    cout << "\tjobID" << "\treachtime" << "\tstarttime" << "\twaittime" << "\troundtime" << endl;

    current_time = jobs[loc].reach_time;

    //每次循环找出最先到达的作业并打印相关信息

    for (i = 0; i < quantity; i++)

    {

        if (jobs[loc].reach_time > current_time)

        {

            jobs[loc].start_time = jobs[loc].reach_time;

            current_time = jobs[loc].reach_time;

        }

        else

        {

            jobs[loc].start_time = current_time;

        }

        jobs[loc].wait_time = current_time - jobs[loc].reach_time;

        cout << "\t" << loc + 1 << "\t" << jobs[loc].reach_time << "\t\t" << jobs[loc].start_time << "\t\t" << jobs[loc].wait_time << "\t\t" << jobs[loc].wait_time + jobs[loc].need_time << endl;

        jobs[loc].visited = 1;

        current_time += jobs[loc].need_time;

        total_waitime += jobs[loc].wait_time;

        total_roundtime = total_roundtime + jobs[loc].wait_time + jobs[loc].need_time;

        //获取剩余作业中最近到达作业

        loc = findearlyjob(jobs, quantity);

    }

    cout << "总等待时间" << total_waitime << ',' << "总周转时间" << total_roundtime << ',';

    cout << "平均等待时间" << (float)total_waitime / quantity << ',' << "平均周转时间" << (float)total_roundtime / quantity << endl;

}


//优先级调度算法
int find_pri_max(job jobs[], int count, int current)
{
    int id = -1, max_priority = -0x3f3f3f3f;

    if (current == 0) {
        id = findearlyjob(jobs, count);
        /*cout << id + 1 << endl;
        puts("");*/
        return id;
    }

    for (int i = 0; i < count; i++)
    {
        if (jobs[i].reach_time > current)
            continue;
        if (jobs[i].visited)
            continue;
        if (jobs[i].privilege > max_priority)
        {
            max_priority = jobs[i].privilege;
            id = i;
        }
    }

    if (id == -1) {
        id = findearlyjob(jobs, count);
        /* cout << id + 1 << endl;
         puts("");*/
    }

   /* cout << id + 1 << endl;
    puts("");*/

    return id;
}
void PSA()
{
    int i;
    int current_time = 0;
    int loc;
    int total_waitime = 0;
    int total_roundtime = 0;

    reset_jinfo();

    cout << "PSA算法作业流" << endl;
    cout << "---------------------------------------------------------------" << endl;
    cout << "\tjobID" << "\treachtime" << "\tstarttime" << "\twaittime" << "\troundtime" << endl;

    for (i = 0; i < quantity; i++) {
        loc = find_pri_max(jobs, quantity, current_time);

        if (loc == -1) break;

        if (current_time > jobs[loc].reach_time)
            jobs[loc].wait_time = current_time - jobs[loc].reach_time;
        else jobs[loc].wait_time = 0;

        jobs[loc].start_time = max(current_time, jobs[loc].reach_time);

        cout << "\t" << jobs[loc].number << "\t" << jobs[loc].reach_time << "\t\t" << jobs[loc].start_time
            << "\t\t" << jobs[loc].wait_time << "\t\t"
            << jobs[loc].wait_time + jobs[loc].need_time << endl;

        jobs[loc].visited = 1;

        if (jobs[loc].wait_time == 0 && jobs[loc].reach_time > current_time)
            current_time = jobs[loc].reach_time;

        if (jobs[loc].wait_time == 0 && jobs[loc].reach_time > current_time)
            current_time = jobs[loc].reach_time;
        current_time += jobs[loc].need_time;

        total_waitime += jobs[loc].wait_time;
        total_roundtime = total_roundtime + jobs[loc].wait_time + jobs[loc].need_time;
    }

    cout << "总等待时间" << total_waitime << ',' << "总周转时间" << total_roundtime << ',';
    cout << "平均等待时间" << (float)total_waitime / quantity << ',' << "平均周转时间"
        << (float)total_roundtime / quantity << endl;
}


//高响应比算法
int find_ratio_next(job jobs[], int count, int current)
{
    int id = -1;
    double max_ratio = -0x3f3f3f3f;

    if (current == 0)
    {
        id = findearlyjob(jobs, count);
        return id;
    }

    for (int i = 0; i < count; i++)
    {
        if (jobs[i].reach_time > current)
            continue;
        if (jobs[i].visited)
            continue;

        int wait = current - jobs[i].reach_time;
        double respond_ratio = 1 + (1.0 * wait / jobs[i].need_time);

        if (respond_ratio > max_ratio)
        {
            max_ratio = respond_ratio;
            id = i;
        }
    }

    if (id == -1) {
        id = findearlyjob(jobs, count);
        /* cout << id + 1 << endl;
         puts("");*/
    }

    return id;
}
void HRRN()
{
    int i;
    int current_time = 0;
    int loc;
    int total_waitime = 0;
    int total_roundtime = 0;

    reset_jinfo();

    cout << "HRRN算法作业流" << endl;
    cout << "---------------------------------------------------------------" << endl;
    cout << "\tjobID" << "\treachtime" << "\tstarttime" << "\twaittime" << "\troundtime" << endl;

    for (i = 0; i < quantity; i++) {
        loc = find_ratio_next(jobs, quantity, current_time);

        if (loc == -1) break;

        if (current_time > jobs[loc].reach_time)
            jobs[loc].wait_time = current_time - jobs[loc].reach_time;
        else jobs[loc].wait_time = 0;

        jobs[loc].start_time = max(current_time, jobs[loc].reach_time);

        cout << "\t" << jobs[loc].number << "\t" << jobs[loc].reach_time << "\t\t" << jobs[loc].start_time
            << "\t\t" << jobs[loc].wait_time << "\t\t"
            << jobs[loc].wait_time + jobs[loc].need_time << endl;

        jobs[loc].visited = 1;

        if (jobs[loc].wait_time == 0 && jobs[loc].reach_time > current_time)
            current_time = jobs[loc].reach_time;

        if (jobs[loc].wait_time == 0 && jobs[loc].reach_time > current_time)
            current_time = jobs[loc].reach_time;
        current_time += jobs[loc].need_time;

        total_waitime += jobs[loc].wait_time;
        total_roundtime = total_roundtime + jobs[loc].wait_time + jobs[loc].need_time;
    }

    cout << "总等待时间" << total_waitime << ',' << "总周转时间" << total_roundtime << ',';
    cout << "平均等待时间" << (float)total_waitime / quantity << ',' << "平均周转时间"
        << (float)total_roundtime / quantity << endl;
}


int main() 
{
    initial_jobs();
    readJobdata();

 //   HRRN();
// PSA();
//    SJF();
    FCFS();
    return 0;
}
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

leisure-pp

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

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

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

打赏作者

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

抵扣说明:

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

余额充值