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

#include
#include
#include
#include 

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;
}

你可能感兴趣的:(Linux,算法,c++,数据结构)