主题:计算机操作系统实验—作业调度(2)
//上接 计算机操作系统实验—作业调度(1)
//-------------------------------------------------------------------------
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;
}
//-------------------------------------------------------------------------
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;
}