當前位置:首頁 » 操作系統 » 優先順序調度演算法

優先順序調度演算法

發布時間: 2022-01-09 06:23:50

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

#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);
}

}

2. 什麼是優先順序輪轉調度演算法支持處理機搶占嗎

我想估計跟多級反饋隊列演算法(Round Robin with Multiple Feedback)有點相似

多級反饋隊列演算法時間片輪轉演算法和優先順序演算法的綜合和發展。

優點:

² 為提高系統吞吐量和縮短平均周轉時間而照顧短進程。

² 為獲得較好的I/O設備利用率和縮短響應時間而照顧I/O型進程。

² 不必估計進程的執行時間,動態調節。

1. 多級反饋隊列演算法

² 設置多個就緒隊列,分別賦予不同的優先順序,如逐級降低,隊列1的優先順序最高。每個隊列執行時間片的長度也不同,規定優先順序越低則時間片越長,如逐級加倍。

² 新進程進入內存後,先投入隊列1的末尾,按FCFS演算法調度;若按隊列1一個時間片未能執行完,則降低投入到隊列2的末尾,同樣按FCFS演算法調度;如此下去,降低到最後的隊列,則按「時間片輪轉」演算法調度直到完成。

² 僅當較高優先順序的隊列為空,才調度較低優先順序的隊列中的進程執行。如果進程執行時有新進程進入較高優先順序的隊列,則搶先執行新進程,並把被搶先的進程投入原隊列的末尾。

3. 動態高優先權優先調度演算法

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

動態優先權是指,在創建進程時所賦予的優先權,是可以隨進程的推進或隨其等待時間的增加而改變的,以便獲得更好的調度性能。例如,我們可以規定,在就緒隊列中的進程,隨其等待時間的增長,其優先權以速率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;
}

4. 1.選用優先順序演算法和時間片輪轉演算法模擬實現進程調度演算法

我們考試上交的 能運行

#define MAX 100
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int b;//存放進程本次結束時的時間
void main()
{
int i,N,t,k;
int a[MAX];//存放進程的剩餘時間
int cnt[MAX];//存放進程調度次數
printf("請輸入進程數N:");
scanf("%d",&N);
printf("\n請輸入時間片t大小:");
scanf("%d",&t);
printf("\n請依次輸入各個進程的服務時間");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
cnt[i]=0;
}
printf("被調度進程\t進程調度次數 \t本次運行時間結果\t剩餘時間\n");
k=1;
while(k)
{
for(i=0;i<N;i++)
{
if(a[i]!=0)
if(a[i]>=t)
{
a[i]-=t;
b+=t;
cnt[i]=cnt[i]+1;
printf("\n\t%d\t\t%d\t\t%d\t\t%d",i+1,cnt[i],b,a[i]);
}
else
{
b=b+a[i];
cnt[i]=cnt[i]+1;
a[i]=0;
printf("\n\t%d\t\t%d\t\t%d\t\t%d",i+1,cnt[i],b,a[i]);
}
else continue;
}
for(i=0;i<N;i++)
if(a[i]!=0)
{ k=1;break;}
else continue;
if(i>=N)
k=0;
}
printf("\n");
printf("進程全部運行完成!");
printf("\n");

}

5. 靜態搶占式優先順序調度演算法是如何進行的

按照優先順序值的大小進行調度,選擇優先順序值大的作業優先調度。搶占式是指如果進入的作業的優先順序數大於當前正在執行的作業的優先順序數,就執行進入的作業,搶佔了當前正在執行的作業的資源。
按照到達時間將作業放入就緒隊列,當前作業執行過程中有作業進入,根據作業的優先順序值進行判斷,如果進入的作業的優先順序值小於或等於當前執行的作業的優先順序值,繼續執行當前作業;如果進入的作業的優先順序值大於當前執行的作業的優先順序值,將資源給進入的作業,當前的作業就放入就緒隊列隊尾,此時還需要的服務時間為原服務時間-進入的作業的到達時間。之後,每到達一個作業就與當前執行的作業進行優先順序值比較,優先順序值大的優先執行。當當前執行的作業執行結束後,比較就緒隊列中的作業的優先順序值,優先順序值大的優先執行。如此執行,直到就緒隊列為空,結束調度。

6. 基於優先順序的調度演算法有哪兩種

靜態優先順序和動態優先順序

7. 優先順序調度演算法如何用JAVA實現

在多線程時,可以手動去設置每個線程的優先順序setPriority(int newPriority)
更改線程的優先順序。

8. 作業調度演算法中HPF演算法的動態優先順序如何設定優先順序標准

方法如下:

php">#include"stdio.h"
#include<STDLIB.H>
#include<CONIO.H>
#definegetpch(type)(type*)malloc(sizeof(type))
#defineNULL0
structpcb{/*定義進程式控制制塊PCB*/
charname[10];
charstate;
intsuper;
intntime;
intrtime;
structpcb*link;
}*ready=NULL,*p;
typedefstructpcbPCB;
sort()/*建立對進程進行優先順序排列函數*/
{
PCB*first,*second;
intinsert=0;
if((ready==NULL)||((p->super)>(ready->super)))/*優先順序最大者,插入隊首*/
{
p->link=ready;
ready=p;
}
else/*進程比較優先順序,插入適當的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->super)>(second->super))/*若插入進程比當前進程優先數大,*/
{/*插入到當前進程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else/*插入進程優先數最低,則插入到隊尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0)first->link=p;
}
}

input()/*建立進程式控制制塊函數*/
{
inti,num;
//clrscr();/*清屏*/
printf(" 請輸入進程號?");
scanf("%d",&num);
for(i=0;i<NUM;I++)scanf(?%s?,p-輸入進程名:?);printf(? p="getpch(PCB);"進程號No.%d: ?,i);{>name);
printf(" 輸入進程優先數:");
scanf("%d",&p->super);
printf(" 輸入進程運行時間:");
scanf("%d",&p->ntime);
printf(" ");
p->rtime=0;p->state='w';
p->link=NULL;
sort();/*調用sort函數*/
}
}
intspace()
{
intl=0;PCB*pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}

disp(PCB*pr)/*建立進程顯示函數,用於顯示當前進程*/
{
printf(" qname state super ndtime runtime ");
printf("|%s ",pr->name);
printf("|%c ",pr->state);
printf("|%d ",pr->super);
printf("|%d ",pr->ntime);
printf("|%d ",pr->rtime);
printf(" ");
}

check()/*建立進程查看函數*/
{
PCB*pr;
printf(" ****當前正在運行的進程是:%s",p->name);/*顯示當前運行進程*/
disp(p);
pr=ready;
printf(" ****當前就緒隊列狀態為: ");/*顯示就緒隊列狀態*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}

destroy()/*建立進程撤消函數(進程運行結束,撤消進程)*/
{
printf(" 進程[%s]已完成. ",p->name);
free(p);
}
running()/*建立進程就緒函數(進程運行時間到,置就緒狀態*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();/*調用destroy函數*/
else
{
(p->super)--;
p->state='w';
sort();/*調用sort函數*/
}
}

main()/*主函數*/
{
intlen,h=0;
charch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf(" Theexecutenumber:%d ",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf(" 按任一鍵繼續......");
ch=getchar();
}
printf(" 進程已經完成. ");
ch=getchar();
}

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

你試一下

#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;
}

10. 時間片輪轉演算法和優先順序調度演算法 C語言模擬實現

一、目的和要求
進程調度是處理機管理的核心內容。本實驗要求用高級語言編寫模擬進程調度程序,以便加深理解有關進程式控制制快、進程隊列等概念,並體會和了解優先數演算法和時間片輪轉演算法的具體實施辦法。
二、實驗內容
1.設計進程式控制制塊PCB的結構,通常應包括如下信息:
進程名、進程優先數(或輪轉時間片數)、進程已佔用的CPU時間、進程到完成還需要的時間、進程的狀態、當前隊列指針等。
2.編寫兩種調度演算法程序:
優先數調度演算法程序
循環輪轉調度演算法程序
3.按要求輸出結果。
三、提示和說明
分別用兩種調度演算法對伍個進程進行調度。每個進程可有三種狀態;執行狀態(RUN)、就緒狀態(READY,包括等待狀態)和完成狀態(FINISH),並假定初始狀態為就緒狀態。
(一)進程式控制制塊結構如下:
NAME——進程標示符
PRIO/ROUND——進程優先數/進程每次輪轉的時間片數(設為常數2)
CPUTIME——進程累計佔用CPU的時間片數
NEEDTIME——進程到完成還需要的時間片數
STATE——進程狀態
NEXT——鏈指針
註:
1.為了便於處理,程序中進程的的運行時間以時間片為單位進行計算;
2.各進程的優先數或輪轉時間片數,以及進程運行時間片數的初值,均由用戶在程序運行時給定。
(二)進程的就緒態和等待態均為鏈表結構,共有四個指針如下:
RUN——當前運行進程指針
READY——就需隊列頭指針
TAIL——就需隊列尾指針
FINISH——完成隊列頭指針
(三)程序說明
1. 在優先數演算法中,進程優先數的初值設為:
50-NEEDTIME
每執行一次,優先數減1,CPU時間片數加1,進程還需要的時間片數減1。
在輪轉法中,採用固定時間片單位(兩個時間片為一個單位),進程每輪轉一次,CPU時間片數加2,進程還需要的時間片數減2,並退出CPU,排到就緒隊列尾,等待下一次調度。
2. 程序的模塊結構提示如下:
整個程序可由主程序和如下7個過程組成:
(1)INSERT1——在優先數演算法中,將尚未完成的PCB按優先數順序插入到就緒隊列中;
(2)INSERT2——在輪轉法中,將執行了一個時間片單位(為2),但尚未完成的進程的PCB,插到就緒隊列的隊尾;
(3)FIRSTIN——調度就緒隊列的第一個進程投入運行;
(4)PRINT——顯示每執行一次後所有進程的狀態及有關信息。
(5)CREATE——創建新進程,並將它的PCB插入就緒隊列;
(6)PRISCH——按優先數演算法調度進程;
(7)ROUNDSCH——按時間片輪轉法調度進程。
主程序定義PCB結構和其他有關變數。
(四)運行和顯示
程序開始運行後,首先提示:請用戶選擇演算法,輸入進程名和相應的NEEDTIME值。
每次顯示結果均為如下5個欄位:
name cputime needtime priority state
註:
1.在state欄位中,"R"代表執行態,"W"代表就緒(等待)態,"F"代表完成態。
2.應先顯示"R"態的,再顯示"W"態的,再顯示"F"態的。
3.在"W"態中,以優先數高低或輪轉順序排隊;在"F"態中,以完成先後順序排隊。

view plain to clipboardprint?
/*
操作系統實驗之時間片輪轉演算法和優先順序調度演算法
By Visual C++ 6.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char name[20]; /*進程的名字*/
int prio; /*進程的優先順序*/
int round; /*分配CPU的時間片*/
int cputime; /*CPU執行時間*/
int needtime; /*進程執行所需要的時間*/
char state; /*進程的狀態,W——就緒態,R——執行態,F——完成態*/
int count; /*記錄執行的次數*/
struct node *next; /*鏈表指針*/
}PCB;
PCB *ready=NULL,*run=NULL,*finish=NULL; /*定義三個隊列,就緒隊列,執行隊列和完成隊列*/
int num;
void GetFirst(); /*從就緒隊列取得第一個節點*/
void Output(); /*輸出隊列信息*/
void InsertPrio(PCB *in); /*創建優先順序隊列,規定優先數越小,優先順序越高*/
void InsertTime(PCB *in); /*時間片隊列*/
void InsertFinish(PCB *in); /*時間片隊列*/
void PrioCreate(); /*優先順序輸入函數*/
void TimeCreate(); /*時間片輸入函數*/
void Priority(); /*按照優先順序調度*/
void RoundRun(); /*時間片輪轉調度*/
int main(void)
{
char chose;
printf("請輸入要創建的進程數目:\n");
scanf("%d",&num);
getchar();
printf("輸入進程的調度方法:(P/R)\n");
scanf("%c",&chose);
switch(chose)
{
case 'P':
case 'p':
PrioCreate();
Priority();
break;
case 'R':
case 'r':
TimeCreate();
RoundRun();
break;
default:break;
}
Output();
return 0;
}
void GetFirst() /*取得第一個就緒隊列節點*/
{
run = ready;

if(ready!=NULL)
{
run ->state = 'R';
ready = ready ->next;
run ->next = NULL;
}
}
void Output() /*輸出隊列信息*/
{
PCB *p;
p = ready;
printf("進程名\t優先順序\t輪數\tcpu時間\t需要時間\t進程狀態\t計數器\n");
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
p = finish;
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
p = run;
while(p!=NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);
p = p->next;
}
}
void InsertPrio(PCB *in) /*創建優先順序隊列,規定優先數越小,優先順序越低*/
{
PCB *fst,*nxt;
fst = nxt = ready;

if(ready == NULL) /*如果隊列為空,則為第一個元素*/
{
in->next = ready;
ready = in;
}
else /*查到合適的位置進行插入*/
{
if(in ->prio >= fst ->prio) /*比第一個還要大,則插入到隊頭*/
{
in->next = ready;
ready = in;
}
else
{
while(fst->next != NULL) /*移動指針查找第一個別它小的元素的位置進行插入*/
{
nxt = fst;
fst = fst->next;
}

if(fst ->next == NULL) /*已經搜索到隊尾,則其優先順序數最小,將其插入到隊尾即可*/
{
in ->next = fst ->next;
fst ->next = in;
}
else /*插入到隊列中*/
{
nxt = in;
in ->next = fst;
}
}
}
}
void InsertTime(PCB *in) /*將進程插入到就緒隊列尾部*/
{
PCB *fst;
fst = ready;

if(ready == NULL)
{
in->next = ready;
ready = in;
}
else
{
while(fst->next != NULL)
{
fst = fst->next;
}
in ->next = fst ->next;
fst ->next = in;
}
}
void InsertFinish(PCB *in) /*將進程插入到完成隊列尾部*/
{
PCB *fst;
fst = finish;

if(finish == NULL)
{
in->next = finish;
finish = in;
}
else
{
while(fst->next != NULL)
{
fst = fst->next;
}
in ->next = fst ->next;
fst ->next = in;
}
}
void PrioCreate() /*優先順序調度輸入函數*/
{
PCB *tmp;
int i;

printf("輸入進程名字和進程所需時間:\n");
for(i = 0;i < num; i++)
{
if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)
{
perror("malloc");
exit(1);
}
scanf("%s",tmp->name);
getchar(); /*吸收回車符號*/
scanf("%d",&(tmp->needtime));
tmp ->cputime = 0;
tmp ->state ='W';
tmp ->prio = 50 - tmp->needtime; /*設置其優先順序,需要的時間越多,優先順序越低*/
tmp ->round = 0;
tmp ->count = 0;
InsertPrio(tmp); /*按照優先順序從高到低,插入到就緒隊列*/
}
}
void TimeCreate() /*時間片輸入函數*/
{
PCB *tmp;
int i;

printf("輸入進程名字和進程時間片所需時間:\n");
for(i = 0;i < num; i++)
{
if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)
{
perror("malloc");
exit(1);
}
scanf("%s",tmp->name);
getchar();
scanf("%d",&(tmp->needtime));
tmp ->cputime = 0;
tmp ->state ='W';
tmp ->prio = 0;
tmp ->round = 2; /*假設每個進程所分配的時間片是2*/
tmp ->count = 0;
InsertTime(tmp);
}
}
void Priority() /*按照優先順序調度,每次執行一個時間片*/
{
int flag = 1;

GetFirst();
while(run != NULL) /*當就緒隊列不為空時,則調度進程如執行隊列執行*/
{
Output(); /*輸出每次調度過程中各個節點的狀態*/
while(flag)
{
run->prio -= 3; /*優先順序減去三*/
run->cputime++; /*CPU時間片加一*/
run->needtime--;/*進程執行完成的剩餘時間減一*/
if(run->needtime == 0)/*如果進程執行完畢,將進程狀態置為F,將其插入到完成隊列*/
{
run ->state = 'F';
run->count++; /*進程執行的次數加一*/
InsertFinish(run);
flag = 0;
}
else /*將進程狀態置為W,入就緒隊列*/
{
run->state = 'W';
run->count++; /*進程執行的次數加一*/
InsertTime(run);
flag = 0;
}
}
flag = 1;
GetFirst(); /*繼續取就緒隊列隊頭進程進入執行隊列*/
}
}
void RoundRun() /*時間片輪轉調度演算法*/
{

int flag = 1;

GetFirst();
while(run != NULL)
{
Output();
while(flag)
{
run->count++;
run->cputime++;
run->needtime--;
if(run->needtime == 0) /*進程執行完畢*/
{
run ->state = 'F';
InsertFinish(run);
flag = 0;
}
else if(run->count == run->round)/*時間片用完*/
{
run->state = 'W';
run->count = 0; /*計數器清零,為下次做准備*/
InsertTime(run);
flag = 0;
}
}
flag = 1;
GetFirst();
}

熱點內容
玩qq三國要什麼樣的電腦配置 發布:2024-09-20 00:50:57 瀏覽:445
樹莓派zero編譯驅動 發布:2024-09-20 00:50:56 瀏覽:483
上傳文件文件夾找不到 發布:2024-09-20 00:26:32 瀏覽:915
承台箍筋加密區 發布:2024-09-20 00:26:31 瀏覽:228
筆記本什麼配置能流暢運行cf 發布:2024-09-20 00:14:19 瀏覽:952
實測華為編譯器 發布:2024-09-19 23:50:52 瀏覽:822
linux匯總 發布:2024-09-19 23:46:39 瀏覽:453
阿里雲伺服器環境搭建教程 發布:2024-09-19 23:21:58 瀏覽:837
黃色文件夾圖標 發布:2024-09-19 23:19:22 瀏覽:684
mysql資料庫導出導入 發布:2024-09-19 23:00:47 瀏覽:183