多级反馈队列调度(模拟实现)

算法描述(来自百度):
1、进程在进入待调度的队列等待时,首先进入 优先级最高的Q1等待。
2、首先调度优先级高的队列中的进程。若高优先级中队列中已没有调度的进程,则调度次 优先级队列中的进程。例如:Q1,Q2,Q3三个队列,当且仅当在Q1中没有进程等待时才去调度Q2,同理,只有Q1,Q2都为空时才会去调度Q3。
3、对于同一个队列中的各个进程,按照FCFS分配时间片调度。比如Q1队列的 时间片为N,那么Q1中的作业在经历了N个时间片后若还没有完成,则进入Q2队列等待,若Q2的时间片用完后作业还不能完成,一直进入下一级队列,直至完成。
4、在最后一个队列QN中的各个进程,按照时间片轮转分配时间片调度。
5、在低 优先级的队列中的进程在运行时,又有新到达的作业,此时须立即把正在运行的进程放回当前队列的队尾,然后把处理机分给高优先级进程。换而言之,任何时刻,只有当第1~i-1队列全部为空时,才会去执行第i队列的进程( 抢占式)。特别说明,当再度运行到当前队列的该进程时, 仅分配上次还未完成的时间片,不再分配该队列对应的完整时间片
 
虽然算法描述已经十分详细了,但是有些资料上的描述不是很详细,这里再从申下重点:
1、该算法为抢占式算法
算法描述已经表达的很清楚了,只有高优先级队列为空时低优先级队列上的进程才可能得到运行,言下之意,若当前时刻cpu分配给了低优先级对列的进程,那么就意味者所有优先级高于当前队列的就绪队列均无进程,而此时若有新的进程进入高优先级的对列,那么当前进程立即停止执行,然后运行高优先级对列上的进程(有些人可能会认为会等该进程的时间片运行完了,才会再次调度,这和以上的算法描述显然不符,而我接触的一些关于该算法的算法描述都是前面的算法的描述,如果我所参考算法描述有误,请不吝指出,谢谢。)
2、当基于 1 中低优先级队列的正在运行的进程被cpu被抢占后重新进入当前队列的队尾后,时间片的分配在算法描述里也写的很清楚, 仅分配上次还未完成的时间片,不再分配该队列对应的完整时间片。
注意到以上两点应该没有什么大问题了。
 
模拟思路:
分析:
该算法的难点应该时对于不同类型的优先队列上进程运行时的处理;
主要时抢占时的对于个队列的处理,和当前进程的保护现场;
这里建议先分析队列的类型和进程的类型,然后考虑对应的操作
队列类型:Q1,Q2,Q3
Q1:最高优先级的队列
Q2:除最高优先级队列和最低优先级队列
Q3:最低优先级的队列
Q4:为了模拟进程边执行边进入队列的过程而设立的队列,在调度前将所有进程和相关信息存入,然后再调度过程中模拟进入系统
 
进程类型:P1,P2,P3,P4
P1:原本位于Q1的队列,被调度后得到CPU
P2:原本位于Q2的队列,被调度后得到CPU
P3:原本位于Q3的队列,被调度后得到CPU
P4:原本位于Q4的队列,当当前时间等于进程到达时间,将其从Q4中取出,并置入Q1队列的队尾
P5:位于Q1,Q2,Q3队列上的,为得到调度的进程,本算法不对其进行操作
 
思路:
可以对于每个时刻对所有进程队列和当前运行的状态进行监控,然后对不同的状态进行不同的响应;
那么显然对于P1,没有被抢占的可能,那么在其运行时,只需对与每个时刻考虑是否有新的进程进入Q1,
这里可以直接令其的已得到cpu时间直接加上其队列对应的时间片长度,然后用一个循环遍历该进程进入的时刻T_p_1到该进程运行完所得时间片t_s_1的时刻T_p_1+t_s_1,询问队列Q4是否有新的进程到达,若有,将其加入Q1。如果时间片用完仍未完成,令其进入下一队列的队尾,否则,将进程状态改为FINISHED,结束运行,让出cpu。
 
对于P2,存在被抢占的可能,那么在其运行时,用一个循环来遍历其得到cpu的时刻T_p_2到该进程运行完所得到的时间片的长度T_p_2+t_s_2,若无新进程到达,那么令其所得到cpu的时间加1个单位,若有,则将该队列加入其调度前所在队列的队尾。然后结束运行并让出
cpu。如果时间片用完仍未完成,令其进入下一队列的队尾,否则,将进程状态改为FINISHED,结束运行,让出cpu。(注意Q2,P2代表的对于操作相似的一类进程和队列,而不是特指某一和队列上的被调度进程和某一队列)
 
对于P3,存在被抢占的可能,那么在其运行时,用一个循环来遍历其得到cpu的时刻T_p_3到该进程运行完所得到的时间片的长度T_p_3+t_s_3,若无新进程到达,那么令其所得到cpu的时间加1个单位,若有,则将该队列加入其调度前所在队列的队尾。然后结束运行并让出
cpu。如果时间片用完仍未完成,令其进入当前队列的队尾,并置使用时间片为0,否则,将进程状态改为FINISHED,结束运行,让出cpu。
 
说了这么多,贴代码了:
 
#include<bits/stdc++.h>
using namespace std;
int  TIME_SLICE=1;
int  HIGHER_TIME_SLICE=1;
int MIDDLE_TIME_SLICE=1;
int LOWER_TIME_SLICE=1;
const string IN_HIGHER="in higher";
const string IN_MIDDLE="in middle";
const string IN_LOWER="in lower";
const string FREE="FREE";
const string HIGH="HIGH";
const string MIDDLE="MIDDLE";
const string LOWER="LOWER";
const string FINISH="FINISH";
const string READY="READY";
const string RUNNING="RUNNING";
class process
{
private:
    string process_name;
    string status;
    int arrive_time;
    int service_time;
    int ran_time;
    int time_slice=0;
    int now_time;
public:
    process(string process_name,int arrive_time,int service_time,int ran_time,string status);
    string get_process_name();
    void printf_infomation();
    int run(int now_time);
    bool is_arrive(int now_time);
    int get_arrive_time();
    process copyself();
};
list<process> higher_list;
list<process> middle_list;
list<process> lower_list;
list<process> process_list;
list<process>::iterator iter;
process process::copyself()
{
    string process_name=this->process_name;
    string status=this->status;
    int arrive_time=this->arrive_time;
    int service_time=this->service_time;
    int ran_time=this->ran_time;
    int time_slice=this->time_slice;
    int now_time=this->now_time;
    return process(process_name,arrive_time,service_time,ran_time,status);
}
process::process(string process_name,int arrive_time,int service_time,int ran_time,string status)
{
    this->process_name=process_name;
    this->arrive_time=arrive_time;
    this->service_time=service_time;
    this->ran_time=ran_time;
    this->status=status;
}
string process::get_process_name()
{
    return this->process_name;
}
void process::printf_infomation()
{
    cout<<"进程名 : "<<this->process_name<<endl;
    cout<<"到达时间 : "<<this->arrive_time<<endl;
    cout<<"需要运行的时间 : "<<this->service_time<<endl;
    cout<<"已使用cpu的时间  : "<<this->ran_time<<endl;
    cout<<"进程当前状态 : "<<this->status<<endl;
};
int  process::run(int now_time)
{
    this->now_time=now_time;
    this->status=RUNNING;
    cout<<"现在是 "<<this->now_time<<"时刻 。"<<endl;
    cout<<" 第一队列的进程如下:";
    for(iter=higher_list.begin(); iter!=higher_list.end(); iter++)
        cout<<iter->get_process_name()<<' ';
    cout<<endl;
    cout<<"第二队列的进程如下:";
    for(iter=middle_list.begin(); iter!=middle_list.end(); iter++)
        cout<<iter->get_process_name()<<' ';
    cout<<endl;
    cout<<"第三队列的进程如下:";
    for(iter=lower_list.begin(); iter!=lower_list.end(); iter++)
        cout<<iter->get_process_name()<<' ';
    cout<<endl<<endl;
    cout<<"正在运行的进程信息如下:"<<endl;
    this->printf_infomation();
    if(this->ran_time==0)
        {
        this->time_slice=min(this->service_time,HIGHER_TIME_SLICE);
            cout<<this->process_name<<"正在使用第一队列时间片!"<<endl<<endl;
        for(int i=1; i<=this->time_slice; i++)
            {
            this->now_time++;
            while(true)
                {
                if(!process_list.empty()&&process_list.front().is_arrive(this->now_time))
                    {
                    process first=process_list.front();
                    process_list.pop_front();
                    higher_list.push_back(first);
                    }
                else
                    {
                    break;
                    }
                }
            }
        this->ran_time+=this->time_slice;
        if(this->service_time==this->ran_time)
            {
            cout<<"现在是 "<<this->now_time<<"时刻,进程"<<this->process_name<<"已完成"<<"1"<<endl<<endl;
            }
        else
            {
            this->status=READY;
            process temp=this->copyself();
            middle_list.push_back(temp);
            }
        return this->now_time;
        }
    else if(this->ran_time<HIGHER_TIME_SLICE+MIDDLE_TIME_SLICE)
        {
        this->time_slice=MIDDLE_TIME_SLICE;
        cout<<this->process_name<<"正在使用第二队列时间片!"<<endl<<endl;
        for(int i=this->ran_time+1; i<=this->time_slice+HIGHER_TIME_SLICE&&i<=this->service_time; i++)
            {
            bool flag=false;
            this->now_time++;
            this->ran_time++;
            while(true)
                {
                if(!process_list.empty()&&process_list.front().is_arrive(this->now_time))
                    {
                    flag=true;
                    process first=process_list.front();
                    process_list.pop_front();
                    higher_list.push_back(first);
                    }
                else
                    {
                    break;
                    }
                }
            if(flag)
                {
                this->status=READY;
                 process temp=this->copyself();
                middle_list.push_back(temp);
                return this->now_time;
                }
            }
        if(this->ran_time==this->service_time)
            {
            cout<<"现在是 "<<this->now_time<<"时刻,进程"<<this->process_name<<"已完成"<<endl;
            }
        else
            {
            this->status=READY;
            process temp=this->copyself();
            lower_list.push_back(temp);
            }
        return this->now_time;
        }
    else
        {
        this->time_slice=LOWER_TIME_SLICE-((this->ran_time-HIGHER_TIME_SLICE-MIDDLE_TIME_SLICE)%LOWER_TIME_SLICE);
        cout<<this->process_name<<"正在使用第三队列时间片!"<<endl<<endl;
        bool flag=false;
        for(int i=1; i<=this->time_slice&&this->ran_time<this->service_time; i++)
            {
            this->now_time++;
            this->ran_time++;
            while(true)
                {
                if(!process_list.empty()&&process_list.front().is_arrive(this->now_time))
                    {
                    flag=true;
                    process first=process_list.front();
                    process_list.pop_front();
                    higher_list.push_back(first);
                    }
                else
                    {
                    break;
                    }
                }
            if(flag)
                {
                     process temp=this->copyself();
                lower_list.push_back(temp);
                return this->now_time;
                break;
                }
            }
        if(this->ran_time<this->service_time)
            {
            this->status=READY;
             process temp=this->copyself();
            lower_list.push_back(temp);
            return this->now_time;
            }
        else
            {
            this->status=FINISH;
            cout<<"现在是 "<<this->now_time<<"时刻,进程"<<this->process_name<<"已完成"<<"2"<<endl;
            return this->now_time;
            }
        }
}
bool  process::is_arrive(int now_time)
{
    if(this->arrive_time<=now_time)
        return true;
    else return false;
}
int  process::get_arrive_time()
{
    return this->arrive_time;
}
bool cmp(process A,process B)
{
    return A.get_arrive_time()<B.get_arrive_time();
}
bool is_finished()
{
    if(higher_list.empty()&&middle_list.empty()&&lower_list.empty()&&process_list.empty())
        {
        return true;
        }
    return false;
}
int now_time;
void dispatch()
{
    while(true)
        {
        if(is_finished())
        {
             break;
        }
        while(true)
            {
            if(!process_list.empty()&&process_list.front().is_arrive(now_time))
                {
                process first=process_list.front();
                process_list.pop_front();
                higher_list.push_back(first);
                }
            else
                {
                break;
                }
            }
        if(!higher_list.empty())
            {
            process first=higher_list.front();
            higher_list.pop_front();
            now_time=first.run(now_time);
            }
        else if(!middle_list.empty())
            {
            process first=middle_list.front();
            middle_list.pop_front();
            now_time=first.run(now_time);
            }
        else if(!lower_list.empty())
            {
            process first=lower_list.front();
            lower_list.pop_front();
            now_time=first.run(now_time);
            }
        else
            {
            now_time++;
            }
        }
    cout<<"********************************************************************************"<<endl<<endl;
    cout<<"现在是 : "<<now_time<<"时刻,系统暂无作业,调度结束!"<<endl;
    cout<<endl<<"********************************************************************************"<<endl;
}
void init()
{
    while(!process_list.empty())
        {
        process_list.pop_front();
        }
    HIGHER_TIME_SLICE=3;
    MIDDLE_TIME_SLICE=6;
    LOWER_TIME_SLICE=12;
    process p1("A",0,10,0,READY);
    process_list.push_back(p1);
    process p2("B",1,2,0,READY);
    process_list.push_back(p2);
    process p3("C",2,3,0,READY);
    process_list.push_back(p3);
    process p4("D",3,40,0,READY);
    process_list.push_back(p4);
    process p5("E",15,2,0,READY);
    process_list.push_back(p5);
}
void user_input_model()
{
    while(!process_list.empty())
        {
        process_list.pop_front();
        }
    int p_count;
    int time_slice;
    string p_name;
    int st_time;
    int se_time;
    cout<<"***************************************************************************************"<<endl;
    cout<<setw(40)<<setfill(' ')<<left<<"请输入要执行的进程数 : ";
    cin>>p_count;
    cout<<endl;
    cout<<setw(40)<<setfill(' ')<<left<<"请输入要设定的第一优先队列时间片大小 : ";
    cin>>time_slice;
    cout<<endl;
    HIGHER_TIME_SLICE =time_slice;
    cout<<setw(40)<<setfill(' ')<<left<<"请输入要设定的第二优先队列时间片大小 : ";
    cin>>time_slice;
    cout<<endl;
    MIDDLE_TIME_SLICE =time_slice;
    cout<<setw(40)<<setfill(' ')<<left<<"请输入要设定的第三优先队列时间片大小 : ";
    cin>>time_slice;
    cout<<endl;
    LOWER_TIME_SLICE =time_slice;
    for(int i=1; i<=p_count; i++)
        {
        cout<<setw(40)<<setfill(' ')<<left<<"请输入进程名 : ";
        cin>>p_name;
        cout<<setw(40)<<setfill(' ')<<left<<"请输入进程到达时间 : ";
        cin>>st_time;
        cout<<setw(40)<<setfill(' ')<<left<<"请输入服务时间 : ";
        cin>>se_time;
        cout<<endl;
        process p(p_name,st_time,se_time,0,READY);
        process_list.push_back(p);
        }
    cout<<"***************************************************************************************"<<endl<<endl;
    process_list.sort(cmp);
}
int main()
{
    init();
    cout<<"即将开始"<<endl;
    cout<<"3..."<<endl;
    cout<<"2..."<<endl;
    cout<<"1..."<<endl;
    cout<<endl;
    dispatch();
    return 0;
    return 0;
}
 
 
 

猜你喜欢

转载自www.cnblogs.com/RGBTH/p/9940454.html
今日推荐