调度类型---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();
}