操作系统SJF和HRN算法

SJF算法思想:

第一步:通过bubbleSort1()函数按到达时间小者优先进行排序,确定本调度算法的第一个作业。

第二步:通过getCount()函数获得后续作业的到达时间小于等于第一个作业的完成时间的数量count。

第三步:判断getCount()函数的返回值

若count等于0,则后续作业的开始时间等于该作业的到达时间

若count大于0,将第一个作业后的count个作业通过bubbleSort()按运行时间短者优先进行排序,确定第二个作业

重复第二、三步确定后续作业的排序。

HRN算法思想:

HRN算法与SJF算法相似,多了两个属性:nowTime(记录当前时间),ratio(记录作业响应比),另外第三步按ratio高者优先进行排序。

SJF,HRN输入数据:

操作系统SJF和HRN算法_第1张图片

SJF输出数据:

操作系统SJF和HRN算法_第2张图片

HRN输出数据:

操作系统SJF和HRN算法_第3张图片

注:在SJF算法中,我没有判断两个作业运行时间相同时的优先级,但是由于第一次排序是按提交时间进行的,所以我用着没问题,可能有些情况特殊,需要使用者手动添加一个判断语句,程序才能正确运行。

另外,我使用的是数组,你们也可以改写一下,改成指针的形式。

如有什么错误的地方,望指出。

SJF算法代码:

#include 

using namespace std;

class JCB
{
public:
    void input();
    void output();
    void bubbleSort(int start, int len);
    void bubbleSort1(int start, int len);
    int getCount(int x);
    void swap(int x, int y);
    void makeJCB(int x);
    void makeTime(int x);  // 一些数据的处理
    void init(); //初始化函数,貌似不需要
    void printCir();
private:
    int length;
    double arriveTime[10];
    double startTime[10];
    double workTime[10];
    double finishTime[10];
    double cirTime[10];
    double dqzzTime[10];
    string name[10];
};

void JCB::input()
{
    cout << "请输入作业个数(不大于10个): ";
    cin >> length;
    for(int i=0; i> name[i];
        cout << "请输入作业到达时间:"; cin >> arriveTime[i];
        cout << "请输入作业运行时间:"; cin >> workTime[i];
    }
    cout << "输入完成!" << endl;
}

void JCB::output()
{
    cout << endl;
    for(int i=0; i workTime[j+1])
                swap(j,j+1);
}

void JCB::bubbleSort1(int start, int len)
{
    for(int i=start; i arriveTime[j+1])
                swap(j,j+1);
}

int JCB::getCount(int x)
{
    int count = 0;
    int y = x-1;
    for(int i=x; i

HRN算法代码:

#include 

using namespace std;

class JCB
{
public:
    void input();
    void output();
    void bubbleSort(int start, int len);
    void bubbleSort1(int start, int len);
    int getCount(int x);
    void swap(int x, int y);
    void makeJCB(int x);
    void makeTime(int x);
    void init(); //貌似不需要这函数
    void printCir();
private:
    int length;
    double nowTime;
    double arriveTime[10];
    double startTime[10];
    double workTime[10];
    double finishTime[10];
    double cirTime[10];
    double dqzzTime[10];
    double ratio[10];
    string name[10];
};

void JCB::input()
{
    cout << "请输入作业个数(不大于10个)";
    cin >> length;
    for(int i=0; i> name[i];
        cout << "请输入作业到达时间:"; cin >> arriveTime[i];
        cout << "请输入作业运行时间:"; cin >> workTime[i];
    }
    cout << "输入完成!" << endl;
}

void JCB::output()
{
    cout << endl;
    for(int i=0; i arriveTime[j+1])
                swap(j,j+1);
}

int JCB::getCount(int x)
{
    int count = 0;
    int y = x-1;
    for(int i=x; i

你可能感兴趣的:(操作系统SJF和HRN算法)