您的位置:首页 > 其它

操作系统之进程调度

2012-02-07 14:40 204 查看
编写并调试一个模拟的进程调度程序,采用“最高优先数优先”调度算法对五个进程进行调度。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class process
{
private:
char name;
int ntime;
int utime;
bool state;
public:
int prio;
process()
{
}
void init()
{

utime=0;
state=true;
cout<<"请分别输入进程的名字,优先级,所需时间"<<endl;
cin>>name>>prio>>ntime;
//		cout<<endl;
}
bool run()
{
utime=utime+1;
prio=prio-1;
cout<<name<<"\t"<<prio<<"\t"<<utime<<"\t"<<endl;
if(ntime==utime)
state=false;
return state;

}
void wait()
{
prio=prio+1;
}
void prn()
{
cout<<name<<"的状态:优先级是 "<<prio
<<". 所需时间是 "<<ntime<<"."<<endl;
}
};
int cmp(const process & a,const process & b)
{
if(a.prio<b.prio)
return 1;
else
return 0;
}
int main()
{
vector <process> vp;
process p[5];
for(int i=0;i<5;i++)
{
p[i].init();
vp.push_back(p[i]);
}
for(i=0;i<5;i++)
p[i].prn();
cout<<"进程名\t优先级\t已用时间\t"<<endl;
while(!vp.empty())
{
sort(vp.begin(),vp.end(),cmp);
for(i=0;i<vp.size()-1;i++)
{
vp[i].wait();
}
if(!vp.back().run())
vp.pop_back();

}

return 1;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: