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

處理器優先數調度演算法

發布時間:2022-06-18 15:01:07

⑴ 作業調度演算法的優先順序法

優先順序演算法(Priority Scheling)是多級隊列演算法的改進,平衡各進程對響應時間的要求。適用於作業調度和進程調度,可分成搶先式和非搶先式。 作業調度中的靜態優先順序大多按以下原則確定:
由用戶自己根據作業的緊急程度輸入一個適當的優先順序。
由系統或操作員根據作業類型指定優先順序。
系統根據作業要求資源情況確定優先順序。
進程的靜態優先順序的確定原則:
按進程的類型給予不同的優先順序。
將作業的情態優先順序作為它所屬進程的優先順序。 進程的動態優先順序一般根據以下原則確定:
根據進程佔用有CPU時間的長短來決定。
根據就緒進程等待CPU的時間長短來決定。

⑵ 基於優先數的時間片輪轉調度演算法調度處理器

沒有完全符合的,但是差不多的,你自己改改吧!
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
typedef struct PCBA
{
char name[30];
int round;
int prio;
int cputime;
int needtime;
char state;
struct PCBA *next;
} PCB,*CurrentRunPCB,*LinkQueue;
CurrentRunPCB SelMosHeiPrio(LinkQueue *Q);//從Q鏈表中選擇一個優先順序最大的進程
void InitQueue(LinkQueue *Q);//初始化鏈表Q,如果鏈表沒有頭指針,可以省略;
void InsertQueue(LinkQueue * Q,PCB* run);
{
}//將Run結點插入鏈表Q中,表示就緒隊列里多了一個進程
void Priosch(LinkQueue Wait_Queue,LinkQueue Finish_Queue)//優先法運行,從就緒隊列Wait_Queue中選擇進程執行,(選擇隊列中優先順序最高的進程),執行結束的進程放入完成隊列中;

{ while (WaitQueue!=NULL)
{
CurrentRunPCB run=SelMosHeiPrio(&WaitQueue); //run為就緒隊列中優先順序最高的進程

run->cputime+=2;//時間片為2
run->needtime-=2;
run->prio=50-run->needtime;//動態優先順序,但是優先順序隨著運行時間的增加,優先順序增加,所以誰優先順序高會一直佔用CPU
run->state='r';
if(run->needtime>0)
{ run->state='w';
InsertQueue(&WaitQueue,run) ;
}

else
{cout<<"當前運行進稱為:"<<run->name<<"運行結束後就緒隊列情況:"<<'\n';
run->state='F';
run->needtime=0;
InsertQueue(&FinishQueue,run) ;
Print(&WaitQueue);
}
//當然可以採用不看進程運行過程,直接將優先順序高的運行完,插入完成隊列
/*
CurrentRunPCB run=SelMosHeiPrio(&WaitQueue);
cout<<"當前運行進稱為:"<<run->name<<"運行結束後就緒隊列情況:"<<'\n';
Print(&WaitQueue);
run->cputime=run->needtime;
run->needtime=0;
run->prio=50;//動態優先順序,但是優先順序隨著運行時間的增加,優先順序增加,所以誰優先順序高會一直佔用CPU
run->state='F';
InsertQueue(&FinishQueue,run) ;*/
}
void Print(LinkQueue*Q)//將隊列的元素顯示輸出,輸出包含進程名,進程CPUTIME,NEEDTIME,狀態,優先順序

{LinkQueue p=*Q;
cout<<"name cputime needtime state prio'"<<'\n';
while (p!=NULL)
{ cout<<p->name<<'\t'<<p->cputime<<'\t'<<p->needtime<<'\t'<<p->state<<'\t'<<p->prio<<'\n';
p=p->next;
}
}
CurrentRunPCB DeQueue(LinkQueue*Q)//從就緒隊列中取出一個進程

{LinkQueue p=*Q;

*Q=(*Q)->next;
p->next=NULL;
return p;
}
void Roundsch(LinkQueue WaitQueue,LinkQueue FinishQueue)//輪轉法運行,從就緒隊列Wait_Queue中選擇進程執行一個時間片,執行結束的進程放入完成隊列中;若一個時間片未能執行完的進程再插入到就緒隊列
{ while (WaitQueue!=NULL)
{
CurrentRunPCB run=DeQueue(&WaitQueue);
cout<<"當前運行進稱為:"<<run->name<<"運行結束後就緒隊列情況:"<<'\n';
run->cputime+=2;//時間片為2
run->needtime-=2;
run->prio=50- run->cputime;
run->state='r';
if(run->needtime>0)
{InsertQueue(&WaitQueue,run) ;
run->state='w';
}
else
{run->state='F';
run->needtime=0;
InsertQueue(&FinishQueue,run) ;
}

Print(&WaitQueue);
}
cout<<"完成隊列情況:"<<'\n';
Print(&FinishQueue);
}

void SetAllpro(LinkQueue*Q)//設置優先順序函數50-p->needtime
{int max=0;
LinkQueue p,t;
t=NULL;
p=*Q;
if (p!=NULL)
{
max=p->prio;
p=p->next;
while(p)
{
if (max<p->prio) max=p->prio;
p=p->next;
}
p=*Q;
t=*Q;
if (t==p&&max==t->prio)
{*Q=(*Q)->next;
t->next=NULL;
return t;}
else{
t=t->next;
while(t)
{
if (max==t->prio)
{
p->next=t->next;
t->next=NULL;
return t;

}
else{p=t;
t=t->next;
}
}
}
}
return t;
}
void main()
{
PCBA *pcb0,*pcb1,*pcb2,*pcb3,*pcb4; //five processes 五個進程
LinkQueue Wait_Queue,Finish_Queue; //兩個隊列 等待和完成
Wait_Queue=NULL; //給隊列賦初值,如果帶有頭指針的鏈表,可以用函數;
Finish_Queue=NULL;
//InitQueue(&Wait_Queue);
//InitQueue(&Finish_Queue);
char ch;
//給各個進程設置初值
pcb0= new PCBA();
pcb1= new PCBA();
pcb2= new PCBA();
pcb3= new PCBA();
pcb4= new PCBA();
//example
strcpy(pcb0->name,"process1");
pcb0->round=2;
pcb0->prio=0;
pcb0->cputime=0;
pcb0->needtime=5;
pcb0->state='W';
pcb0->next=NULL;
strcpy(pcb1->name,"process2");
pcb1->round=2;
pcb1->prio=0;
pcb1->cputime=0;
pcb1->needtime=7;
pcb1->state='W';
pcb1->next=NULL;
strcpy(pcb2->name,"process3");
pcb2->round=2;
pcb2->prio=0;
pcb2->cputime=0;
pcb2->needtime=3;
pcb2->state='W';
pcb2->next=NULL;
strcpy(pcb3->name,"process4");
pcb3->round=2;
pcb3->prio=0;
pcb3->cputime=0;
pcb3->needtime=11;
pcb3->state='W';
pcb3->next=NULL;
strcpy(pcb4->name,"process5");
pcb4->round=2;
pcb4->prio=0;
pcb4->cputime=0;
pcb4->needtime=8;
pcb4->state='W';
pcb4->next=NULL;

//將各個進程插入就緒隊列中
InsertQueue(&Wait_Queue,pcb0);
InsertQueue(&Wait_Queue,pcb1);
InsertQueue(&Wait_Queue,pcb2);
InsertQueue(&Wait_Queue,pcb3);
InsertQueue(&Wait_Queue,pcb4);
//利用此演算法實現Wait_Queue中prio=50-needtime;
SetAllpro(&Wait_Queue);

cout<<"請輸入選擇的調度演算法(1 or 2 or anykey exit!):"<<endl;
cin>>ch;
switch(ch)
{
case '1':
Print(&Wait_Queue);
Roundsch(Wait_Queue,Finish_Queue);
break;
case '2':
Print(&Wait_Queue);
Priosch(Wait_Queue,Finish_Queue);
break;
default :
cout<<"你選擇了退出!"<<endl;
system("pause");
return;
}

return;
}

⑶ 作業調度演算法的選擇原則有哪幾個

批處理作業的調度演算法主要有以下幾種:
①先來先服務演算法。原則上按照作業進入輸入井的次序調度,如果作業的資源得不到滿足,將會推遲調度,它的資源得到滿足的時候會優先被調度進來。
優點:具有一定的公平性。
缺點:系統的吞吐率低,平均周轉時間長,有大作業到來的時,許多小作業推遲調度。
②計算時間短的作業優先.優先調度計算時間短的作業進行調度,資源不滿足的情況下推遲調度。在這種調度演算法下,要求用戶要對作業的計算時間預先有一個估計,調度以此為依據。
優點:由於被選中的作業計算時間,所以不能盡快地完成並退出系統,降低了作業的平均等待時間,提高了系統的吞吐率。
缺點:大作業會不滿意,而且極限情況下使得某些大作業始終得不到調度。
③響應比高者優先演算法。該演算法考慮了計算時間等待時間,既考慮了計算時間短的作業優先,又考慮了大作業長期等待的問題。所謂響應比是按照以下公式來定義的:
響應比R=等待時間/計算時間
這里的計算時間是估計的作業計算時間,從公式看,計算時間越短,響應比越高;而另一方面,大作業等待時間越長,響應比也會越大。一個作業完成以後,需要重新計算一下在輸入井中的各個作業的響應比,最高的將優先調度。
④優先數調度演算法。為每一個作業指定一個優先數,優先數高的作業先被調度。對於優先數相等的作業採用先來先服務的策略。優先數的制定原則是:作業的緩急程序,估計的計算時間,作業的等待時間,資源申請情況等因素綜合考慮。
⑤均衡調度演算法。使用不同資源的進程同時執行,減少作業等待同類設備而耗費的時間,加快作業的執行。

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

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

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

⑸ 設計一個按優先數調度演算法實現處理器調度的程序。

#include "stdio.h"
#include "malloc.h"
#include "string.h"
typedef struct node{
int time;
int name;
char statement;
int num;
struct node *next;
}node,*L;
void createL(L &l,int n){
l=(L)malloc(sizeof(node));
if(!l)
printf("error!");
else
l->next=NULL;
L p,q;
q=l;
for(int i=0;i<n;i++){
p=(L)malloc(sizeof(node));
printf("請輸入進程的名字name:\n");
scanf("%d",&p->name);
getchar();
printf("請輸入該進程的運行時間time:\n");
scanf("%d",&p->time);
printf("請輸入其優先順序數num:\n");
scanf("%d",&p->num);
getchar();
printf("請輸入其狀態:\n");
p->statement=getchar();
p->next=q->next;
q->next=p;
q=p;
getchar();
}

}
void traL(L &l){
L p;
p=l->next;
printf("進程名\t運行時間\t優先數\t狀態\n");
while(p){
printf(" %d\t%5d\t%11d\t %c",p->name,p->time,p->num,p->statement);
printf("\n");
p=p->next;
}
}
void Sort(L &l)
{
L tail=NULL;

while(tail!= l->next)
{
L pre = l;
L cur = pre->next;
while(cur != tail && cur->next != tail)
{
if( cur->num < cur->next->num )
{
pre->next = cur->next;
cur->next = cur->next->next;
pre->next->next = cur;
}
pre = pre->next;
cur = pre->next;
}
tail = cur;
}
}
void run(L &l){
Sort(l);
L p,r,q;
q=l;
if(l->next!=NULL)
p=l->next;
int j=0;
printf("第k次運行\t進程名\t運行時間\t優先數\t狀態\n");
while(p!=NULL){
j++;
printf("%5d\t %d\t%5d\t%11d\t %c",j,p->name,p->time,p->num,p->statement);
printf("\n");
p->num--;
p->time--;
if(p->time==0){
p->statement='E';
r=p;
if(p->next!=NULL)
{
l->next=p->next;
free(r);
p=l->next;
}
else
break;
}
else{
Sort(l);
if(l->next!=NULL)
p=l->next;
else
break;
}
}
}

void main(){
L l;
int n;
printf("請輸入進程的數目n:\n");
scanf("%d",&n);
createL(l,n);
traL(l);
run(l);
}
//這是運行過的,應該符合你的要求!

⑹ 多核CPU調度有哪幾種演算法 比如單核的有優先順序、先來先服務。那多核的有哪幾種呢

一般多核任務調度演算法有全局隊列調度和局部隊列調度。前者是指操作系統維護一個全局的任務等待隊列,當系統中有一個CPU核心空閑時,操作系統就從全局任務等待隊列中選取就緒任務開始在此核心上執行。這種方法的優點是CPU核心利用率較高。後者是指操作系統為每個CPU內核維護一個局部的任務等待隊列,當系統中有一個CPU內核空閑時,便從該核心的任務等待隊列中選取恰當的任務執行,這種方法的優點是任務基本上無需在多個CPU核心間切換,有利於提高CPU核心局部Cache命中率。目前多數多核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);
}

}

⑻ 進程調度方案設計 實現一個基本動態優先順序的調度演算法

前兩天做操作系統作業的時候學習了一下幾種進程調度演算法,在思考和討論後,有了一些自己的想法,現在就寫出來,跟大家討論下。,或者說只有有限的CPU資源,當系統中有多個進程處於就緒狀態,要競爭CPU資源時,操作系統就要負責完成如何分配資源的任務。在操作系統中,由調度程序來完成這一選擇分配的工作,調度程序所使用的演算法即是調度演算法。調度演算法需要考慮的指標主要有盡量保證CPU資源分配的公平性;按照一定策略強制執行演算法調度;平衡整個計算機系統,盡量保持各個部分都處於忙碌狀態。而根據系統各自不同的特點和要求,調度演算法又有一些側重點和目標不同,因此,演算法按照系統差異主要分為三大類:批處理系統中的調度演算法,代表調度演算法有:先來先服務、最短作業優先、最短剩餘時間優先。互動式系統中的調度演算法,代表調度演算法有:輪轉調度、優先順序調度、多級隊列、最短進程優先、保證調度、彩票調度、公平分享調度。實時系統中的調度演算法,代表調度演算法有:速率單調調度、最早最終時限優先調度。下面就上述提到的調度演算法中挑出幾個進行重點分析:保證調度保證調度是指利用演算法向用戶做出明確的性能保證,然後盡力按照此保證實現CPU的資源分配。利用這種演算法,就是定一個進程佔用CPU的時間的標准,然後按照這個標准去比較實際佔用CPU的時間,調度進程每次使離此標准最遠的進程得到資源,不斷滿足離所保證的標准最遠的進程,從而平衡資源分配滿足這個標準的要求。保證調度演算法的優點是:能很好的保證進程公平的CPU份額,當系統的特點是:進程的優先順序沒有太大懸殊,所制定的保證標准差異不大,各個進程對CPU的要求較為接近時,比如說系統要求n個進程中的每個進程都只佔用1/n的CPU資源,利用保證調度可以很容易的實現穩定的CPU分配要求。但缺點是,這種情況太過理想,當系統的各個進程對CPU要求的緊急程度不同,所制定的保證較為復雜的時候,這個演算法實現起來比較困難。彩票調度彩票調度這種演算法的大意是指向進程提供各種系統資源如CPU資源的彩票,當系統需要做出調度決策時,隨機抽出一張彩票,由此彩票的擁有者獲得資源。在彩票調度系統中,如果有一個新的進程出現並得到一些彩票,那麼在下一次的抽獎中,該進程會有同它持有彩票數量成正比例的機會贏得獎勵。進程持有的彩票數量越多,則被抽中的可能性就越大。調度程序可以通過控制進程的彩票持有數量來進行調度。彩票調度有很多優點:首先,它很靈活,系統增加分給某個進程的彩票數量,就會大大增加它佔用資源的可能性,可以說,彩票調度的反應是迅速的,而快速響應需求正是互動式系統的一個重要要求。其次,彩票調度演算法中,進程可以交換彩票,這個特點可以更好的保證系統的平衡性,使其各個部分都盡可能的處於忙碌狀態。而且利用彩票調度還可以解決許多別的演算法很難解決的問題,例如可以根據特定的需要大致成比例的劃分CPU的使用。速率單調調度速率單調調度演算法是一種可適用於可搶占的周期性進程的經典靜態實時調度演算法。當實時系統中的進程滿足:每個周期性進程必須在其周期內完成,且進程之間沒有相互依賴的關系,每個進程在一次突發中需要相同的CPU時間量,非周期的進程都沒有最終時限四個條件時,並且為了建模方便,我們假設進程搶占即刻發生沒有系統開銷,可以考慮利用速率單調演算法。速率單調調度演算法是將進程的速率(按照進程周期所算出的每秒響應的次數)賦為優先順序,則保證了優先順序與進程速率成線性關系,這即是我們所說的速率單調。調度程序每次運行優先順序最高的,只要優先順序較高的程序需要運行,則立即搶占優先順序低的進程,而優先順序較低的進程必須等所有優先順序高於它的進程結束後才能運行。速率單調調度演算法可以保證系統中最關鍵的任務總是得到調度,但是缺點是其作為一種靜態演算法,靈活性不夠好,當進程數變多,系統調度變得復雜時,可能不能較好的保證進程在周期內運行。最早最終時限優先調度最早最終時限優先調度演算法是一個動態演算法,不要求進程是周期性的,只要一個進程需要CPU時間,它就宣布它的到來時間和最終時限。調度程序維持一個可運行的進程列表,按最終時限排序,每次調度一個最終時限最早的進程得到CPU 。當新進程就緒時,系統檢查其最終時限是否在當前運行的進程結束之前,如果是,則搶占當前進程。由於是動態演算法,最早最終優先調度的優點就是靈活,當進程數不超過負載時,資源分配更優,但也同樣由於它的動態屬性,進程的優先順序都是在不斷變化中的,所以也沒有哪個進程是一定可以保證滿足調度的,當進程數超過負載時,資源分配合理度會急速下降,所以不太穩定。

⑼ 用c語言實現先到先處理和最短路徑優先的cpu調度演算法

#include <stdio.h>
#define n 20
struct fcfs
{
int id;
int atime;
int runtime;
int ftime;
}f[n];
zcx(){
int xz;
int amount;
printf("**************分割線*********************\n");
printf("1.先來先服務\n");
printf("2.優先順序\n");
printf("請輸入你的選擇:");
scanf("%d",&xz);
printf("\n\n");
if(xz==1)
{
printf("你的選擇是先來先服務\n");
printf("請輸入進程數:");
scanf("%d",&amount);
yihao(amount);

}
else
{
printf("你的選擇是優先順序\n");
printf("請輸入進程數:");
scanf("%d",&amount);
erhao(amount);

}
}
yihao(int amount)
{
int i,j,l,k;
struct fcfs f[n];

printf("\n\n");
for(i=0;i<amount;i++)
{
printf("請輸入第 %d 個程序的信息\n",i+1);
printf("進程名 :");
scanf("%d",&f[i].id);
printf("到達時間:");
scanf("%d",&f[i].atime);
printf("運行時間:");
scanf("%d",&f[i].runtime);

}
for(i=0;i<amount;i++)
{
for(j=0;j<amount-i-1;j++)
{
if(f[j].atime>f[j+1].atime)
{
l=f[j].atime;
f[j].atime=f[j+1].atime;
f[j+1].atime=l;

k=f[j].id;
f[j].id=f[j+1].id;
f[j+1].id=k;
}
}
}

printf("進程名 開始時間 運行時間 結束時間 \n");
for(i=0;i<amount;i++)
{
f[i].ftime=f[i].atime+f[i].runtime;

printf("%d %d %d %d\n",f[i].id,f[i].atime,f[i].runtime,f[i].ftime);
f[i+1].atime=f[i].ftime;
}

zcx();

}
erhao(int amount)
{
int i,j,l,k;
struct fcfs f[n];

printf("\n\n");
for(i=0;i<amount;i++)
{
printf("請輸入第 %d 個程序的信息\n",i+1);
printf("進程名 :");
scanf("%d",&f[i].id);
printf("優先順序 :");
scanf("%d",&f[i].atime);
printf("運行時間:");
scanf("%d",&f[i].runtime);

}
for(i=0;i<amount;i++)
{
for(j=0;j<amount-i-1;j++)
{
if(f[j].atime>f[j+1].atime)
{
l=f[j].atime;
f[j].atime=f[j+1].atime;
f[j+1].atime=l;

k=f[j].id;
f[j].id=f[j+1].id;
f[j+1].id=k;
}
}
}
printf("進程名 優先順序 工作時間 \n");
for(i=0;i<amount;i++)
{
f[i].ftime=f[i].atime+f[i].runtime;

printf("%d %d %d \n",f[i].id,f[i].atime,f[i].runtime);
f[i+1].ftime=f[i].ftime+f[i+1].atime;
}

zcx();

}

void main()
{

zcx();

}

這是操作系統的作業吧

⑽ CPU的調度演算法:先來先服務、最短運行期、時間片輪轉、優先權設置分別是什麼意思

調度演算法說的是現在有若干個進程(每個進程擁有自己的屬性),演算法根據它們的屬性選擇哪一個進程去執行。

先來先服務:按照進程來的時間早晚屬性來判斷,先來的先執行
最短:按照進程運行需要的時間長短屬性來判斷,最短的先執行
時間片輪轉:和進程屬性無關,每個進程都分配相同的時間去運行,輪著來
優先權設置:根據進程的優先順序屬性判斷誰先執行,優先順序是用戶可以設定的
希望能夠幫到你

閱讀全文

與處理器優先數調度演算法相關的資料

熱點內容
中興gpon命令 瀏覽:881
python中取出字典key值 瀏覽:676
Linux目錄inode 瀏覽:142
手機上如何用文件夾發郵件 瀏覽:424
暢課app密碼忘了怎麼找回 瀏覽:75
怎麼編譯idea 瀏覽:229
如何查看伺服器是否做了熱備 瀏覽:999
硬碟同名文件夾病毒 瀏覽:727
百度雲不解壓下載 瀏覽:560
新冠疫情app怎麼用 瀏覽:971
拆二代程序員 瀏覽:398
河北壓縮空氣冷干機生產廠家 瀏覽:580
圖論與java 瀏覽:577
程序員寫代碼告白初音 瀏覽:740
sshpdf 瀏覽:539
windows調用linux 瀏覽:594
如何查找本地伺服器名稱 瀏覽:819
linux文件只讀屬性 瀏覽:586
VNAS技術加密 瀏覽:131
python編程電話費計算話費 瀏覽:463