时间片轮转法RRC++实现


该程序实现作业调度的RR算法,只要输入时间片,进程号,到达时间,运行所需时间即可,输出的是一条时间轴和对应的进程运行流程,表示一个时间点里哪个进程在运行。
资源截图
代码片段和文件信息
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAXN = 1000;//假设能够容纳的进程最多的个数
 
struct Process
{
    int pos;                          //代表第几个输入的进程
    char Process_name[50];            //进程名字,默认最长长度占用50字符
    double Arrival_time;             //进程到达时间
    double Service_time;             //服务时间       进程所需要的时间
    double Start_time;               //服务开始时间
    double End_time;                 //服务结束的时间
    double Turnaround_time;          //周转时间    进程结束的时间-进程到达时间
    double Weight_Turnaround_time;   //带权周转时间       周转时间 / 服务时间
}process[MAXN];
double every_time;
int n;
double temp[100];
double tmp;
double sumwait;
 
void Init()
{
    printf(“请输入时间片的时间:“);
    scanf(“%lf“&every_time);
    printf(“请输入进程的数量:“);
    scanf(“%d“&n);
    for(int i = 0;i < n;i ++)
    {
        cout<<“请输入第“<        scanf(“%s“process[i].Process_name);
        scanf(“%lf“&process[i].Arrival_time);
        scanf(“%lf“&process[i].Service_time);
        process[i].pos = i;
    }
}
bool cmp1(const Process &aconst Process &b)
{
    return a.Arrival_time < b.Arrival_time;
}
 
void Solve_TSRA()
{
    /*
    时间片轮转算法:
    */
    printf(“时间片轮转算法:
“);
    queue que;
    sort(processprocess+ncmp1);
    bool vis[MAXN];      //表示这个进程有没有在完成,完成使用true表示
    Process ans[MAXN];
    double time = max(0.0process[0].Arrival_time);int index = 0;
    memset(visfalsesizeof(vis));
    que.push(process[0]);vis[process[0].pos] = 1;
    for(int i = 0;i < n;i ++)
    {
        while(!que.empty())
        {
            Process temp = que.front();   que.pop();
            temp.Start_time = time;
            temp.End_time = temp.Service_time >= every_time ? time + every_time : time + temp.Service_time;
            for(int j = 0;j < n;j ++)
                if(!vis[process[j].pos] && process[j].Arrival_time <= temp.End_time)
                {
                    vis[process[j].pos] = 1;
                    que.push(process[j]);
                }
            if(temp.Service_time > every_time)
            {
                temp.Service_time -= every_time;
                que.push(temp);
                time += every_time;
            }
            else
                time += temp.Service_time;       //这里面的时间都是有联系的,所以不用再次更新time
            ans[index++] = temp;             //将顺序存储到最终的答案序列中
        }
        bool flag = false; //判断是否所有的进程都已经完成
        for(int j = 0;j < n;j ++)
            if(!vis[process[j].pos])
            {
                que.push(process[j]);//将一个时间最靠前的添加到队列中
                time = process[j].Arrival_time;   //这里就要更新time了,因为这里的时间和上面的有些脱节!
                flag = true;break;
            }
        if(!flag) break;
    }
    printf(“进程的运行顺序为:
“);
    for(int i = 0;i < index;i ++){
        printf(“%.0lf - %s - “ans[i].Start_timeans[i].Process_name);
if(i==index-1)
 pr

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      92672  2018-12-14 12:04  RRDebug操作系统6.4.exe

     文件     421600  2018-12-14 12:04  RRDebug操作系统6.4.ilk

     文件    1002496  2018-12-14 12:04  RRDebug操作系统6.4.pdb

     文件       3920  2018-12-14 11:32  RR操作系统6.46.4.cpp

     文件     277061  2018-12-14 12:04  RR操作系统6.4Debug6.4.obj

     文件       1414  2018-12-14 12:04  RR操作系统6.4Debugcl.command.1.tlog

     文件      26442  2018-12-14 12:04  RR操作系统6.4DebugCL.read.1.tlog

     文件        470  2018-12-27 11:16  RR操作系统6.4DebugCL.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink-cvtres.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink-cvtres.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink-rc.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink-rc.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10544-cvtres.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10544-cvtres.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10544-rc.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10544-rc.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10544.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10544.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10652-cvtres.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10652-cvtres.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10652-rc.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10652-rc.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10652.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.10652.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.11028-cvtres.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.11028-cvtres.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.11028-rc.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.11028-rc.write.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.11028.read.1.tlog

     文件          2  2018-12-14 12:04  RR操作系统6.4Debuglink.11028.write.1.tlog

............此处省略50个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)