操作系统课程设计-模拟时间片轮转法实现处理器调度
操作系统课程设计-模拟时间片轮转法实现处理器调度|精品课程网站设计|课程设计网报告总结心得#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
class List;
class PCB
{
public:
friend class List;
private:
PCB (char p[],int t){strcpy(pname,p);runnedtime=0;requesttime=t;status='R';next=NULL;}
char pname[2];
int runnedtime;
int requesttime;
char status;
PCB* next;
};
class List
{
public:
List(char p[]="Q1",int t=0){list=new PCB(p,t);pc=list;time=0;}
int print();
int append(char p[],int t);
void insert(char p[],int t);
void run();
void link();
int length();
private:
PCB *list;
PCB *end();
PCB *pc;
int time;
};
int List::length()
{
int cnt=0;
PCB *pt=list;
for(;pt->next!=list;pt->next,cnt++)
;
return cnt;
}
void List::link()
{
if(list!=NULL)
(end())->next=list;
}
void List::insert(char p[],int t)
{
PCB *pt=new PCB(p,t);
pt->next=list;
list=pt;
}
int List::append(char p[],int t)
{
PCB *pt=new PCB(p,t);
if(list==NULL)
list=pt;
else
{
(end())->next=pt;
}
return 1;
}
PCB *List::end()
{
PCB *prv,*pt;
for(prv=pt=list;pt;prv=pt,pt=pt->next)
;
return prv;
}
int List :: print()
{
time++;
if(list==0)
return 0;
PCB *pt=list;
cout<<"name run_time req_time state"<<endl;
while(pt)
{
cout<<" ";
cout<<pt->pname[0]<<pt->pname[1]<<" ";
cout<<pt->runnedtime<<" ";
cout<<pt->requesttime<<" ";
cout<<pt->status<<" ";
cout<<endl;
pt=pt->next;
}
getch();
return 1;
}
void List:: run()
{
for(;pc;)
{
PCB *pt=list;
cout<<"********************************************"<<endl;
cout<<"CPU运行时间:"<<time<<endl;
cout<<"正在运行的进程为:"<<pc->pname<<endl;
pc->runnedtime+=1;
if(pc->runnedtime==pc->requesttime)
{
pc->status='E';
cout<<"******************************************"<<endl;
cout<<"进程"<<pc->pname<<"运行时间已满,退出队列!"<<endl;
if(pc==list)
{
list=list->next;
pc=pc->next;
}
else
{
for(;pt->next!=pc;pt=pt->next)
;
if(pc->next!=NULL)
{
pt->next=pc->next;
pc=pc->next;
}
else
{
pt->next=NULL;
pc=list;
}
}
}
else
{
if(pc->next!=NULL)
pc=pc->next;
else
pc=list;
}
print();
}
}
void main()
{
cout<<"**********************模拟时间片轮转法实现处理器调度************************"<<endl;
List list("Q1",3);
list.append("Q2",4);
list.append("Q3",2);
list.append("Q4",6);
list.append("Q5",8);
cout<<"初始化各进程状态如下:"<<endl;
list.print();
list.run();
}