導航:首頁 > 源碼編譯 > 優先調度演算法

優先調度演算法

發布時間:2022-02-13 07:57:24

『壹』 短作業優先調度演算法和優先順序為基礎的非搶占式調度演算法

短進程優先演算法是一種非剝奪式演算法,總是選取預計作業時間最短的作業優先運行;最短剩餘時間優先演算法是非剝奪式的,但可以改造成剝奪式的調度演算法,稱搶占式最短作業優先演算法。

『貳』 優先順序調度演算法是什麼

優先順序演算法是指在進程創建時先確定一個初始優先數,以後在進程運行中隨著進程特性的改變不斷修改優先數,這樣,由於開始優先數很低而得不到CPU的進程,就能因為等待時間的增長而優先數變為最高而得到CPU運行。

『叄』 高響應比優先調度演算法的原理

高響應比優先調度演算法既考慮作業的執行時間也考慮作業的等待時間,綜合了先來先服務和最短作業優先兩種演算法的特點。
該演算法中的響應比是指作業等待時間與運行比值,響應比公式定義如下:
響應比 =(等待時間+要求服務時間)/ 要求服務時間,即RR=(w+s)/s=1+w/s,因此響應比一定是大於1的。
如實例:
某系統有3個作業,系統確定它們在全部到達後,再開始採用響應比高者優先的調度演算法,則它們的調度順序是什麼?各自的周轉時間是什麼?
作業號 提交時間 運行時間
1 8.8 1.5
2 9.0 0.4
3 9.5 1.0
(1)如果都到達再算的話,等待時間=最後一個的提交時間-該作業到達的時刻
1: 9.5-8.8=0.7
2: 9.5-9=0.5
3: 0
所以響應比為(等待時間+要求服務時間)要求服務時間=等待時間/要求服務時間+1
1: 0.7/1.5+1=1.47
2: 0.5/0.4+1=2.25
3: 1
所以2先運行,2從9.5開始運行到9.9結束;
再以9.9時刻算響應比:
1: (9.9-8.8)/1.5+1=1.73
3: (9.9-9.5)/1+1=1.4
所以2執行完後1開始執行,從9.9執行到11.4結束
最後一個是3:從11.4開始執行到12.4結束
(2)如果不是都到達後才運行,那麼在8.8時只有作業1到達,所以先運行作業1
8.8+1.5(運行時間)=10.3
到10.3的時候作業1完成,此時作業2和3都已到達所以計算其響應比
(等待時間+要求服務時間)要求服務時間=等待時間/要求服務時間+1
作業2:(10.3-9.0)/0.4+1=4.325
作業3:(10.3-9.5)/1.0+1=1.8
所以先運行作業2
10.3+0.4=10.7
到10.7運行作業3
10.7+1.0=11.7
到11.7結束

『肆』 高響應比優先調度演算法

- - 你是西農的吧

『伍』 動態高優先權優先調度演算法

動態高優先權優先調度演算法:

動態優先權是指,在創建進程時所賦予的優先權,是可以隨進程的推進或隨其等待時間的增加而改變的,以便獲得更好的調度性能。例如,我們可以規定,在就緒隊列中的進程,隨其等待時間的增長,其優先權以速率a提高。若所有的進程都具有相同的優先權初值,則顯然是最先進入就緒隊列的進程,將因其動態優先權變得最高而優先獲得處理機,此即FCFS演算法。若所有的就緒進程具有各不相同的優先權初值,那麼,對於優先權初值低的進程,在等待了足夠的時間後,其優先權便可能升為最高,從而可以獲得處理機。當採用搶占式優先權調度演算法時,如果再規定當前進程的優先權以速率b下降,則可防止一個長作業長期地壟斷處理機。

演算法代碼模擬實現:

#include<stdio.h>
#include<stdlib.h>
#defineN6

//待插入就緒隊列的進程數據
intid[N]={0,1,
2,3,4,
5};
intpriority[N]={9,38,17,
2,7,18};
intcpuTime[N]={0,
0,0,0,
0,0};
intallTime[N]={3,
2,3,6,
1,3};

//********************************
//
//模擬進程/PCB數據結構
//
//********************************

//
枚舉進程的狀態:就緒、執行、阻塞、完成
enumSTATE{Ready,Run,Block,Finish
};

//建立PCB結構體
structPCB{
intid;//標志數
intpriority;//優先數
intcpuTime;//
已佔CPU時間
intallTime;//
還需佔CPU時間
intblockTime;//已被阻塞的時間
STATEstate;//
進程狀態
PCB*pre;//
PCB的前指針
PCB*nxt;//
PCB的後指針
};

//********************************
//
//模擬進程隊列
//
//********************************

//進程入列
voidqueQush(PCB*process,PCB
*queHead)
{
process->pre=NULL;
process->nxt=
queHead->nxt;
if(queHead->nxt!=NULL){
//非第一個入列
queHead->nxt->pre=
process;
}
queHead->nxt=process;
}

//進程出列
voidquePop(PCB*process,PCB
*queHead)
{
if(process->pre!=NULL){
//不是頭節點
process->pre->nxt=
process->nxt;
}
else{
queHead->nxt=
process->nxt;
}
if(process->nxt!=NULL){
//不是尾節點
process->nxt->pre=
process->pre;
}
//
清空進程指針
process->pre=process->nxt=
NULL;
}

//查看隊列里進程的信息
voidqueWalk(PCB*queHead)
{
PCB*pro=queHead->nxt;
if(pro==NULL){
printf("(無進程) ");
return;
}
while(pro!=NULL)
{
printf("id:%d,
pri:%d,alltime:%d ",
pro->id,
pro->priority,
pro->allTime);
pro=
pro->nxt;
}
}

//********************************
//
//模擬就緒隊列
//
//********************************

intreadyQueNum;//就緒隊列的進程數量
PCBreadyQueHead;//
就緒隊列的頭部
PCB*readyMaxProcess;//就緒隊列中優先順序最高的進程

//進程插入到就緒隊列
voidreadyQueQush(PCB
*process)
{
readyQueNum++;
process->state=Ready;
queQush(process,&readyQueHead);
}

//優先順序最高的進程出列
PCB*readyQuePop()
{
readyQueNum--;
quePop(readyMaxProcess,
&readyQueHead);
returnreadyMaxProcess;
}

//每個時間片,更新就緒隊列里進程的信息
voidreadyQueUpdate()
{
intmaxPriority=-1;
PCB*pro=readyQueHead.nxt;
if(pro==NULL){
//就緒隊列沒有進程
readyMaxProcess=
NULL;
return;
}
while(pro!=NULL)
{
pro->priority
++;
if(pro->priority>maxPriority)
{
maxPriority=
pro->priority;
readyMaxProcess=pro;
}
pro=
pro->nxt;
}
}

//返回就緒隊列最高優先順序的值
intreadyMaxPriority()
{
returnreadyMaxProcess->priority;
}

//查看就緒隊列里進程的信息
voidreadyQueWalk()
{
printf("就緒隊列里的進程信息為: ");
queWalk(&readyQueHead);
}

//********************************
//
//模擬阻塞隊列
//
//********************************

#defineEndBlockTime3
//進程最長被阻塞時間

intblockQueNum;//阻塞隊列的進程數量
PCBblockQueHead;//
阻塞隊列的頭部
PCB*blockMaxProcess;//阻塞隊列中優先順序最高的進程

//進程插入到阻塞隊列
voidblockQueQush(PCB
*process)
{
blockQueNum++;
process->blockTime=0;
process->state=Block;
queQush(process,&blockQueHead);
}

//優先順序最高的進程出列
PCB*blockQuePop()
{
blockQueNum--;
quePop(blockMaxProcess,
&blockQueHead);
returnblockMaxProcess;
}

//每個時間片,更新阻塞隊列里進程的信息
voidblockQueUpdate()
{
intmaxPriority=-1;
PCB*pro=blockQueHead.nxt;
while(pro!=NULL)
{
pro->blockTime
++;
if(pro->blockTime>=EndBlockTime)
{
PCB*process=pro;
pro=pro->nxt;
//阻塞時間到,調入就緒隊列
blockQueNum--;
quePop(process,
&blockQueHead);
readyQueQush(process);
}else
if(pro->priority>maxPriority)
{
//更新阻塞隊列里優先順序最高的進程指針
maxPriority=
pro->priority;
blockMaxProcess=pro;
pro=pro->nxt;
}
}
}

//查看阻塞隊列里進程的信息
voidblockQueWalk()
{
printf("阻塞隊列里的進程信息為: ");
queWalk(&blockQueHead);
}

//********************************
//
//模擬動態優先權的進程調度
//
//********************************

//初始化數據
voidinitData()
{
//
初始化就緒隊列和阻塞隊列
readyQueNum=blockQueNum=0;
readyMaxProcess=blockMaxProcess=NULL;
readyQueHead.pre=readyQueHead.nxt=NULL;
blockQueHead.pre=blockQueHead.nxt=NULL;

//
初始化進程進入就緒隊列
inti,maxPriority=-1;
for(i=0;i<N;i
++)
{
//分配一個PCB的內存空間
PCB*pro=(PCB
*)malloc(sizeof(PCB));
//給當前的PCB賦值
pro->id
=id[i];
pro->priority
=priority[i];
pro->cpuTime
=cpuTime[i];
pro->allTime
=allTime[i];
pro->blockTime
=0;
if(pro->allTime>0){
//插入到就緒隊列中
readyQueQush(pro);
//更新就緒隊列優先順序最高的進程指針
if(pro->priority>
maxPriority){
maxPriority=pro->priority;
readyMaxProcess=pro;
}
}
}
}

//模擬cpu執行1個時間片的操作
voidcpuWord(PCB
*cpuProcess)
{
cpuProcess->priority-=3;
if(cpuProcess->priority<0)
{
cpuProcess->priority=0;
}
cpuProcess->cpuTime++;
cpuProcess->allTime--;
//
顯示正執行進程的信息:
printf("CPU正執行的進程信息為: ");
printf("id:M,pri:M,
alltime:M ",
cpuProcess->id,
cpuProcess->priority,
cpuProcess->allTime);
}

intmain()
{
inttimeSlice=0;//
模擬時間片
intcpuBusy=0;
//模擬cpu狀態
PCB*cpuProcess=NULL;//當前在cpu執行的進程
//
初始化數據
initData();
//
模擬進程調度
while(1)
{
if(readyQueNum==0
&&blockQueNum==0
&&cpuBusy==0){
//就緒隊列、阻塞隊列和cpu無進程,退出
break;
}
//printf(" %d%d",
readyQueNum,blockQueNum);
if(cpuBusy==0)
{
//cpu空閑,選擇一個進程進入cpu
if(readyQueNum>0)
{
//
選擇緒隊列優先順序最高的進程
cpuProcess
=readyQuePop();
}else{
//
就緒隊列沒有進程,改為選擇阻塞隊列優先順序最高的進程
cpuProcess
=blockQuePop();
}
cpuProcess->cpuTime=
0;
cpuProcess->state=
Run;
cpuBusy=1;
}
timeSlice++;
printf(" 第%d個時間片後: ",
timeSlice);
//
模擬cpu執行1個時間片的操作
cpuWord(cpuProcess);
if(cpuProcess->allTime==0){
cpuProcess->state=
Finish;
//釋放已完成進程的PCB
free(cpuProcess);
cpuBusy=0;
}
//
更新就緒隊列和阻塞隊列里的進程信息
blockQueUpdate();
readyQueUpdate();
//
查看就緒隊列和阻塞隊列的進程信息
readyQueWalk();
blockQueWalk();
if(cpuBusy==1
&&readyQueNum>0
&&
cpuProcess->priority
<readyMaxPriority()){
//需搶佔cpu,當前執行的進程調入阻塞隊列
blockQueQush(cpuProcess);
cpuProcess=readyQuePop();
}
}
printf(" 模擬進程調度演算法結束 ");
return0;
}

『陸』 什麼是高響應比優先調度演算法,它採用何種調度方式

高響應比優先調度演算法就是把CPU分配給就緒隊列中響應比最高的進程。響應比=等待時間/要求服務時間+1。採用非搶占式調度方式。030

『柒』 為什麼說響應比高者優先調度演算法是對先來先服務以及短作業優先這兩種調度演算法的折中

先來先服務的作業調度演算法,重點考慮的是作業在後備作業隊列里的等待時間,因此對短作業不利;短作業優先的作業調度演算法,重點考慮的是這樣所需要的CPU時間(當然,這個時間是用戶自己估計的),因此對長作業不利;「響應比高者優先」的作業調度演算法,試圖綜合這兩方面的因素,以便能更好的滿足各種用戶的需要。
所謂一個作業的響應比,是指該作業已經等待的時間與所需運行時間的比,即:
響應比=(已等待時間)/(所需CPU時間)
該比值的分母是不變的,但是隨著時間的推移,一個作業的「已等待時間」會不斷地發生變化。顯然,短作業比較容易獲得較高的響應比,這是因為它們的分母比較小,只需稍加等待,整個比值就會上升。另一方面,長作業的分母雖然很大,但隨著等待時間的增加,比值也會逐漸上升,從而獲得較高的響應比。根據這種分析,「可見響應比高者優先」的作業調度演算法,既照顧到了短作業的利益,也照顧到長作業的利益,是一種折中的作業調度演算法。

『捌』 設計一個按優先數調度演算法實現處理器調度的程序。 高手幫忙!!

#include <stdio.h>
#include <stdlib.h> //提供atoi()函數
#include <conio.c> //提供clrscr()函數
#define M 10 //字元串大小常量
#define N 3 //進程數常量
#define SLOT 2
typedef struct node{
char name[M];
int prio; //優先順序
int round; //時間片長度
int cputime; //已經使用的cpu時間
int needtime;//需要多少cpu時間
int count; //計數器
char state; //進程的當前狀態
struct node *next; //指向下一個進程
}PCB;

PCB *finish,*ready,*tail,*run;

void ShowHead(char *s1,char *s2);
int Menu_Select();
void Creat(int select);
void InsertPriority(PCB *q);
void InsertSlot(PCB *q);
void PrintOneProcess(int select,PCB *q);
void PrintAllProcess(int select);
void FirstIn();
void FIFODo(int select);
void PriorityDo(int select);
void SlotDo(int select);
void Quit();
main()
{
while(1)
{
clrscr();
switch(Menu_Select())
{
case 1:
Creat(1);
FIFODo(1);
printf("\n\n\t\t按回車鍵返回菜單...");
getchar();
getchar();
break;
case 2:
Creat(2);
PriorityDo(2);
printf("\n\n\t\t按回車鍵返回菜單...");
getchar();
getchar();
break;
case 3:
Creat(3);
SlotDo(3);
printf("\n\n\t\t按回車鍵返回菜單...");
getchar();
getchar();
break;
case 0:
Quit();
break;
}
}
}

/*列印每個界面的開頭顯示*/
void ShowHead(char *s1,char *s2)
{
printf("\t ==================%s========================\n\n",s1);
printf("\t\t\t\t%s\n\n",s2);
printf("\t ========================================================\n\n");
}

/*主菜單*/
int Menu_Select()
{
int choose;
char s[2];
clrscr();
printf("====================進程調度演算法模擬程序=====================\n\n");
printf("\t\t 1.先進先出調度策略\n");
printf("\t\t 2.優先數調度策略\n");
printf("\t\t 3.時間片輪轉調度策略\n");
printf("\t\t 0.退出系統\n\n");
printf("=============================================================\n\n");
do
{
printf("\n請輸入你的選擇(0-3):");
scanf("%s",s);
choose=atoi(s);
}while(choose<0 || choose>3);
return choose;
}

/*創建調度演算法的鏈表*/
void Creat(int select)
{
PCB *p,*q; //q為就緒隊列的最後一個結點
int i,time,rounds;
char na[M];
ready=NULL;
finish=NULL;
run=NULL;
if(select==1) //先進先出調度創建
{
printf("\nEnter name and time of process:\n");
for(i=1;i<=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='W';
p->next=NULL;
if(ready!=NULL) //就緒隊列不為空
{
q->next=p;
q=p;
}
else //就緒隊列為空
{
p->next=ready;
ready=p;
q=ready;
}
}
clrscr();
ShowHead("先進先出調度策略","FIFO dispatching algorithm ");
printf("\t\t name cputime needtime state\n");
PrintAllProcess(select); //列印出初始狀態的信息
}
else if(select==2) //優先數調度創建
{
printf("\nEnter name and time of process:\n");
for(i=1;i<=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='W';
p->prio=50-time;
if(ready!=NULL) //就緒隊列不為空的時候
InsertPriority(p);
else //就緒隊列為空
{
p->next=ready;
ready=p;
}
}//end of for()
clrscr();
ShowHead("優先順序調度策略","Priority dispatching algorithm ");
printf("\t\t name cputime needtime prio state\n");
PrintAllProcess(select); //列印出初始狀態的信息
}//end of else if()
else if(select==3) //時間片輪轉調度創建
{
printf("\nEnter name and the time of process:\n");
for(i=1;i<=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0; //計數器
p->round=SLOT;
p->state='W';
if(ready!=NULL)
InsertSlot(p);
else
{
p->next=ready;
ready=p;
}
}
clrscr();
ShowHead("時間片輪轉調度策略","Time slot dispatching algorithm ");
printf("\n\t\t name cputime needtime count slot state\n");
PrintAllProcess(select); //列印出初始狀態的信息
}
run=ready; //從就緒隊列取一個進程,使其運行,同時就緒隊列的頭指針後移
run->state='R';
ready=ready->next;
}

/*優先調度:把進程插入到合適的位置,就緒隊列按優先順序由高到低的順序排列*/
void InsertPriority(PCB *q)
{
PCB *pre,*p;
int flag=1;
pre=NULL;
p=ready;
while(p!=NULL && flag)
{
if(p->prio>q->prio)
{
pre=p;
p=p->next;
}
else
flag=0;
}
if(pre==NULL)
{
q->next=ready;
ready=q;
}
else
{
pre->next=q;
q->next=p;
}
}

/*時間片輪轉:把結點插入到就緒隊列的末尾*/
void InsertSlot(PCB *q)
{
PCB *pre,*p;
pre=NULL;
p=ready;
while(p!=NULL)
{
pre=p;
p=p->next;
}
pre->next=q;
q->next=NULL; /*由於插入到隊列的末尾,所以必須要使其的下一個結點為空值*/
}

/*列印一個信息*/
void PrintOneProcess(int select,PCB *q)
{
if(select==1)
printf("\t\t %-10s%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->state);
else if(select==2)
printf("\t\t %-10s%-10d%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->prio,q->state);
else if(select==3)
printf("\t\t %-10s%-10d%-10d%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->count,q->round,q->state);
}

/*將所有的進程列印出來,按運行,就緒,完成順序列印*/
void PrintAllProcess(int select)
{
PCB *p;
if(run!=NULL)
PrintOneProcess(select,run);
p=ready;
while(p!=NULL)
{
PrintOneProcess(select,p);
p=p->next;
}
p=finish;
while(p!=NULL)
{
PrintOneProcess(select,p);
p=p->next;
}
}

/*把就緒隊列的第一個進程調入運行*/
void FirstIn()
{
run=ready;
ready=ready->next;
run->state='R';
}

/*先進先出調度策略*/
void FIFODo(int select)
{
while(run!=NULL)
{

run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
if(run->needtime==0) //進程執行結束
{
printf("\n\t\t name cputime needtime state\n");
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
FirstIn();
PrintAllProcess(1);
}
}
}

/*優先順序演算法*/
void PriorityDo(int select)
{
while(run!=NULL)
{
printf("\n\t\t name cputime needtime prio state\n");
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-3;
if(run->needtime==0)
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
FirstIn();
}
else if((ready!=NULL) && (run->prio < ready->prio))
{
run->state='W';
InsertPriority(run);
FirstIn();
}
PrintAllProcess(select);
}
}

/*時間片輪轉演算法*/
void SlotDo(int select)
{
while(run!=NULL)
{
printf("\n\t\t name cputime needtime count slot state\n");
run->count=run->count+1;
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
if(run->needtime==0) /*運行完成時*/
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
FirstIn(); //就緒隊列不為空,將就緒隊列的第一個進程投入運行
}
else if(run->count==run->round) /*時間片已經到了,還未運行完成*/
{
run->count=0; //時間片
if(ready!=NULL)
{
run->state='W';
InsertSlot(run);
FirstIn();
}
}
PrintAllProcess(select); //列印本次的所有記錄
}
}

void Quit()
{
char ch;
clrscr();
gotoxy(10,5);
printf("==========================================================\n\n");
printf(" Thank you for you using\n\n");
gotoxy(10,9);
printf("==========================================================\n\n");
gotoxy(13,15);

getchar();
printf("\n\t\tDo you really want to quit(y/Y or n/N):");
scanf("%c",&ch);
if(ch=='y' || ch=='Y')
{
printf("\n\t\t按任意鍵退出系統...");
getchar();
exit(0);
}

}

『玖』 先來先服務調度演算法。 優先順序調度演算法。 短作業優先調度演算法 輪轉調度演算法 響應比高優先調度演算法

你試一下

#include<stdio.h>
//using namespace std;
#define MAX 10
struct task_struct
{
char name[10]; /*進程名稱*/
int number; /*進程編號*/
float come_time; /*到達時間*/
float run_begin_time; /*開始運行時間*/
float run_time; /*運行時間*/
float run_end_time; /*運行結束時間*/
int priority; /*優先順序*/
int order; /*運行次序*/
int run_flag; /*調度標志*/
}tasks[MAX];
int counter; /*實際進程個數*/
int fcfs(); /*先來先服務*/
int ps(); /*優先順序調度*/
int sjf(); /*短作業優先*/
int hrrn(); /*響應比高優先*/
int pinput(); /*進程參數輸入*/
int poutput(); /*調度結果輸出*/

void main()
{ int option;
pinput();
printf("請選擇調度演算法(0~4):\n");
printf("1.先來先服務\n");
printf("2.優先順序調度\n");
printf(" 3.短作業優先\n");
printf(" 4.響應比高優先\n");
printf(" 0.退出\n");
scanf("%d",&option);
switch (option)
{case 0:
printf("運行結束。\n");
break;
case 1:
printf("對進程按先來先服務調度。\n\n");
fcfs();
poutput();
break;
case 2:
printf("對進程按優先順序調度。\n\n");
ps();
poutput();
break;
case 3:
printf("對進程按短作業優先調度。\n\n");
sjf();
poutput();
break;
case 4:
printf("對進程按響應比高優先調度。\n\n");
hrrn();
poutput();
break;
}
}
int fcfs() /*先來先服務*/
{
float time_temp=0;
inti;
intnumber_schel;
time_temp=tasks[0].come_time;
for(i=0;i<counter;i++)
{
tasks[i].run_begin_time=time_temp;
tasks[i].run_end_time=tasks[i].run_begin_time+tasks[i].run_time;
tasks[i].run_flag=1;
time_temp=tasks[i].run_end_time;
number_schel=i;
tasks[number_schel].order=i+1;
}
return 0;
}

int ps() /*優先順序調度*/
{
float temp_time=0;
inti=0,j;
intnumber_schel,temp_counter;
intmax_priority;
max_priority=tasks[i].priority;
j=1;
while((j<counter)&&(tasks[i].come_time==tasks[j].come_time))
{
if (tasks[j].priority>tasks[i].priority)
{
max_priority=tasks[j].priority;
i=j;
}
j++;
} /*查找第一個被調度的進程*/
/*對第一個被調度的進程求相應的參數*/
number_schel=i;
tasks[number_schel].run_begin_time=tasks[number_schel].come_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
tasks[number_schel].order=1;
temp_counter=1;
while (temp_counter<counter)
{
max_priority=0;
for(j=0;j<counter;j++)
{if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
if (tasks[j].priority>max_priority)
{
max_priority=tasks[j].priority;
number_schel=j;
}
} /*查找下一個被調度的進程*/
/*對找到的下一個被調度的進程求相應的參數*/
tasks[number_schel].run_begin_time=temp_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
temp_counter++;
tasks[number_schel].order=temp_counter;

}return 0;
}

int sjf() /*短作業優先*/
{
float temp_time=0;
inti=0,j;
intnumber_schel,temp_counter;
float run_time;
run_time=tasks[i].run_time;
j=1;
while((j<counter)&&(tasks[i].come_time==tasks[j].come_time))
{
if (tasks[j].run_time<tasks[i].run_time)
{
run_time=tasks[j].run_time;
i=j;
}
j++;
} /*查找第一個被調度的進程*/
/*對第一個被調度的進程求相應的參數*/
number_schel=i;
tasks[number_schel].run_begin_time=tasks[number_schel].come_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
tasks[number_schel].order=1;
temp_counter=1;
while (temp_counter<counter)
{
for(j=0;j<counter;j++)
{
if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
{run_time=tasks[j].run_time;number_schel=j;break;}
}

for(j=0;j<counter;j++)
{if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
if(tasks[j].run_time<run_time)
{run_time=tasks[j].run_time;
number_schel=j;
}
}
/*查找下一個被調度的進程*/
/*對找到的下一個被調度的進程求相應的參數*/
tasks[number_schel].run_begin_time=temp_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
temp_counter++;
tasks[number_schel].order=temp_counter;
}return 0;
}

int hrrn() /*響應比高優先*/
{ int j,number_schel,temp_counter;
float temp_time,respond_rate,max_respond_rate;
/*第一個進程被調度*/
tasks[0].run_begin_time=tasks[0].come_time;
tasks[0].run_end_time=tasks[0].run_begin_time+tasks[0].run_time;
temp_time=tasks[0].run_end_time;
tasks[0].run_flag=1;
tasks[0].order=1;
temp_counter=1;
/*調度其他進程*/
while(temp_counter<counter)
{
max_respond_rate=0;
for(j=1;j<counter;j++)
{
if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
{respond_rate=(temp_time-tasks[j].come_time)/tasks[j].run_time;
if (respond_rate>max_respond_rate)
{
max_respond_rate=respond_rate;
number_schel=j;
}
}
} /*找響應比高的進程*/
tasks[number_schel].run_begin_time=temp_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
temp_time=tasks[number_schel].run_end_time;
tasks[number_schel].run_flag=1;
temp_counter+=1;
tasks[number_schel].order=temp_counter;
}
return 0;
}
int pinput() /*進程參數輸入*/
{ int i;
printf("please input the processcounter:\n");
scanf("%d",&counter);

for(i=0;i<counter;i++)
{printf("******************************************\n");
printf("please input the process of %d th :\n",i+1);
printf("please input the name:\n");
scanf("%s",tasks[i].name);
printf("please input the number:\n");
scanf("%d",&tasks[i].number);
printf("please input the come_time:\n");
scanf("%f",&tasks[i].come_time);
printf("please input the run_time:\n");
scanf("%f",&tasks[i].run_time);
printf("please input the priority:\n");
scanf("%d",&tasks[i].priority);
tasks[i].run_begin_time=0;
tasks[i].run_end_time=0;
tasks[i].order=0;
tasks[i].run_flag=0;
}
return 0;
}
int poutput() /*調度結果輸出*/
{
int i;
float turn_round_time=0,f1,w=0;
printf("name number come_time run_timerun_begin_time run_end_time priority order turn_round_time\n");
for(i=0;i<counter;i++)
{
f1=tasks[i].run_end_time-tasks[i].come_time;
turn_round_time+=f1;
w+=(f1/tasks[i].run_time);
printf(" %s, %d, %5.3f, %5.3f, %5.3f, %5.3f, %d, %d,%5.3f\n",tasks[i].name,tasks[i].number,tasks[i].come_time,tasks[i].run_time,tasks[i].run_begin_time,tasks[i].run_end_time,tasks[i].priority,tasks[i].order,f1);
}
printf("average_turn_round_timer=%5.2f\n",turn_round_time/counter);
printf("weight_average_turn_round_timer=%5.2f\n",w/counter);
return 0;
}

『拾』 什麼是最早截止時間優先調度演算法

最早截止時間優先(Earliest Dealine First, EDF)是實時系統中常用的一種調度演算法,系統中有多個任務時,由調度演算法決定哪個任務當前佔用處理器,那麼EDF演算法就是按照任務的截止時間(deadline)來確定任務的執行順序,最早截止的任務先執行。
設現在所有的進程都是就緒狀態,調度器會計算EDF,按進程的完成時間排序,也就是執行時間短的排在前面,調度器會按EDF的排序依次執行; 當有新的進程時,調度器會重新計算EDF,按進程的完成時間重新排序,如果新的進程排序比當前執行的進程排序高,那麼會先執行新的進程

閱讀全文

與優先調度演算法相關的資料

熱點內容
伺服器機櫃叫什麼名字 瀏覽:343
vue3源碼作者 瀏覽:826
app右下角標怎麼關 瀏覽:827
怎麼對一個盤加密碼 瀏覽:668
練習sql用什麼樣的伺服器 瀏覽:972
asp與php對比 瀏覽:641
php模除 瀏覽:250
android上傳圖片方式 瀏覽:882
華為視頻會議伺服器如何連接 瀏覽:220
phpfpm並發數 瀏覽:923
雲伺服器哪裡看 瀏覽:243
戴爾emc伺服器led怎麼拔出 瀏覽:792
程序員霸王 瀏覽:370
文件夾重合如何分開 瀏覽:162
mdk3命令 瀏覽:502
我的世界伺服器雲地址是什麼 瀏覽:755
往復壓縮機氣缸 瀏覽:248
骰子掛雲伺服器 瀏覽:945
彈性基礎鋼筋加密區 瀏覽:747
html中插入php代碼 瀏覽:277