操作系統(tǒng)進(jìn)程調(diào)度的基本算法,時間片輪轉(zhuǎn)。
假設(shè)cpu只有單核,但是進(jìn)程很多,最簡單就是先來先服務(wù),但是會造成后來的進(jìn)程等待很久,所以有另一種策略就是每個進(jìn)程都服務(wù)一會,這樣就不會出現(xiàn)一個進(jìn)程長時間得不到服務(wù)的情況? ?(餓死現(xiàn)象),在輪流服務(wù)一會的方式中,由于cpu一般速度較快,所以我們可能會產(chǎn)生一種錯覺就是有多個cpu在給我們服務(wù),我們的多個進(jìn)程好像都收到反饋了
輸入即將到達(dá)的進(jìn)程數(shù)還有進(jìn)程的信息,時間片大小,初始化pcb, 設(shè)時間為0
創(chuàng)建3條隊列(就緒,未到達(dá),完成) ,各進(jìn)程按到達(dá)時間排序, 入未到達(dá)隊列
外循環(huán)(以是否有進(jìn)程還沒執(zhí)行好為條件)
? ? { 內(nèi)循環(huán)(當(dāng)前時間未到達(dá)隊列是否有進(jìn)程到達(dá)了 為條件)
? ? ? ? { ???送入就緒隊列
? ? ? ? }
? ? ? ?if(就緒隊列空) ?
? ? ? ?{ 時間加一,后面不用執(zhí)行
? ? ? ? }
? ? ? ?else
? ? ? ?{ 就緒隊列取進(jìn)程,狀態(tài)改’run’
? ? ? ?時間片循環(huán)(時間片未用完 為條件)
? ? ? { 時間加一
? ? ? ? 循環(huán)(當(dāng)前時間未到達(dá)隊列是有進(jìn)程到達(dá))
? ? ? ? { ???送入就緒隊列
? ? ? ? ?}
? ? ? ? if(剩余服務(wù)時間==0)
? ? ? ? { 進(jìn)程狀態(tài)’finish’
? ? ? ? ? ? 完成時間就是當(dāng)前時間
? ? ? ? ? ? 插入完成隊列
? ? ? ? ? ?跳出時間片循環(huán)
? ? ? ?}
? ? }
? ? if(進(jìn)程狀態(tài)’run’)
? ? { 進(jìn)程狀態(tài)’wait’
? ? ? 插入就緒隊列
? ? }
?}
}
打印結(jié)果
示例代碼如下
#define time_slice 3 //時間片長度
#define N 5 //進(jìn)程數(shù)
#include<iostream>
#include <queue>
using namespace std;
struct pcb
{
char process_name[10]; //進(jìn)程名字
int arrive_time; //到達(dá)時間
int service_time; //需要服務(wù)時間
int remain_time; //還差多少時間
int complete_time; //完成時間
char state; //進(jìn)程狀態(tài)
}PCB[N + 1]; //0號單元不存數(shù)據(jù),直接用作交換
queue<pcb> Wait_queue; //就緒隊列
queue<pcb> Coming_queue; //還未到達(dá)
queue<pcb> Finish_queue; //已完成
void sort(int n) //按到達(dá)時間升序
{
int i, j;
for (i = 1; i < n; i++)
{
for (j = 1; j <= n - i; j++)
{
if (PCB[j].arrive_time > PCB[j + 1].arrive_time)
{
PCB[0] = PCB[j];
PCB[j] = PCB[j + 1];
PCB[j + 1] = PCB[0];
}
}
}
}
int rr(int n)
{
int i, time = 0, t;
cout << "\n請輸入各進(jìn)程的信息\n" << endl;
for (i = 1; i <= n; i++) //輸入各進(jìn)程信息,插入未到達(dá)隊列
{
PCB[i].state = 'w';
cout << "------\n請輸入第" << i << "進(jìn)程名字: ";
cin >> PCB[i].process_name;
cout << "請輸入到達(dá)時間: ";
cin >> PCB[i].arrive_time;
cout << "請輸入服務(wù)時間: ";
cin >> PCB[i].service_time;
PCB[i].remain_time = PCB[i].service_time;
}
sort(n);
for (i = 1; i <= n; i++)
{
Coming_queue.push(PCB[i]);
}
i = 1;
// 進(jìn)程還有未執(zhí)行完畢
while (Coming_queue.empty() == false || Wait_queue.empty() == false)
{
while (Coming_queue.empty() == false && time >= Coming_queue.front().arrive_time) //有進(jìn)程到達(dá)
{
Wait_queue.push(Coming_queue.front()); //從未到達(dá)隊列 放到就緒隊列
Coming_queue.pop();
}
if(Wait_queue.empty() == true) //就緒隊列空,時間加1,本次不運行進(jìn)程
{
time++;
continue;
}
else //就緒隊列非空
{
cout << "\n第" << i << "次調(diào)度執(zhí)行進(jìn)程:" << Wait_queue.front().process_name
<< "\t 時間: " << time << endl;
t = time_slice; //t等于時間片
PCB[0] = Wait_queue.front(); //取出就緒隊列頭
Wait_queue.pop();
PCB[0].state = 'r'; //狀態(tài)正在運行
while (t-- > 0)
{
time++;
while (Coming_queue.empty() == false && time >= Coming_queue.front().arrive_time) //有進(jìn)程到達(dá)
{
Wait_queue.push(Coming_queue.front()); //插入就緒隊列
Coming_queue.pop();
}
if (--PCB[0].remain_time == 0) //剩余服務(wù)時間-1,結(jié)果已經(jīng)為0
{
PCB[0].state = 'f'; //進(jìn)程完成
PCB[0].complete_time = time; //完成時間就是當(dāng)前時間
Finish_queue.push(PCB[0]); //插入完成隊列
break;
}
}
if (PCB[0].state == 'r') //時間片結(jié)束還沒執(zhí)行完
{
PCB[0].state = 'w'; //回到就緒隊列
Wait_queue.push(PCB[0]);
}
}
i++;
}
return 0;
}
void print()
{
int i = 0;
float round_time[N], //周轉(zhuǎn)時間
force_round_time[N], //帶權(quán)周轉(zhuǎn)時間
sum = 0; //存放各進(jìn)程的帶權(quán)周轉(zhuǎn)時間和
cout << "\n 進(jìn)程 |" << "到達(dá)時間 |" << " 服務(wù)時間 |" << " 完成時間 |" << " 周轉(zhuǎn)時間 |" << " 帶權(quán)周轉(zhuǎn)時間" << endl;
while (Finish_queue.empty() == false)
{
round_time[i] = Finish_queue.front().complete_time - Finish_queue.front().arrive_time;
force_round_time[i] = round_time[i] / Finish_queue.front().service_time;
cout << Finish_queue.front().process_name
<< "\t| " << Finish_queue.front().arrive_time
<< "\t | " << Finish_queue.front().service_time << " \t | " << Finish_queue.front().complete_time
<< "\t | " << round_time[i]
<< "\t | " << force_round_time[i]
<< endl;
Finish_queue.pop();
sum += force_round_time[i];
i++;
}
cout << "\n\n系統(tǒng)平均帶權(quán)周轉(zhuǎn)時間: " << (sum / i) << endl;
}
int main()
{
cout << "\t\t時間片輪轉(zhuǎn)調(diào)度算法" << endl;
rr(N);
print();
return 0;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-770602.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-770602.html
到了這里,關(guān)于時間片輪轉(zhuǎn)算法(c++)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!