導航:首頁 > 源碼編譯 > c語言優先順序調度演算法

c語言優先順序調度演算法

發布時間:2024-10-21 23:40:00

A. )用C語言(或其它語言,如Java)編程實現對N個進程採用某種進程調度演算法(如動態優先權調度

公眾:類PrivilegeProcess {
公共靜態無效的主要(字串[] args){

MyQueue的MyQueue的新MyQueue的();/ /聲明隊列

印刷電路板[PCB = {新的PCB(001 ,8,1),新的PCB(002,7,9),新的PCB(003,3,8),新的PCB(004,1,7),新的PCB(005,7,4)};
> PCB段=新的PCB();

(INT I = 0; <pcb.length; + +){/ /初始化先進行排序,選擇排序這里使用的是高優先順序的一線隊

(J =我; <pcb.length; J + +){

(PCB [I]。特權<PCB [J]。特權){

段= PCB [1];

PCB [I] = PCB [J];

PCB [J] =段;

}

}

}

體系。通過out.println(「入隊後第一時間的進程的順序:」);

(INT I = 0; <pcb.length; + +){

的System.out調用println(第一次入隊#程序名稱:「+ PCB [我]。名稱+ totaltime:」+ PCB [I]。totaltime +「的」特權「+ PCB [我]。特權); }

();

myqueue.start(PCB);

}

}

類MyQueue的{

INT指數= 0;

PCB [] PC =新的PCB [5];

PCB [] PC1 =新的PCB [4];

PCB溫度=新的PCB() BR />公共無效排隊(PCB工藝){/ /排隊演算法

(指數== 5){

(「出界!」);

返回

}

PC [索引] =進程;

指數+ +;

}

公共:PCB DEQUEUE(){/ /出隊演算法(索引== 0)

返回空;

(INT I = 0; <pc1.length; + +){

PC1 [I] = PC [ +1];

}

指數 -

溫度= PC [0];

(INT I = 0; <pc1.length; + +){ BR /> PC [I] = PC1 [I];

}

回報條件;

}

公共無效啟動(PCB [] PC){/ /進程表演算法

(PC [0]。isNotFinish ==真| | PC [1 isNotFinish ==真| | PC [2 isNotFinish ==真| | PC [3]。時isNotFinish ==真| | PC [4]。isNotFinish ==){

/ / *註:| |運算符都是假的,所有的表達式結果為假,否則真

(INT I = 0; <PC長度; + +){

PC [I]。運行(這一點); />} 的System.out.println();

(INT I = 0; <pc.length; + +){/ /處理每個運行一次運行的時間片的長度重新排序優先一旦

(J =我; <pc.length; J + +){

如果(PC [I]特權<PC [J]。特權){

溫度= PC [I];

PC [I] = PC [J];

PC [J] =溫度;

}

}

}

}

}

}

類PCB {/ /聲明過程級

和int名,totaltime ,運行時特權;

布爾isNotFinish的;

公眾PCB(){

}

公開PCB(名稱,詮釋totaltime特權){

this.name =的名稱;/ /進程名

this.totaltime = totaltime ;/ /

this.privilege =特權;/ /總時間優先 this.runtime = 2 ;/ /時間片值是2

this.isNotFinish =真;/ /是否執行完成

(「初始值:程序名稱:」+名+「totaltime:」+ totaltime +「特權」+特權);

System.out的。調用println();

}

MyQueue的MQ公共無效的run(){/ /處理的基礎上實施的時間片演算法

(totalTime> 1){ totaltime =運行;/ /總時間大於1,總時間=總時間 - 時間片

特權 -

(「程序名稱:」+姓名+「 remaintime:「+ +」特權「+特權); totaltime

的} else if(totaltime == 1){

totaltime - ;/ /總時間為1時,執行時間為1
>特權 -

(「程序名稱:」+姓名+「remaintime:」+ totaltime +「特權」+特權);

}其他{

isNotFinish =假;/ / 0,將isNotFinish標志設置為假

}

如果(isNotFinish ==真){br mq.deQueue();

mq.enQueue(本);

}

}
}

B. 用C語言編程,演算法是基於ISLIP的改進演算法PIRM演算法

首先 普里姆演算法 並不是一種交換機調度算粗芹法 是構建一個最小生成樹的演算法
其次 性能拿棗測試分析吞吐量岩敏畢和公平性 這是什麼東西也要說清楚一下

C. 時間片輪轉演算法和優先順序調度演算法 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();
}

D. 求進程調度先來先服務演算法,短進程優先演算法完整c語言代碼

/*(一)進程調度

進程調度演算法有FIFO,優先數調度演算法,時間片輪轉調度演算法,分級調度演算法,

輸入:進程流文件,其中存儲的是一系列要執行的進程,
每個作業包括三個數據項:
進程名 所需時間 優先數(0級最高)
輸出:
進程執行流 等待時間 平均等待時間

本程序包括:FIFO,優先數調度演算法,時間片輪轉調度演算法

進程流文件process_stream.txt
測試數據:
p0 16 2
p1 5 1
p2 4 3
p3 8 0
p4 9 4
p5 7 6

VC++調試通過
*/
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <stdlib.h>

const int Quatum=2;//定義時間片的長度為2秒
const int MAXPCB=100;//定義最大進程數

//定義進程結構體
typedef struct node
{
char name[20];//進程名
int time; //進程運行時間
int privilege;//進程優先順序(靜態)
int finished;//進程完成標志,0-未完成,1-已完成
int wait_time;//進程等待時間
}pcb;

pcb pcbs[MAXPCB];
int quantiry;//進程流文件中的進程總數

void initial()
{
int i;
for (i=0;i<MAXPCB;i++)
{
strcpy(pcbs[i].name,"");
pcbs[i].time=0;
pcbs[i].privilege=0;
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
quantiry=0;
}

int readData()
{
FILE *fp;
char fname[20];
int i;
cout<<"請輸入進程流文件名:"<<endl;
cin>>fname;
if ((fp=fopen(fname,"r"))==NULL)
{
cout<<"錯誤,文件打不開,請檢查文件名"<<endl;
}
else
{
while (!feof(fp))
{
fscanf(fp,"%s %d %d %d",pcbs[quantiry].name,
&pcbs[quantiry].time,&pcbs[quantiry].privilege);
quantiry++;
}
//輸出所讀入得數據
cout<<"輸出所讀入的數據"<<endl;
cout<<"進程流文件中的進程總數="<<quantiry<<endl;
cout<<"進程名 所需時間 優先數"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].time<<" "<<pcbs[i].privilege<<endl;
}

return 1;
}

return 0;
}

//重置數據,以供另一個演算法使用
void init()
{
int i;
for (i=0;i<MAXPCB;i++)
{
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
}

void FIFO()
{
int i,j;

int total;
//輸出FIFO演算法執行流
cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"FIFO演算法執行流:"<<endl;
cout<<"進程名 等待時間"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].wait_time<<endl;
for (j=i+1;j<quantiry;j++)
{
pcbs[j].wait_time+=pcbs[i].time;
}
}

total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}
cout<<"總等待時間:"<<total<<" "<<"平均等待時間:"<<total/quantiry<<endl;
}

//優先度調度演算法
void privilege()
{
int i,j,p;
int passed_time=0;
int total;

int queue[MAXPCB];
int current_privielege=1000;

for (i=0;i<quantiry;i++)
{
current_privielege=1000;
for (j=0;j<quantiry;j++)
{
if ((pcbs[j].finished==0)&&(pcbs[j].privilege<current_privielege))
{
p=j;
current_privielege=pcbs[j].privilege;
}
}
queue[i]=p;
pcbs[p].finished=1;
pcbs[p].wait_time+=passed_time;
passed_time+=pcbs[p].time;

}
//輸出優先數調度執行流
cout<<endl<<"-----------------------------------------"<<endl;
cout<<"優先數調度執行流:"<<endl;
cout<<"進程名 等待時間"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[queue[i]].name<<" "<<pcbs[queue[i]].wait_time<<"--"<<queue[i]<<endl;
}

total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}

cout<<"總等待時間:"<<total<<" 平均等待時間:"<<total/quantiry<<endl;
}

//時間片輪轉調度演算法
void timer()
{
int i,j,sum,flag=1;
int passed_time=0;
int max_time=0;
int round=0;

int queue[1000];
int total=0;

while(flag==1)
{
flag=0;
for (i=0;i<quantiry;i++)
{
if (pcbs[i].finished==0)
{
flag=1;
queue[total]=i;
total++;
if (pcbs[i].time<=Quatum*(round+1))
pcbs[i].finished=1;
}
}
round++;
}

cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"時間片輪轉調度執行流:";
for(i=0;i<total;i++)
{
cout<<pcbs[queue[i]].name<<" ";
}
cout<<endl;
cout<<"進程名 結束時間 運行時間 等待時間"<<endl;

sum=0;

for (i=0;i<quantiry;i++)
{
for(j=total-1;j>=0;j--)//從輪轉調度執行流序列由後往前比較,找到同名進程即可計算其完成時間
{
if (strcmp(pcbs[queue[j]].name,pcbs[i].name)==0)
{
cout<<" "<<pcbs[i].name<<" "<<(j+1)*Quatum<<" ";
cout<<pcbs[i].time<<" "<<(j+1)*Quatum-pcbs[i].time<<endl;
sum+=(j+1)*Quatum-pcbs[i].time;
break;
}
}
}

cout<<"總等待時間:"<<sum<<" "<<"平均等待時間:"<<sum/quantiry<<endl;
}

//顯示版權信息函數
void version()
{
cout<<endl<<endl;

cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 進程調度模擬系統 ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃ version 2011 ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
cout<<endl<<endl;
}
//主函數

int main()
{
int flag;
version();
initial();
flag=readData();
if(flag==1){
FIFO();
init();
privilege();
init();
timer();
}
cout<<endl;
system("pause");
return 0;
}

E. 權重輪詢調度演算法(Weighted Round-Robin Scheling) [C語言實現]

weight[i+1] = a % weight[i+1];
這句話導致後面的weight為0

閱讀全文

與c語言優先順序調度演算法相關的資料

熱點內容
程序員什麼樣的公司最好 瀏覽:65
製冷壓縮機與設備實訓 瀏覽:11
自己在家搭建伺服器有什麼用 瀏覽:706
mysql和php和ajax 瀏覽:375
四洲制的編譯者是 瀏覽:443
單片機小車調速程序 瀏覽:984
伺服器沒反應是怎麼回事啊 瀏覽:496
通達信嵌入Python編寫公式嗎 瀏覽:210
程序員視頻資源 瀏覽:200
惡意pdf 瀏覽:615
伺服器如何點亮硬碟 瀏覽:267
用於掃描的文件夾怎麼設置 瀏覽:897
貓團網app邀請人在哪裡填 瀏覽:691
平衡101規約源碼 瀏覽:531
類似淘寶的源碼 瀏覽:31
javaocx控制項 瀏覽:149
php實現文件夾壓縮 瀏覽:38
mysql表修復命令 瀏覽:705
安卓手機怎麼連接gt2 瀏覽:861
易企秀源碼免費下載 瀏覽:767