当前位置:文档之家› 按优先数调度算法实现处理机调度C++程序代码

按优先数调度算法实现处理机调度C++程序代码

#include<iostream>
using namespace std;
struct PCB
{
char Name; //进程名
float Time; //要求运行时间
int Level; //优先数
bool state; //状态,1表就绪
PCB *next; //指针
};
void Init(PCB *head)
{
int num;
PCB *s,*p;
cout<<"请输入进程数";
cin>>num;
for(int i=0;i <num;i++)
{
p=head;
s=new PCB;
cout<<"请依次输入进程名要求运行时间优先数";
cin>>s->Name>>s->Time>>s->Level;
if(s->Time>0)
{
s->state =1;
while(p->next)
{
if(s->Level >p->next->Level )break;
p=p->next ;
}
s->next=p->next;
p->next=s;
}
else
{
s->state =0;
cout<<"此进程要求运行时间时间不符合要求,不添加入进程列表";
}
}
}
int Run(PCB *head)
{
PCB *cur,*p;
p=head;
cur=p->next;
p->next =cur->next;
cur->Level--;
cur->Time--;
cout<<"此次执行的进程信息(执行后):进程名";
cout<<cur->Name<<"剩余时间"<<cur->Time<<"优先数"<<cur->Level;
if(cur->Time<=0)
{
cout<<"状态为完成态"<<endl;
delete cur;
}
else
{
cout<<"状态为就绪态"<<endl;
while(p->next)
{
if(cur->Level >p->next->Level )break;
p=p->next ;
}
cur->next=p->next;
p->next=cur;
}
cout<<"此次执行后的进程列表序列为:";
p=head;
while(p->next)
{
cout<<p->next->Name<<" ";
p=p->next ;
}
cout<<endl; return 0;
}
int main()
{
PCB *Head;
Head=new PCB;
Head->next =NULL;
Init(Head);
while(Head->next )
{
Run(Head);
}
return 0;
}。

相关主题