生活随笔
收集整理的這篇文章主要介紹了
作业调度算法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <stdio.h>
#include <string.h>
#include <conio.h>/*conio.h是一個(gè)包含一些字符處理函數(shù)的頭文件,如getch(),
getch()是無(wú)顯示的從鍵盤(pán)接收一個(gè)字符,有顯示的接收是getchar()*/
typedef char string[10]; /* //定義string為含有10個(gè)字符元素的字符數(shù)組類(lèi)型*/
struct task {
string name; /*作業(yè)號(hào)*/
int arrTime; /* 作業(yè)到達(dá)時(shí)間*/
int serTime; /*作業(yè)要求服務(wù)時(shí)間*/
int waiTime; /*等待時(shí)間*/
int begTime; /*開(kāi)始運(yùn)行時(shí)間*/
int finTime; /*結(jié)束運(yùn)行時(shí)間*/
int turTime; /*周轉(zhuǎn)時(shí)間*/
int wTuTime; /*帶權(quán)周轉(zhuǎn)時(shí)間*/
int priority;/*優(yōu)先權(quán)*/
int finish;/*是否已經(jīng)完成*/
}JCB[5];
int num;
void input()
{
int i;
system("cls");
printf("\nPlease input task number: ");
scanf("%d", &num);
for(i=0;i<num;i++)
{
printf("\nPlease input task NO.%d:\n",i);
printf(" The name of task: ");
scanf("%s",JCB[i].name);
printf(" The time arrive: ");
scanf("%d",&JCB[i].arrTime);
printf(" The time need: ");
scanf("%d",&JCB[i].serTime);
JCB[i].priority = 0;
JCB[i].finish =0;
}
}
int FCFS()
{
int current;
int i,j;
for(i=0; i<num; i++)
{
if(!JCB[i].finish)
{
current=i; /* 找到第一個(gè)還沒(méi)完成的作業(yè)*/
break;
}
}
for( j=i; j<num; j++) /* 和后面的作業(yè)比較*/
{
if(!JCB[j].finish && JCB[j].arrTime<JCB[current].arrTime)
{
current=j; /*找出先來(lái)的未完成作業(yè)*/
}
}
return current; /* 返回當(dāng)前作業(yè)*/
}
int SJF(int pre)
{
int current,i,j;
for(i=0; i<num; i++)
{
if(!JCB[i].finish)
{
current=i; /*找到第一個(gè)還沒(méi)完成的作業(yè)*/
break;
}
}
for(j=i; j<num; j++) /* 和后面的作業(yè)比較*/
{
if( !JCB[j].finish) /* 還沒(méi)完成(運(yùn)行)*/
{
if(JCB[current].arrTime<=JCB[pre].finTime)/*如果作業(yè)在上一個(gè)作業(yè)完成之前到達(dá)*/
{
if(JCB[j].arrTime<=JCB[pre].finTime && JCB[j].serTime<JCB[current].serTime )
current=j; /* 找出到達(dá)時(shí)間在上一個(gè)作業(yè)完成之前,服務(wù)時(shí)間比較小的未完成作業(yè)*/
}
else /*如果作業(yè)是在上一個(gè)作業(yè)完成之后到達(dá)*/
{
if(JCB[j].arrTime<JCB[current].arrTime)
current=j; /*找出比較早到達(dá)的一個(gè)*/
if(JCB[j].arrTime==JCB[current].arrTime) /*如果同時(shí)到達(dá)*/
if(JCB[j].serTime<JCB[current].serTime)
current=j; /*找出服務(wù)時(shí)間比較短的一個(gè)*/
}
}
}
return current; /*返回當(dāng)前作業(yè)*/
}
int HRN(int pre)
{
int current=1,i,j;
/* 優(yōu)先權(quán) =(等待時(shí)間+服務(wù)時(shí)間)/服務(wù)時(shí)間*/
for(i=0; i<num; i++)
{
JCB[i].waiTime=JCB[pre].finTime-JCB[i].arrTime; /*等待時(shí)間 =上一個(gè)作業(yè)的完成時(shí)間-到達(dá)時(shí)間*/
JCB[i].priority=(JCB[i].waiTime+JCB[i].serTime)/JCB[i].serTime;
}
for(i=0; i<num; i++)
{
if(!JCB[i].finish)
{
current=i; /*找到第一個(gè)還沒(méi)完成的作業(yè)*/
break;
}
}
for( j=i; j<num; j++) /*和后面的作業(yè)比較*/
{
if( !JCB[j].finish) /* 還沒(méi)完成(運(yùn)行)*/
{
if(JCB[current].arrTime<=JCB[pre].finTime) /*如果作業(yè)在上一個(gè)作業(yè)完成之前到達(dá)*/
{
if(JCB[j].arrTime<=JCB[pre].finTime && JCB[j].priority>JCB[current].priority )
current=j;/* 找出到達(dá)時(shí)間在上一個(gè)作業(yè)完成之前,優(yōu)先權(quán)高的作業(yè)*/
}
else /* 如果作業(yè)是在上一個(gè)作業(yè)完成之后到達(dá)*/
{
if(JCB[j].arrTime<JCB[current].arrTime)
current=j; /* 找出比較早到達(dá)的一個(gè)*/
if(JCB[j].arrTime==JCB[current].arrTime) /* 如果同時(shí)到達(dá)*/
if(JCB[j].priority>JCB[current].priority)
current=j; /*找出服務(wù)時(shí)間比較短的一個(gè)*/
}
}
}
return current;/*返回當(dāng)前作業(yè)*/
}
void runing(int i, int times, int pre, int staTime, int endTime)
{
if(times==0)
{
JCB[i].begTime=JCB[i].arrTime;
JCB[i].finTime=JCB[i].begTime+JCB[i].serTime;
JCB[i].turTime=JCB[i].serTime;
JCB[i].wTuTime=1.0;
staTime=JCB[i].begTime;
}
else
{
if(JCB[i].arrTime>JCB[pre].finTime)
JCB[i].begTime=JCB[i].arrTime;
else
JCB[i].begTime=JCB[pre].finTime;
JCB[i].finTime=JCB[i].begTime+JCB[i].serTime;
JCB[i].turTime=JCB[i].finTime-JCB[i].arrTime;
JCB[i].wTuTime=JCB[i].turTime/JCB[i].serTime;
}
if(times==num-1)
endTime=JCB[i].finTime;
JCB[i].finish=1;
}
void print(int i,int times)
{
if(times==0)
{
printf(" name arrTime serTime begTime finTime turTime wTuTime\n");
}
printf("%9s%9d%9d%9d%9d%9df%9df\n",
JCB[i].name,JCB[i].arrTime,JCB[i].serTime,
JCB[i].begTime,JCB[i].finTime,JCB[i].turTime,JCB[i].wTuTime);
}
void check( )
{
int i;
int staTime, endTime, sumTurTime=0.0, sumWTuTime=0.0, aveTurTime, aveWTuTime;
int current=0, times=0, pre=0;
JCB[pre].finTime=0;
printf("\n-- FCFS -----------------------------------------------------------------\n");
for(times=0; times<num; times++)
{
current=FCFS();
runing(current, times, pre, staTime, endTime);
print(current, times);
pre=current;
}
for(i=0; i<num; i++)
{
sumTurTime+=JCB[i].turTime;
sumWTuTime+=JCB[i].wTuTime;
}
aveTurTime=sumTurTime/num;
aveWTuTime=sumWTuTime/num;
printf("(total) %9.2f%9.2f%9.2f%9.2f\n",staTime,endTime,aveTurTime,aveWTuTime);
printf("-------------------------------------------------------------------------\n");
for(i=0; i<num; i++)
{
JCB[i].finish=0;
}
staTime, endTime, sumTurTime=0.0, sumWTuTime=0.0, aveTurTime, aveWTuTime;
current=0; times=0; pre=0;
JCB[pre].finTime=0;
printf("\n-- SJF ------------------------------------------------------------------\n");
for(times=0; times<num; times++)
{
current=SJF(pre);
runing(current, times, pre, staTime, endTime);
print(current, times);
pre=current;
}
for(i=0; i<num; i++)
{
sumTurTime+=JCB[i].turTime;
sumWTuTime+=JCB[i].wTuTime;
}
aveTurTime=sumTurTime/num;
aveWTuTime=sumWTuTime/num;
printf("(total) %9d%9d%9d%9d\n",staTime,endTime,aveTurTime,aveWTuTime);
printf("-------------------------------------------------------------------------\n");
for(i=0; i<num; i++)
{
JCB[i].finish=0;
}
staTime, endTime, sumTurTime=0.0, sumWTuTime=0.0, aveTurTime, aveWTuTime;
current=0; times=0; pre=0;
JCB[pre].finTime=0;
printf("\n-- HRN ------------------------------------------------------------------\n");
for(times=0; times<num; times++)
{
current=HRN(pre);
runing(current, times, pre, staTime, endTime);
print(current, times);
pre=current;
}
for(i=0; i<num; i++)
{
sumTurTime+=JCB[i].turTime;
sumWTuTime+=JCB[i].wTuTime;
}
aveTurTime=sumTurTime/num;
aveWTuTime=sumWTuTime/num;
printf("(total) %9d%9d%9d f%9df\n",staTime,endTime,aveTurTime,aveWTuTime);
printf("-------------------------------------------------------------------------\n");
}
void main()
{
char again;
do {
system("cls"); /*清屏*/
printf("please input 4 groups of datas:\n");
input();
check();
printf("Continue...(Y/N): ");
do{
again = getch();
}while(again!='Y' && again!='y' && again!='N' && again!='n');
}while(again=='Y' || again=='y');
}
總結(jié)
以上是生活随笔為你收集整理的作业调度算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。