編程實現(xiàn)進程調(diào)度的算法,更好地掌握操作系統(tǒng)的原理及實現(xiàn)方法,從而有利于把握進程調(diào)度細(xì)節(jié)。
創(chuàng)新互聯(lián)建站是一家專業(yè)提供連山企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計制作、成都網(wǎng)站設(shè)計、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為連山眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。二、設(shè)計要求(1)要求實現(xiàn)先來先服務(wù),短作業(yè)優(yōu)先,時間片輪轉(zhuǎn),高優(yōu)先權(quán)調(diào)度算法四種算法并進行對比分析.
(2)要求界面簡單,易懂,關(guān)鍵代碼部分要注釋.
(3)編程語言可以采用自己任意精通的語言
三、設(shè)計思想說明先來先服務(wù):程序的執(zhí)行調(diào)度順序按先進入隊列的先獲得執(zhí)行,并且其他進程都不能中斷正在執(zhí)行的進程,要等進程完成后才能,讓出CPU給其他進程。執(zhí)行的時候可以隨時在隊列中插入進程。
短作業(yè)優(yōu)先:進程的調(diào)度順序按程序的服務(wù)時間來決定,進程的執(zhí)行順序。服務(wù)時間短的先被調(diào)用。調(diào)度時先從隊列中選取服務(wù)時間最短的進程來執(zhí)行。進程中途不能中斷,即使此時隊列中存在服務(wù)時間比其更短的進程,仍需要等待該進程執(zhí)行完后才能被執(zhí)行。
高優(yōu)先權(quán)調(diào)度:選取進程中優(yōu)先級最高的一個,以優(yōu)先級的值大,優(yōu)先級就大。調(diào)度時總是選取隊列中進程優(yōu)先級最高的來執(zhí)行,不管是否有某個進程在執(zhí)行,只要存在比正在執(zhí)行進程優(yōu)先級高的進程,則就會立刻中斷正在執(zhí)行的進程,讓給跟高優(yōu)先級的進程。
時間片輪轉(zhuǎn):本課程設(shè)計采用多級反饋隊列調(diào)度算法,設(shè)立4個進程隊列,分給隊列1的時間片為3秒,隊列2的時間片為6秒,隊列3的時間片為12秒,隊列4的時間片為24秒。隊列1的優(yōu)先級最高,隊列4的優(yōu)先級最低。高優(yōu)先級的隊列沒執(zhí)行完,即不為空,就永遠(yuǎn)不執(zhí)行其下面的低優(yōu)先級的隊列里面的進程。當(dāng)執(zhí)行低優(yōu)先級隊列里面的進程時,突然間高優(yōu)先級的隊列插入了進程就立刻跳到高優(yōu)先級的隊列執(zhí)行其里面的進程。每個隊列的進程都是按先來先執(zhí)行的順序執(zhí)行。進程初次執(zhí)行肯定要進入隊列1。如何從頭到尾執(zhí)行一遍隊列1中的進程是,存在某些進程在隊列1的時間片內(nèi)還沒執(zhí)行完,就把進程移交到下一個隊列中。每個隊列都如此類推。直到最后一個隊列4,如果在隊列4還有進程在本時間片內(nèi)還沒沒執(zhí)行完,就把該程序放到隊尾,從新等待時間片執(zhí)行。
基本概念程序:程序是指靜態(tài)的指令集合,它不占用系統(tǒng)的運行資源,可以長久地保存在磁盤中。
進程:進程是指進程實體(由程序、數(shù)據(jù)和進程控制塊構(gòu)成)的運行過程,是系統(tǒng)進行資源分配和調(diào)度的一個獨立單位。進程執(zhí)行程序,但進程與程序之間不是一一對應(yīng)的。通過多次運行,一個程序可以包含多個進程;通過調(diào)用關(guān)系,同一進程可以被多個程序包含(如一個DLL文件可以被多個程序運用)。
作業(yè):作業(yè)由一組統(tǒng)一管理和操作的進程集合構(gòu)成,是用戶要求計算機系統(tǒng)完成的一項相對獨立的工作。作業(yè)可以是完成了編譯、鏈接之后的一個用戶程序,也可以是各種命令構(gòu)成的一個腳本。
作業(yè)調(diào)度:作業(yè)調(diào)度是在資源滿足的條件下,將處于后備狀態(tài)的作業(yè)調(diào)入內(nèi)存,同時生成與作業(yè)相對應(yīng)的進程,并為這些進程提供所需要的資源。作業(yè)調(diào)度適用于多道批處理系統(tǒng)中的批處理作業(yè)。根據(jù)作業(yè)控制塊中的信息,檢查系統(tǒng)是否滿足作業(yè)的資源要求,只有在滿足作業(yè)調(diào)度的資源需求的情況下,系統(tǒng)才能進行作業(yè)調(diào)度。
基本調(diào)度算法1)先來先服務(wù)(First-Come First-Served,F(xiàn)CFS)調(diào)度算法
先來先服務(wù)調(diào)度算法遵循按照進入后備隊列的順序進行調(diào)度的原則。該算法是一種非搶占式的算法,是到目前為止最簡單的調(diào)度算法,其編碼實現(xiàn)非常容易。該算法僅考慮了作業(yè)到達的先后順序,而沒有考慮作業(yè)的執(zhí)行時間長短、作業(yè)的運行特性和作業(yè)對資源的要求。
2)短作業(yè)優(yōu)先(Shortest-Job-First,SJF)調(diào)度算法
短作業(yè)優(yōu)先調(diào)度算法根據(jù)作業(yè)控制塊中指出的執(zhí)行時間,選取執(zhí)行時間最短的作業(yè)優(yōu)先調(diào)度。本實驗中規(guī)定,該算法是非搶占式的,即不允許立即搶占正在執(zhí)行中的長進程,而是等當(dāng)前作業(yè)執(zhí)行完畢再進行調(diào)度。
3)響應(yīng)比高者優(yōu)先(HRRF)調(diào)度算法
FCFS調(diào)度算法只片面地考慮了作業(yè)的進入時間,短作業(yè)優(yōu)先調(diào)度算法考慮了作業(yè)的運行時間而忽略了作業(yè)的等待時間。響應(yīng)比高者優(yōu)先調(diào)度算法為這兩種算法的折中。響應(yīng)比為作業(yè)的響應(yīng)時間與作業(yè)需要執(zhí)行的時間之比。作業(yè)的響應(yīng)時間為作業(yè)進入系統(tǒng)后的等待時間與作業(yè)要求處理器處理的時間之和。
4)優(yōu)先權(quán)高者優(yōu)先(Highest-Priority-First,HPF)調(diào)度算法
優(yōu)先權(quán)高者優(yōu)先調(diào)度算法與響應(yīng)比高者優(yōu)先調(diào)度算法十分相似,根據(jù)作業(yè)的優(yōu)先權(quán)進行作業(yè)調(diào)度,每次總是選取優(yōu)先權(quán)高的作業(yè)優(yōu)先調(diào)度。作業(yè)的優(yōu)先權(quán)通常用一個整數(shù)表示,也叫優(yōu)先數(shù)。優(yōu)先數(shù)的大小與優(yōu)先權(quán)的關(guān)系由系統(tǒng)或者用戶規(guī)定。優(yōu)先權(quán)高者優(yōu)先調(diào)度算法綜合考慮了作業(yè)執(zhí)行時間和等待時間的長短、作業(yè)的緩急度,作業(yè)對外部設(shè)備的使用情況等因素,根據(jù)系統(tǒng)設(shè)計目標(biāo)和運行環(huán)境而給定各個作業(yè)的優(yōu)先權(quán),決定作業(yè)調(diào)度的先后順序。
實驗環(huán)境與測試數(shù)據(jù)
實驗環(huán)境
硬件環(huán)境:計算機一臺;
軟件環(huán)境:Linux操作系統(tǒng),C語言編程環(huán)境。
本實驗所選用的調(diào)度算法均默認(rèn)為非搶占式調(diào)度。
測試數(shù)據(jù)如下表所示。
作業(yè)Id | 到達時間 | 執(zhí)行時間 | 優(yōu)先權(quán) |
1 | 800 | 50 | 0 |
2 | 815 | 30 | 1 |
3 | 830 | 25 | 2 |
4 | 835 | 20 | 2 |
5 | 845 | 15 | 2 |
6 | 700 | 10 | 1 |
void initial_jobs()
初始化所有作業(yè)信息
void reset_jinfo()
重置所有作業(yè)信息
int findminjob(job jobs[],int count)
找到執(zhí)行時間最短的作業(yè)。輸入?yún)?shù):所有的作業(yè)信息及待查找的作業(yè)總數(shù),輸出為執(zhí)行時間最短的作業(yè)id
int findrearlyjob(job jobs[],int count)
找到達到最早的作業(yè) 輸入?yún)?shù):所有的作業(yè)信息及待查找的作業(yè)總數(shù),輸出參數(shù)為最早達到的作業(yè)id
void readJobdata()
//讀取作業(yè)的基本信息
void FCFS()
//先來先服務(wù)算法
void SFJschdulejob(job jobs[],int count)
//短作業(yè)優(yōu)先算法 輸入?yún)?shù):所有的作業(yè)信息及待查找的作業(yè)總數(shù)
實驗內(nèi)容運行程序代碼
#include#include#include#include//大作業(yè)數(shù)量
#define MAXJOB 50
//作業(yè)的數(shù)據(jù)結(jié)構(gòu)
typedef struct node
{
int number;//作業(yè)號
int reach_time;//作業(yè)抵達時間
int need_time;//作業(yè)的執(zhí)行時間
int privilege;//作業(yè)優(yōu)先權(quán)
float excellent; //響應(yīng)比
int start_time;//作業(yè)開始時間
int wait_time;//等待時間
int visited;//作業(yè)是否被訪問過
bool isreached;//作業(yè)是否抵達
}job;
job jobs[MAXJOB];//作業(yè)序列
int quantity;//作業(yè)數(shù)量
//初始化作業(yè)序列
void initial_jobs()
{
int i;
for(i=0;ijobs[i].reach_time&&jobs[i].visited==0)
{
rearlyjob=jobs[i].reach_time;
rearlyloc=i;
}
}
return rearlyloc;
}
//讀取作業(yè)數(shù)據(jù)
void readJobdata()
{
FILE *fp;
char fname[20];
int i;
//輸入測試文件文件名
printf("please input job data file name\n");
scanf("%s",fname);
if((fp=fopen(fname,"r"))==NULL)
{//讀取文件失敗
printf("error, open file failed, please check filename:\n");
}
else
{
//依次讀取作業(yè)信息
while(!feof(fp))
{
if(fscanf(fp,"%d %d %d %d",&jobs[quantity].number,&jobs[quantity].reach_time,&jobs[quantity].need_time,&jobs[quantity].privilege)==4)
quantity++;
}
//打印作業(yè)信息
printf("output the origin job data\n");
printf("---------------------------------------------------------------------\n");
printf("\tjobID\treachtime\tneedtime\tprivilege\n");
for(i=0;icurrent_time)
{
jobs[loc].start_time=jobs[loc].reach_time;
current_time=jobs[loc].reach_time;
}
else
{
jobs[loc].start_time=current_time;
}
jobs[loc].wait_time=current_time-jobs[loc].reach_time;
printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n",loc+1,jobs[loc].reach_time,jobs[loc].start_time,jobs[loc].wait_time,
jobs[loc].wait_time+jobs[loc].need_time);
jobs[loc].visited=1;
current_time+=jobs[loc].need_time;
total_waitime+=jobs[loc].wait_time;
total_roundtime=total_roundtime+jobs[loc].wait_time+jobs[loc].need_time;
//獲取剩余作業(yè)中最近到達作業(yè)
loc=findrearlyjob(jobs,quantity);
}
printf("總等待時間:%-8d 總周轉(zhuǎn)時間:%-8d\n",total_waitime,total_roundtime);
printf("平均等待時間: %4.2f 平均周轉(zhuǎn)時間: %4.2f\n",(float)total_waitime/(quantity),(float)total_roundtime/(quantity));
}
//短作業(yè)優(yōu)先作業(yè)調(diào)度
//在current_time之前找最短,都在current_time之后找最近
void SFJschdulejob()
{
int loc=0;
int current_time=0;
int total_waitime=0;
int total_roundtime=0;
int rearlyloc = findrearlyjob(jobs, quantity);//最早到達的作業(yè)序號
loc = findminjob(jobs, jobs[rearlyloc].reach_time);
//獲取執(zhí)行時間最短的作業(yè)
//輸出作業(yè)流
printf("\n\nSJF算法作業(yè)流\n");
printf("------------------------------------------------------------------------\n");
printf("\tjobID\treachtime\tstarttime\twaittime\troundtime\n");
current_time=jobs[loc].reach_time;
//每次循環(huán)找出最先到達的作業(yè)并打印相關(guān)信息
for(int i=0;icurrent_time)
{
jobs[loc].start_time=jobs[loc].reach_time;
current_time=jobs[loc].reach_time;
}
else
{
jobs[loc].start_time=current_time;
}
jobs[loc].wait_time=current_time-jobs[loc].reach_time;
printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n",loc+1,jobs[loc].reach_time,jobs[loc].start_time,jobs[loc].wait_time,
jobs[loc].wait_time+jobs[loc].need_time);
jobs[loc].visited=1;
current_time+=jobs[loc].need_time;
total_waitime+=jobs[loc].wait_time;
total_roundtime=total_roundtime+jobs[loc].wait_time+jobs[loc].need_time;
//獲取剩余作業(yè)中最短的作業(yè)
loc=findminjob(jobs,current_time);
}
printf("總等待時間:%-8d 總周轉(zhuǎn)時間:%-8d\n",total_waitime,total_roundtime);
printf("平均等待時間: %4.2f 平均周轉(zhuǎn)時間: %4.2f\n",(float)total_waitime/(quantity),(float)total_roundtime/(quantity));
}
//高響應(yīng)比調(diào)度算法
int max_excellent(job jobs[], int current_time)
{
int maxjob= -1;
int maxloc= -1;
for (int i = 0; i< quantity; i++)
{
//current_time之前有最高響應(yīng)比的作業(yè),取
if(jobs[i].reach_time<= current_time && jobs[i].visited==0)
{
if(jobs[i].excellent >maxjob)
{
maxjob=jobs[i].excellent;
maxloc=i;
}
}
}
int min_reachtime = 1000000;
//若current_time之前沒有作業(yè),
//取current_time之后最先開始的(如果多個項目同時最先開始,取權(quán)值最高的)
if (maxloc == -1)
{
for(int i=0;imaxjob)
{
maxjob=jobs[i].excellent;
maxloc=i;
}
}
}
}
}
return maxloc;
}
void HRRFschdulejob()
{
int i;
int current_time=0;
int loc;
int total_waitime=0;
int total_roundtime=0;
float new_excellent=0;
//獲取最高響應(yīng)比的作業(yè)
int rearlyloc = findrearlyjob(jobs,quantity);
loc = max_excellent(jobs, jobs[rearlyloc].reach_time);
//輸出作業(yè)流
printf("\n\nHRRF算法作業(yè)流\n");
printf("------------------------------------------------------------------------\n");
printf("\tjobID\treachtime\tstarttime\twaittime\troundtime\n");
current_time=jobs[loc].reach_time;
//每次循環(huán)找出最先到達的作業(yè)并打印相關(guān)信息
for(i=0;icurrent_time)
{
jobs[loc].start_time=jobs[loc].reach_time;
current_time=jobs[loc].reach_time;
}
else
{
jobs[loc].start_time=current_time;
}
jobs[loc].wait_time=current_time-jobs[loc].reach_time;
printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n",loc+1,jobs[loc].reach_time,jobs[loc].start_time,jobs[loc].wait_time,
jobs[loc].wait_time+jobs[loc].need_time);
jobs[loc].visited=1;
current_time+=jobs[loc].need_time;
total_waitime+=jobs[loc].wait_time;
total_roundtime=total_roundtime+jobs[loc].wait_time+jobs[loc].need_time;
//獲取剩余作業(yè)中最近到達作業(yè)
for(int j = 0; j< quantity; j++)
{
if(current_time >jobs[j].reach_time && jobs[j].visited == 0)
{
jobs[j].wait_time = current_time - jobs[j].reach_time;
new_excellent =( (float)jobs[j].wait_time + (float)jobs[j].need_time ) / (float)jobs[j].need_time ;
jobs[j].excellent = new_excellent;
}
}
loc = max_excellent(jobs, current_time);
}
printf("總等待時間:%-8d 總周轉(zhuǎn)時間:%-8d\n",total_waitime,total_roundtime);
printf("平均等待時間: %4.2f 平均周轉(zhuǎn)時間: %4.2f\n",(float)total_waitime/(quantity),(float)total_roundtime/(quantity));
}
int max_privilege(job jobs[], int current_time)
{
int maxjob= -1;
int minloc= -1;
for (int i = 0; i< quantity; i++)
{
//current_time之前有最高權(quán)值,取
if(jobs[i].reach_time<= current_time && jobs[i].visited==0)
{
if(jobs[i].privilege >maxjob)
{
maxjob=jobs[i].privilege;
minloc=i;
}
}
}
int min_reachtime = 100000;
//current_time之前沒有作業(yè),
//取current_time之后最先開始的(如果多個項目同時最先開始,取權(quán)值最高的)
if (minloc == -1)
{
for(int i=0;imaxjob)
{
maxjob=jobs[i].privilege;
minloc=i;
}
}
}
}
}
return minloc;
}
//優(yōu)先權(quán)高者優(yōu)先調(diào)度算法
void HPF()
{
int loc=0;
int current_time=0;
int total_waitime=0;
int total_roundtime=0;
int rearlyloc = findrearlyjob(jobs, quantity);
loc = max_privilege(jobs, jobs[rearlyloc].reach_time);
//獲取優(yōu)先權(quán)最高的作業(yè)
//輸出作業(yè)流
printf("\n\nHPF算法作業(yè)流\n");
printf("------------------------------------------------------------------------\n");
printf("\tjobID\treachtime\tstarttime\twaittime\troundtime\n");
current_time=jobs[loc].reach_time;
//每次循環(huán)找出最先到達的作業(yè)并打印相關(guān)信息
for(int i=0;icurrent_time)
{
jobs[loc].start_time=jobs[loc].reach_time;
current_time=jobs[loc].reach_time;
}
else
{
jobs[loc].start_time=current_time;
}
jobs[loc].wait_time=current_time-jobs[loc].reach_time;
printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n",loc+1,jobs[loc].reach_time,jobs[loc].start_time,jobs[loc].wait_time,
jobs[loc].wait_time+jobs[loc].need_time);
jobs[loc].visited=1;
current_time+=jobs[loc].need_time;
total_waitime+=jobs[loc].wait_time;
total_roundtime=total_roundtime+jobs[loc].wait_time+jobs[loc].need_time;
//獲取剩余作業(yè)中最短的作業(yè)
loc=max_privilege(jobs,current_time);
}
printf("總等待時間:%-8d 總周轉(zhuǎn)時間:%-8d\n",total_waitime,total_roundtime);
printf("平均等待時間: %4.2f 平均周轉(zhuǎn)時間: %4.2f\n",(float)total_waitime/(quantity),(float)total_roundtime/(quantity));
}
int main()
{
initial_jobs();
readJobdata();
HRRFschdulejob();
reset_jinfo();
HPF();
reset_jinfo();
FCFS();
reset_jinfo();
SFJschdulejob();
system("pause");
return 0;
}
時間片輪轉(zhuǎn)調(diào)度算法模擬C語言#include#include#define MAX 10 //大進程數(shù)
int Time; //時間片
int process_amount; //進程數(shù)量
int current_number = 0; //當(dāng)前執(zhí)行的“號碼牌”
int index = 0; //就緒隊列要發(fā)的“號碼牌”,初始值為0
struct PCB
{
char name[10]; //進程名字
int arrival_time; //到達時間
int service_time; //服務(wù)時間
int completion_time; //完成時刻
int sign_completion; //標(biāo)志是否完成調(diào)用,0表示沒完成,1表示完成
int remaining_time; //剩余服務(wù)時間
int number; //進程在就緒隊列里的“號碼牌”
}process[MAX];
void input() //初始化進程的信息
{
int i;
printf("請輸入時間片:\n");
scanf("%d",&Time);
printf("請輸入進程名稱、到達時間、服務(wù)時間:\n");
for(i = 0; i< process_amount; i ++)
{
printf("%d號進程:\n",i+1);
scanf("%s%d%d",process[i].name,&process[i].arrival_time,&process[i].service_time);
printf("\n");
process[i].remaining_time = process[i].service_time;
process[i].sign_completion = 0;
process[i].number = 0; //“號碼牌”初始為0
}
}
void BubbleSort() //冒泡排序算法對進程抵達時間先后排序
{
int i,j,n = process_amount;
for(i = 0; i< n - 1; i++)
for(j = 0; j< n - 1 - i; j++)
{
if(process[j].arrival_time >process[j+1].arrival_time)
{
process[n] = process[j+1];
process[j+1] = process[j];
process[j] = process[n];
}
}
}
void RunProcess() //時間片輪轉(zhuǎn)調(diào)用過程
{
int time = process[0].arrival_time; //給當(dāng)前時間賦初值
int sum = 0; //記錄完成的進程數(shù)
int i,j;
while(sum< process_amount)
{
for(i = 0; i< process_amount; i++)
if(current_number == process[i].number && process[i].sign_completion == 0)
{
if(process[i].remaining_time<= Time) //剩余服務(wù)時間少于等于一個時間片
{
time = time + process[i].remaining_time;
process[i].sign_completion = 1;
process[i].completion_time = time;
process[i].remaining_time = 0;
printf("%s ",process[i].name);
sum++;
current_number++;
for(j = i + 1; j< process_amount; j++) //檢測后面有沒有新進程到達
if(process[j].arrival_time<= time && process[j].number == 0)
{
index++;
process[j].number = index;
}
}
else if(process[i].remaining_time >Time)//剩余服務(wù)時間大于一個時間片
{
time = time + Time;
process[i].remaining_time -= Time;
printf("%s ",process[i].name);
current_number++;
for(j = i + 1; j< process_amount; j++) //檢測后面有沒有新進程到達
if(process[j].arrival_time<= time && process[j].number == 0)
{
index++;
process[j].number = index;
}
index++;
process[i].number = index;
}
}
if(index< current_number && sum< process_amount) // 還有沒執(zhí)行的進程,且沒進入就緒隊列
{
for(i = 0; i<= process_amount; i++)
if(process[i].sign_completion == 0)
{
time = process[i].arrival_time;
index++;
process[i].number = index;
break;
}
}
}
}
void output() //打印信息
{
int i;
printf("程序名 到達時間 服務(wù)時間 完成時間 周轉(zhuǎn)時間 帶權(quán)周轉(zhuǎn)時間\n");
for(i = 0; i< process_amount; i++)
{
float weight_time = (float)(process[i].completion_time - process[i].arrival_time)/process[i].service_time;
printf(" %s\t %d\t %d\t %d\t %d\t\t%.2f\n",process[i].name,process[i].arrival_time,process[i].service_time,
process[i].completion_time,process[i].completion_time-process[i].arrival_time,weight_time);
}
}
int main()
{
int f;
printf("模擬時間片輪轉(zhuǎn)法實現(xiàn)進程調(diào)度\n");
printf("請輸入總進程數(shù):\n");
scanf("%d",&process_amount);
input();
BubbleSort();
printf("進程運行順序:\n");
RunProcess();
printf("\n");
output();
printf("\n");
system("pause");
return 0;
}
實驗步驟1)先來先服務(wù)(First-Come First-Served,F(xiàn)CFS)調(diào)度算法,2)短作業(yè)優(yōu)先(Shortest-Job-First,SJF)調(diào)度算法,3)響應(yīng)比高者優(yōu)先(HRRF)調(diào)度算法,4)優(yōu)先權(quán)高者優(yōu)先(Highest-Priority-First,HPF)調(diào)度算法
代碼如下
使用vim tiaodu.c 編輯器并且編寫代碼,保存退出,再使用cat tiaodu.c查看
使用vim a.txt創(chuàng)建一個測試數(shù)據(jù),保存后使用cat b.txt查看
使用命令gcc tiaodu.c –o tiaodu.c 編譯代碼,./tiaodu運行程序,接著輸入測試數(shù)據(jù)的文件名
使用vim rr3.c 編輯器并且編寫代碼,保存退出
使用cat rr3.c查看
1)先來先服務(wù)(First-Come First-Served,F(xiàn)CFS)調(diào)度算法,2)短作業(yè)優(yōu)先(Shortest-Job-First,SJF)調(diào)度算法,3)響應(yīng)比高者優(yōu)先(HRRF)調(diào)度算法,4)優(yōu)先權(quán)高者優(yōu)先(Highest-Priority-First,HPF)調(diào)度算法
結(jié)果如下
時間片輪轉(zhuǎn)調(diào)度算法結(jié)果
編輯切換為居中
添加圖片注釋,不超過 140 字(可選)
實驗總結(jié)由四種算法的測試數(shù)據(jù)來看,算法思想不同,所需的等待時間和周轉(zhuǎn)時間也不同。
算法與等待時間、執(zhí)行時間、優(yōu)先級的關(guān)系
作業(yè)調(diào)度算法 | 等待時間 | 執(zhí)行時間 | 優(yōu)先權(quán) |
FCFS | √ | ||
SJF | √ | ||
HRRF | √ | √ | |
HPF | √ |
從上表得出FCFS算法僅考慮作業(yè)的等待時間,等待時間長的優(yōu)先考慮;SJF算法主要考慮作業(yè)的執(zhí)行時間,執(zhí)行時間短的優(yōu)先考慮;HRRF算法同時考慮了作業(yè)的等待時間和執(zhí)行時間,是FCFS和SJF算法的折中;HPF算法僅考慮作業(yè)的優(yōu)先權(quán),優(yōu)先權(quán)高者先執(zhí)行。
我們實驗結(jié)果中可以發(fā)現(xiàn)對測試數(shù)據(jù)而言,并非HRRF算法的平均等待時間和平均周轉(zhuǎn)時間最短。對于這組作業(yè),SJF算法的平均等待時間和平均周轉(zhuǎn)時間比 HRRF算法和HPF算法的短,說明最適合這個作業(yè)的調(diào)度算法是SJF。
由此可以得出判斷算法的好壞要根據(jù)具體的作業(yè),如果對于a作業(yè)A算法的平均等待時間和周轉(zhuǎn)時間是最短的,那說明A算法是最適合a作業(yè)的調(diào)度算法。
心得FCFS算法比較有利于長作業(yè),而不利于短作業(yè),有利于CPU繁忙型作業(yè),而不利于I/O繁忙型作業(yè)。并且 用于批處理系統(tǒng),不適于分時系統(tǒng)。
SJF算法易于實現(xiàn),照顧了短進程,縮短了短進程的等待時間,體現(xiàn)了短進程優(yōu)先原則,改善了平均周轉(zhuǎn)時間和平均帶權(quán)周轉(zhuǎn)時間,有利于提高系統(tǒng)的吞吐量。但是對長進程不利,甚至?xí)?dǎo)致長進程長時間無法得到關(guān)注而使得系統(tǒng)整體性能下降,完全未考慮進程的急迫程度,因而不能保證緊迫性進程會被及時處理,進程的運行時間很難精確估計,進程在運行前不一定能真正做到短進程被優(yōu)先調(diào)度。
HRRF算法既照顧了短作業(yè)又照顧了長作業(yè),同時也照顧了先到達進程。但是調(diào)度之前需要計算各個進程的響應(yīng)比,增加了系統(tǒng)開銷,導(dǎo)致對實時進程無法做出及時反映。
HPF算法調(diào)度靈活,能適應(yīng)多種調(diào)度需求。但是進程優(yōu)先級的劃分和確定每個進程優(yōu)先級比較困難,同時搶占式調(diào)度增加了系統(tǒng)開銷。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
新聞標(biāo)題:操作系統(tǒng)期末課程設(shè)計-創(chuàng)新互聯(lián)
標(biāo)題來源:http://m.kartarina.com/article48/ccgshp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、企業(yè)建站、App設(shè)計、網(wǎng)站排名、網(wǎng)站收錄、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容