⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fcfs.cpp

📁 操作系统作业调度FCFS算法模拟程序。根据先来先服务原则对提交作业进行调度执行。
💻 CPP
字号:
/*
程序采用FCFS算法模拟作业运行,程序运行时先输入作业个数,并依次输入作业信息,
输入过程中作业信息构成一个全局链表,表头为head,表尾last,每次新来的作业插在表尾;
运行时,每次取表头节点投入运行,head指针往后移动一位直至表为空,每次运行将各作业
的状态显示出来,显示有正在运行的作业及在等待链表里的作业,每个作业运行结束后,将
全局跟踪CPU时刻变量加上刚运行完作业的运行时间 ,然后自动调用释放函数,将该作业的
提交时间、投入运行时刻、结束时刻、周转时间及带权周转时间显示出来,将总周转时间、
总带权周转时间更新 ,然后释放该作业单位。所有作业运行完后将平均周转时间、平均带
权周转时间计算出来并显示。 
*/ 

#include<iostream>
#include<string>
using namespace std;
#define null 0
int Runtime=0;//CPU时刻跟踪变量 
float T_time=0; //总周转时间 
float W_time=0;//总带权周转时间 
int n;
typedef struct jcb{
string name;
int Ttime;
int Rtime;
int Sourse;
char State;
struct jcb *next;
}JCB;                                //作业信息表 
JCB *head=null,*last=null,*p;    //链表头、链尾及机动指针定义 
void insert()    //向链表插入作业函数 
{
 if(head==null)
      {
       head=p;
       last=p;
      }
 else {last->next=p;last=p;}
}
int randtime(int T,int R)//根据前一个作业到达时间及总完成时间产生当前作业到来的随机时间 
{
    return T+rand()%(R-T-1)+1;
}
void display(JCB *pr)//显示pr指针所指作业信息 
{
     cout<<pr->name<<'\t'<<pr->State<<'\t'<<pr->Sourse<<'\t'<<pr->Ttime<<'\t'<<pr->Rtime<<'\n';
}
void input()//输入函数 
{
     int T=0,R=0;
     cout<<"请输入作业个数:";
     cin>>n;
     for(int i=1;i<=n;i++)
     {
       p=new JCB; 
      cout<<"输入NO."<<i<<"作业名称:";
      cin>> p->name;
      cout<<"输入作业运行时间:";
      cin>>p->Rtime;
      
      cout<<"输入作业所需资源:";
      cin>>p->Sourse;
      p->next=null;
      p->State='W';
      if(i==1) p->Ttime=0;
      else p->Ttime=randtime(T,R);
      R+=p->Rtime;
      T=p->Ttime;
      insert();
     } 
      cout<<"\n\n输入完毕\n\n提出请求的作业队列为:\n";
    JCB *pr=head;
  cout<<"name\tstate\tSourse\tTtime\tRtime\n";
  while(pr)
  {
           display(pr);
           pr=pr->next;
  }
 system("pause");  
 cout<<"\n\n开始运行!\n\n";
}
int getlen()//返回链表长度 
{
    int l=0;
    JCB *pr=head;
    
    while(pr)
    {
     l++;
     pr=pr->next;
    }
    return (l);
} 
void show()//显示当前CPU内作业运行状态 
{
     cout<<"\t正在运行的作业\n";
     cout<<"name\tState\tSourse\tTtime\tRtime\n";
     display(p);
     cout<<"\t等待的作业\n";
     JCB *pr=head;
     if(!pr) {cout<<"\t无就绪进程!\n";return ;}
    cout<<"name\tState\tSourse\tTtime\tRtime\n";
     while(pr)
     {
       display(pr);
       pr=pr->next;
     }
}
void destroy()//作业运行完毕,撤销并释放内存 
{  p->State='F';
  cout<<"作业运行完成\n" ;  
  cout<<"Ttime\tBegin\tDone\tZ_time\tW_time\n";
 cout<<p->Ttime<<'\t'<<Runtime-p->Rtime<<'\t'<<Runtime<<'\t'<<Runtime-p->Ttime<<'\t'<<(float)(Runtime-p->Ttime)/p->Rtime<<endl; 
 T_time+=(float)Runtime-p->Ttime;
 W_time+=(float)(Runtime-p->Ttime)/p->Rtime;
 system("pause");
  delete p;
}
void run()//模拟运行函数 
{
     system("cls");
     p=head;
     head=p->next;
     p->State='R';
     show();
     Runtime+=p->Rtime;
     system("pause");
     system("cls");
     destroy();
}
int main()//主函数 
{
    input();
    while(1)
    {
            run();
            if(!getlen())
            break;
    }
    system("cls");
    cout<<"所有作业都已完成后\n\n";
    cout<<"AVG_T_time\tAVG_W_time\n";
    cout<<T_time/n<<'\t'<<'\t'<<W_time/n<<endl;
    system("pause");    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -