按時間片輪轉排程演算法(C 實現)

2021-09-24 04:39:17 字數 2918 閱讀 4248

演算法思想:

按時間片輪轉排程演算法:

(1)假設系統中有5個程序,每個程序有乙個程序控制塊(pcb)來標識。程序控制塊內容包括:程序名,鏈結指標,到達時間,估計執行時間,程序狀態。

程序名即程序標識。

鏈結指標:按照程序到達系統的時間將處於就緒狀態的程序連線成乙個就緒佇列。指標指出下乙個到達程序的程序控制塊位址。最後乙個程序的鏈結指標為null。

(2)為每個程序任意確定乙個到達時間和要求執行時間。

(3)設定乙個隊首指標head,用來指出最先進入系統的程序。各就緒程序通過鏈結指標連在一起構成乙個迴圈佇列。

(4)處理機排程時開始選擇隊首指標指向的程序投入執行。由於本實驗是模擬試驗,所以對被選中程序並不實際啟動執行,而只是執行:估計執行時間減1。用這個操作來模擬程序的一次執行,而且省去程序的現場保護和現場恢復工作。

(5)程序執行一次後,以後的排程則將當前指標依次下移乙個位置,指向下乙個程序。同時還應判斷該程序的剩餘的執行時間是否為0,若不為0,則等待下一輪的執行;若該程序的剩餘時間為0,則把它的狀態改為完成態(c),並撤出就緒佇列。

(6)若就緒佇列非空,則重複上述的(4)和(5),直到所有程序為完成態。

(7)在所設計的程式中應有顯示或列印語句,能顯示或列印正執行程序的程序名、已執行時間、還剩時間、就緒佇列中的程序等。

**:

#include#includeusing namespace std;

typedef struct pcb pcb;

void createprocess(pcb *p, int n)

r->next = null;

q->next = null;

q = null;

delete q;

delete r;

cout << endl << endl;

}void sortofarrivetime(pcb *p, int n)

q = q->next;

} }t = null;

m = null;

q = null;

delete t;

delete m;

delete q;

}void runprocess(pcb *p, int n)

if (number>n)

break;

} if (number>n)//所有程序都不能進行訪問

continue;

cout << "正在執行的程序" << endl;

cout << "程序名\t到達時間 服務時間 已執行時間 還剩執行時間" << endl;//輸出當前正在執行的程序

cout << m->pname << "\t" << m->arrivetime << "\t " << m->servicetime << "\t ";

cout << m->servicetime - m->estimatedrunningtime << "\t " << m->estimatedrunningtime << endl;

m->estimatedrunningtime--;

m->finishtime = currenttime + 1;

if (m->estimatedrunningtime == 0)

m->state = 'c';

cout << "程序" << m->pname << "執行一次之後就緒佇列中的程序" << endl;

cout << "程序名\t到達時間 服務時間 已執行時間 還剩執行時間" << endl;

s = p->next;

while (s)

s = s->next;

} cout << endl << endl << endl;

q = m;

m = m->next;//q、m指標後移

if (m == null)

s = p->next;

while (s&&s->state == 'c')

s = s->next;

if (s == null)//若所有程序已完成,則退出迴圈

break;

i++;

} q = p;

m = p->next;

for (int i = 0; istarttime = m->arrivetime;

m->turnaroundtime = m->finishtime - m->arrivetime;

m->weightedturaroundtime = m->turnaroundtime*1.0 / m->servicetime;

} else

m = m->next;

} q = null;

m = null;

s = null;

delete q;

delete m;

delete s;

cout << endl;

}void printprocess(pcb *p)

cout << endl << endl;

}int main()

實驗結果:

時間片輪轉排程演算法

include define max 10 struct task struct tasks max int counter 實際程序個數 int time counter 0 int poutput 排程結果輸出 int time int charge 判斷是否所有的程序都被執行過 int tim...

UCOSIII時間片輪轉排程

os rate hz const oscfg tickrate hz os rate hz os cfg tick rate hz define os cfg tick rate hz 200u tick rate in hertz 10 to 1000 hz 時間片長度200hz,也就是乙個系統時...

作業系統 時間片輪轉排程演算法

時間片輪轉法 round robin,rr 主要用於分時系統中的程序排程。為了實現輪轉排程,系統把所有就緒程序按先入先出的原則排成乙個佇列。新來的程序加到就緒佇列末尾。每當執行程序排程時,程序排程程式總是選出就緒佇列的隊首程序,讓它在cpu上執行乙個時間片的時間。時間片是乙個小的時間單位,通常為10...