cpu scheduler

cpu scheduler

  • Solution
    1. pre process
      • sort based on arrival time
    2. update based on queue
      !queue.isEmpty || i < len
    3. Calculate Turn Around and Waiting
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

class Process
{
    char pid;
    int brust;
    int reminder;
    int arrival;
    int start;
    int end;
    int turnAround;
    int waiting;

public:

    Process(char p, int b, int a, int s, int e, int t, int w)
            :pid(p), brust(b),reminder(b), arrival(a), start(s), end(e), turnAround(t), waiting(w){};

    void print(Process * process)
    {
        cout << process->pid << endl;
    }

    static void printSummary(const vector<Process *> myVector)
    {
        cout << "PID\t\t" << "TT\t\t\t" << "WT" << endl;

        int TTsum = 0, WTsum = 0;
        for (auto& it : myVector) {
            TTsum += it->turnAround;
            WTsum += it->waiting;
            cout << it->pid << "\t\t" << it->turnAround << "\t\t\t" << it->waiting << endl;
        }
        cout << "Ave\t\t" << TTsum << "/" << myVector.size() << "=" << 1.0 * TTsum/myVector.size() << "\t"
                        << WTsum << "/" << myVector.size() << "=" << 1.0 * WTsum/myVector.size() << endl;
    }

    static bool compare_arrival(const Process * a, const Process * b)
    {
        return a->arrival - b->arrival < 0;
    }

    static bool compare_brust(const Process * a, const Process * b) {
        return a->brust - b->brust < 0;
    }

    struct Comparator
    {
        bool operator()(const Process* lhs, const Process* rhs)
        {
            return lhs->brust > rhs->brust;
        }
    };

    static void SRTFScheduler(const vector<Process *> myVector, int quantum)
    {
        priority_queue<Process *,vector<Process *>, Comparator> pq;
        int T = -1;
        int index = 0;
        int len = myVector.size();

        while (pq.size() != 0 || index < len)
        {
            if (pq.size() != 0)
            {
                Process *top = pq.top();
                if (top->start == -1)
                {
                    top->start = T;
                }
                -- top->reminder;

                pq.pop();
                if (top->reminder == 0)
                {
                    top->end = T;
                    top->turnAround = top->end - top->arrival;
                    top->waiting = top->start - top->arrival;
//                    pq.pop();
                }
                else {
                    pq.push(top);
                }
            }
            ++ T;
            // fill arrival processes to queue
            for (;index < len && (myVector[index]->arrival) <= T; ++ index)
            {
                pq.push(myVector[index]);
            }
        }
    }

    static void SJFScheduler(const vector<Process *> myVector, int quantum)
    {
        priority_queue<Process *,vector<Process *>, Comparator> pq;
        int T = -1;
        int index = 0;
        int len = myVector.size();

        while (pq.size() != 0 || index < len)
        {
            if (pq.size() != 0)
            {
                Process *top = pq.top();
                top->start = T;
                T += top->brust;
                top->end = T;
                top->turnAround = top->end - top->arrival;
                top->waiting = top->start - top->arrival;

                pq.pop();
            }
            else
            {
                ++ T;
            }

            // fill arrival processes to queue
            for (;index < len && (myVector[index]->arrival) <= T; ++ index)
            {
                pq.push(myVector[index]);
            }
        }
    }

    static void RRScheduler(const vector<Process *> myVector, int quantum)
    {
        queue<Process *> q;
        int T = -1;
        int index = 0;
        int len = myVector.size();
        while (q.size() != 0 || index < len)
        {
            Process *front = nullptr;
            if (q.size() != 0)
            {
                front = q.front();
                if (front->start == -1)
                {
                    front->start = T;
                }
                for (int i = 0; i < quantum && front->reminder > 0; ++ i)
                {
                    -- front->reminder;
                    ++ T;
                }
                if (front->reminder == 0)
                {
                    front->end = T;
                    front->turnAround = front->end - front->arrival;
                    front->waiting = front->start - front->arrival;
                }
                q.pop();
            }
            else
            {
                ++ T;
            }

            // fill arrival processes to queue
            for (;index < len && (myVector[index]->arrival) <= T; ++ index)
            {
                q.push(myVector[index]);
            }

            if (front != nullptr && front->reminder > 0)
            {
                q.push(front);
            }
        }

    }

};

int main() {
    cout<<"Hello World\n";

    const int SIZE = 5;
    char pid[SIZE] = {'A', 'B', 'C', 'D', 'E'};
    int brust[SIZE] = {10, 1, 2, 1, 5};
    int arrival[SIZE] = {0, 1, 3, 0, 1};

    vector<Process *> myVector;
    for(int i = 0; i < 5; ++ i) {
        Process *rr = new Process(pid[i], brust[i], arrival[i], -1, -1, -1, -1);
        myVector.push_back(rr);
    }

    sort(myVector.begin(), myVector.end(), Process::compare_arrival);
//    Process::RRScheduler(myVector, 3);
//    Process::printSummary(myVector);

//    Process::SJFScheduler(myVector, 3);
//    Process::printSummary(myVector);

    Process::SRTFScheduler(myVector, 3);
    Process::printSummary(myVector);

    return 0;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 越长大越觉得自己活的不够纯粹。看着镜子里逐渐浑浊的双眼,我常常反思自己是否在向自己讨厌的方向发展。 我总是担心自己...
    迦南知识铺阅读 195评论 0 0
  • 远家思故土,僻壤生穷根, 本在孤山逸,双路出平户。 与云与海望,遥遥不见亲。 泪落念爹娘,面无其颜色。 心痛由自知...
    回眸三生琥珀色_阅读 260评论 1 3