調度類型---OS的三級調度
1.作業調度---高級調度(用於決定將外存上處於後備隊列中的哪些作業調入內存,處於內存就緒隊列,準備執行)
2.進程調度---低級調度(決定就緒隊列中哪個進程將獲得處理機)
3.交換調度---中級調度(目的是提高內存的利用率和系統吞吐量)吞吐量指單位時間內處理工作量的總和
進程調度的方式:
1.非剝奪方式(非搶佔方式)
一旦佔有cpu,直至完成後阻塞;不利於實時任務;不利於短作業;
2.剝奪方式(搶佔方式)
在一定情況下,可剝奪一進程佔有的處理機;
搶佔的原則有:
(1).短作業(進程)優先;
(2).時間片原則;
(3).優先權原則;
進程調度的算法,在這裏,只討論單道作業的操作系統;
1.先來先服務調度算法
2.短作業(進程)優先調度算法
3.時間片輪轉調度算法
4.優先權調度算法--靜態優先級,動態優先級
如下圖所示:
時間片輪轉:
代碼實現:
#include <cassert>
#include<iostream>
using namespace std
enum Statue
{
READY,
BLOCK,
RUNING,
};
typedef struct Pcb
{
int _name; //進程名
int _atime; //到達時間
int _runtime; //服務時間
int _start; //開始執行時間
int _finish; //完成時間
int _cycling; //週轉時間
int _wi; //帶權週轉時間
Statue _statue; //進程狀態
bool operator!=(Pcb f)
{
return _name!=f._name;
}
bool operator==(Pcb f)
{
return _name == f._name;
}
Pcb(int name = 0,int atime = 0,int runtime = 0,Statue statue = READY)
:_name(name)
,_atime(atime)
,_runtime(runtime)
,_start(0)
,_finish(_start+_runtime)
,_statue(statue)
,_cycling(0)
,_wi(0)
{}
};
class JobScheduling
{
public:
JobScheduling()
{
int amount,i;
printf("input a number:\n");
scanf("%d",&amount);
_f.resize(amount);
for(i=0;i<_f.size();i++)
{
printf("請輸入進程名,進程到達時間,進程運行時間:\n");
scanf("%d",&_f[i]._name);
scanf("%d",&_f[i]._atime);
scanf("%d",&_f[i]._runtime);
}
Sort(amount);
for(i = 0; i<amount; ++i) //修改進程的開始執行時間,週轉時間,帶權週轉時間
{
if(i == 0)
_f[i]._start = 0;
else
_f[i]._start = _f[i-1]._start+_f[i-1]._runtime;
_f[i]._finish = _f[i]._start+_f[i]._runtime;
_f[i]._cycling = _f[i]._finish-_f[i]._atime;
_f[i]._wi = _f[i]._cycling/_f[i]._runtime;
}
}
void Print()
{
cout<<"進程名"<<" "<<"到達時間"<<" "<<"服務時間"<<" "<<"開始執行時間"
<<" "<<"完成時間"<<" "<<"週轉時間"<<" "<<"帶權週轉時間"
<<" "<<"進程狀態"<<endl;
for(int i = 0; i < _f.size();++i)
{
cout<<_f[i]._name<<" ";
cout<<_f[i]._atime<<" ";
cout<<_f[i]._runtime<<" ";
cout<<_f[i]._start<<" ";
cout<<_f[i]._finish<<" ";
cout<<_f[i]._cycling<<" ";
cout<<_f[i]._wi<<" ";
if(_f[i]._statue == READY)
cout<<"READY";
else if(_f[i]._statue == RUNING)
cout<<"RUNING";
else
cout<<"BLOCK";
cout<<endl;
}
}
void FcFs()//先來先服務
{
while(!_f.empty())
{
int i = _f.size();
while(i--)
{
_f[0]._statue = RUNING;
Print();
_f.erase(_f.begin());
}
}
}
void RoundRobin(int slot)//時間片輪轉
{
for(int i = 0; i<_f.size(); ++i)
{
_f[i]._statue = RUNING;
Print();
if(_f[i]._runtime == slot)
{
_f.erase(_f.begin());
i -= 1;
}
else if(_f[i]._runtime > slot)
{
Pcb tmp = _f[i];
tmp._runtime -= slot;
tmp._statue = BLOCK;
_f.erase(_f.begin());
_f.push_back(tmp);
i-=1;
}
else
{
_f.erase(_f.begin());
continue;
}
}
}
//短作業優先(short job first)
void SJF()
{
SortSJF();
while(!_f.empty())
{
int i = _f.size();
while(i--)
{
_f[0]._statue = RUNING;
Print();
_f.erase(_f.begin());
}
}
}
void SortSJF()
{
int size = _f.size();
for(int i = 0; i<size; ++i)
{
for(int j = 1; j<size-i-1; ++j)
{
if(_f[j]._atime <= _f[i]._finish
&& _f[j+1]._atime <= _f[i]._finish
&& _f[j+1]._runtime < _f[j]._runtime)
{
Swap(_f[j],_f[j+1]);
/*int atime =_f[j]._atime;
_f[j]._atime=_f[j+1]._atime;
_f[j+1]._atime=atime;
int name=_f[j]._name;
_f[j]._name=_f[j+1]._name;
_f[j+1]._name=name;
int runtime =_f[j]._runtime;
_f[j]._runtime = _f[j+1]._runtime;
_f[j+1]._runtime = runtime;
int start = _f[j]._start;
_f[j]._start = _f[j+1]._start;
_f[j+1]._start = start;
int finish = _f[j]._finish;
_f[j]._finish = _f[j+1]._finish;
_f[j+1]._finish = finish;
Statue statue = _f[j]._statue;
_f[j]._statue = _f[j+1]._statue;
_f[j+1]._statue = statue;
int cycling = _f[j]._cycling;
_f[j]._cycling = _f[j+1]._cycling;
_f[j+1]._cycling = cycling;
int wi = _f[j]._wi;
_f[j]._wi = _f[j+1]._wi;
_f[j+1]._wi = wi;*/
}
}
}
}
void Sort(int amount)
{
int l,k;
int size = _f.size();
for(int i=0;i<size;i++) //按進程到達時間的先後排序
{ //如果兩個進程同時到達,按在屏幕先輸入的先運行
for(int j=0;j<size-i-1;j++)
{
if(_f[j]._atime>_f[j+1]._atime)
{
Swap(_f[j],_f[j+1]);
}
}
}
}
protected:
vector<Pcb> _f;
};
void Test()
{
JobScheduling fc;
//fc.FcFs();
//fc.RoundRobin(2);
fc.SJF();
}