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

📄 zydd.txt

📁 单道批处理系统的作业调度程序(短作业优先
💻 TXT
字号:
// zuoye.cpp : Defines the entry point for the console application.
//

#include <iostream.h>
#define WorkMAX 20
//---------------------------------------------------------
typedef struct zuoye                 /*作业控制快*/
   {char name;                       /*作业名*/
    float handintime;                /*提交时间*/              
    float runtime;                   /*运行时间*/
    int workspace;                   /*工作空间*/
    float starttime;                 /*开始时间*/
    float finishtime;                /*完成时间*/
    float T;                         /*周转时间*/
    float W;                         /*带权周转时间*/
    char condition;                  /*状态*/
   }JCB;
//---------------------------------------------------------
void turn_func(JCB work[WorkMAX],int i,int j) /*交换位置*/
{JCB bridge;
bridge=work[i];
work[i]=work[j];
work[j]=bridge;
}
//--------------------------------------------------------
int input_func(JCB work[WorkMAX])            /*输入函数*/
{int n,i,j;
cout<<"please input the work number:"<<endl; /*作业个数*/
cin>>n;
cout<<"please input the works message"<<endl; /*作业信息*/
cout<<"Workname Handintime Runtime:"<<endl;
for(i=0;i<n;i++)
  {cin>>work[i].name;
   cin>>work[i].handintime;
   cin>>work[i].runtime;
   work[i].condition='W';
  }
for(i=0;i<n-1;i++)                      /*按时间先后排序*/
     for(j=i+1;j<n;j++)
         if(work[i].handintime>work[j].handintime)
             turn_func(work,i,j);
return n;
}
//--------------------------------------------------------
int Fselect_func(JCB work[WorkMAX],int n,int s0)/*先来先服务*/
{int i,s1,nostop;
for(nostop=1,i=0;i<n&&nostop;i++)
   if(work[i].condition=='W')
     {s1=i;nostop=0;}
  return s1;
}
//-----------------------------------------------------------
int Sselect_func(JCB work[WorkMAX],int n,int s0)/*短作业优先*/
{int i,j,s1,count,nostop,a[WorkMAX];
for(nostop=1,i=0;i<n&&nostop;i++)
   if(work[i].condition=='W')
     {s1=i;nostop=0;}
if(s0!=-1)
{
     for(j=0,i=0;i<n;i++)
         if(work[i].condition=='W'&&work[i].handintime<=work[s0].finishtime)      
         {
            a[j]=i;
            j++;                  
         }
      count=j;
      if(count>1)
      {
        s1=a[0];
        for(j=1;j<count;j++)
            if(work[a[j]].runtime<work[s1].runtime)
               s1=a[j];
      }
} 
return s1;
}

//-------------------------------------------------------------------------
int Hselect_func(JCB work[WorkMAX],int n,int s0) /*最高响应比*/
{int i,j,s1,count,nostop,b[WorkMAX];
for(nostop=1,i=0;i<n&&nostop;i++)
   if(work[i].condition=='W')
     {s1=i;nostop=0;}
if(s0!=-1)
{
     for(j=0,i=0;i<n;i++)
         if(work[i].condition=='W'&&work[i].handintime<=work[s0].finishtime)      
         {
            b[j]=i;
            j++;                  
         }
      count=j;
      if(count>1)
      {
        s1=b[0];
        for(j=1;j<count;j++)
            if((work[s0].finishtime-work[b[j]].handintime)/work[b[j]].runtime>
                (work[s0].finishtime-work[s1].handintime)/work[s1].runtime)
               s1=b[j];
      }
} 
return s1;
}
//------------------------------------------------------------------------
void run_func(JCB work[WorkMAX],int s0,int s1)        /*作业运行函数*/
{ work[s1].condition='R';
  if(s0==-1)
    work[s1].starttime=work[s1].handintime;
  else if(work[s1].handintime>work[s0].finishtime)
    work[s1].starttime=work[s1].handintime;
  else work[s1].starttime=work[s0].finishtime;
  work[s1].finishtime=work[s1].starttime+work[s1].runtime;
  work[s1].condition='F';
  work[s1].T=work[s1].finishtime-work[s1].handintime;
  work[s1].W=work[s1].T/work[s1].runtime;
  cout<<work[s1].name;
}
//----------------------------------------------------------------
void print_func(JCB work[WorkMAX],int n)            /*输出*/
{int i;
float sumT=0,sumW=0;
for(i=0;i<n;i++)
   {sumT+=work[i].T;sumW+=work[i].W;}
cout<<endl;
cout<<"The average of T is:"<<sumT/n<<endl; 
cout<<"The average of W is:"<<sumW/n<<endl;
cout<<endl;  
}
//--------------------------------------------------------------
int main(int argc, char* argv[])                 /*模拟作业调度*/
{int k,n,s0,s1;
JCB work[WorkMAX];
n=input_func(work);                            /*输入*/
//---------------------------------------------------------------
cout<<"FCFS:"<<endl;                             /*先来先服务*/
for(k=0;k<n;k++)
    work[k].condition='W';
s0=-1;
for(k=0;k<n;k++)
{s1=Fselect_func(work,n,s0);
  run_func(work,s0,s1);
  s0=s1;
}
print_func(work,n);
//----------------------------------------------------------------- 
cout<<"Short work:"<<endl;                        /*短作业优先*/
for(k=0;k<n;k++)
  work[k].condition='W';
s0=-1;
for(k=0;k<n;k++)
{s1=Sselect_func(work,n,s0);
  run_func(work,s0,s1);
  s0=s1;
}
print_func(work,n);
//------------------------------------------------------------------     
cout<<"High respond:"<<endl;                        /*最高响应比*/
for(k=0;k<n;k++)
  work[k].condition='W';
s0=-1;
for(k=0;k<n;k++)
{s1=Hselect_func(work,n,s0);
  run_func(work,s0,s1);
  s0=s1;
}
print_func(work,n);
//------------------------------------------------------------------
    return 0;
}

⌨️ 快捷键说明

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