时间片轮状调度算法

时间:2022-04-26 08:03:39

时间片轮转调度算法实现:

#include <iostream>
#include <string.h>
#include <ctime>
#include <windows.h>
using namespace std;
#define   BLOCK   -1;
#define   READY    0;
#define   RUNING   1;
int flag;
void Runing();
class Node
{
    public:
    string pid;                                              //进程ID
    int priority;                                            //进程的优先级
    int state;                                               //进程的状态
    double time;                                             //进程的运行时间
    Node *next;
    Node()
    {
        next=NULL;
    }
    Node(string _pid,int _priority,int _state,double _time)
    {
        pid=_pid;
        priority=_priority;
        state=_state;
        time=_time;
        next=NULL;
    }
    void display()
    {
        cout<<pid<<"   "<<priority;
        if(state==1)
        cout<<"         "<<"Runed"<<"       "<<time<<endl;
        else
        cout<<"         "<<"Ready"<<"       "<<time<<endl;
    }
    ~Node()
    {}
};
class linklist
{
    public:
    Node* head;
    Node* rear;
    linklist()
    {
        head=NULL;
        rear=NULL;
    }
    void InsertNode(string _pid,int _priority,int _state,double _time)
    {
        Node* p=new Node(_pid,_priority,_state,_time);
        if(rear==NULL)
        head=rear=p;
        else
        {
            rear->next=p;
            rear=rear->next;
        }
    }
    void DeleteHeadNode()
    {
        Node* p=head;
        cout<<"              运行结束的进程为"<<p->pid<<endl<<endl;
        if(p->next!=NULL)                                                //如果此时队列中有大于一个进程在排队
        head=head->next;
        else                                                             //队列中仅有一个进程在排队
        head=rear=NULL;
        delete p;
    }
    void ReInsertNode(Node* p)
    {
        p->state=1;
        rear->next=p;
        rear->state=0;
        rear=rear->next;
    }
    void Runing()
    {
        if(head==NULL||rear==NULL)
        {
            cout<<"           进程队列已空,进程全部运行完成!"<<endl;
            flag=0;
            return ;
        }
        else
        {
            Node* p=head;
            clock_t perruntime=0.1*CLOCKS_PER_SEC;              //为每个进程分配的时间片
            double time=clock();
            if(p->time>0.1)                                      //队首进程的剩余时间大于时间片时
            {
                while((clock()-time)<perruntime)
                ;
                p->time=p->time-0.1;
                if(p->time<0.001)                                   //进程恰好整时间片运行结束,删除队首进程
                {
                    DeleteHeadNode();
                    return;
                }
            }                                                   //队首进程的剩余时间小于时间片时
            else
            {
                while((clock()-time)<(p->time*CLOCKS_PER_SEC))   //把队首进程的剩余时间片运行完成后删除队首进程
                ;
                DeleteHeadNode();
                return;
            }
            if(head->next!=NULL)                                 //如果此时队列中有多于一个进程
            {
                head=head->next;
                p->next=NULL;
                ReInsertNode(p);
            }
        }
    }
    void display()
    {
        Node *p=head;
        cout<<"             当前就绪队列中的进程状态"<<endl;
        cout<<"********************************************************"<<endl;
        cout<<"PID        "<<"Priority  "<<"State   "<<"RuningNeedTime"<<endl;
        while(p!=NULL)
        {
            p->display();
            p=p->next;
        }
        cout<<"********************************************************"<<endl;
    }
};

int main()
{
    flag=1;
    linklist list;
    list.InsertNode("process1",4,0,0.5);
    list.InsertNode("process2",3,0,1.25);
    list.InsertNode("process3",2,0,0.8);
    list.InsertNode("process4",1,0,1);
    while(flag)
    {
        list.Runing();
        if(list.rear!=NULL)
        list.display();
        cout<<endl;
        //system("pause");
        //system("cls");
    }
    return 0;
}