#include
#include
#define MAX 5
using namespace std;
enum Status{running,ready,blocked};
enum Policy{spf,rr};
typedef class PCB
{//定义PCB类。//data structure
public:
int id,cputime,alltime,startblock,blocktime;
Status state;
class PCB *next;
PCB()
{
}
}PCB,*PCBptr,**PCBpp;
PCBpp pp;//两个全局变量
void Print(PCBptr head)
{//打印head为头指针的PCB链表信息。
PCBptr p;
cout<<"\nRunning:";
for(p=head;p->next;p=p->next)
{
if(p->next->state==running)
{
cout<<"ID"<
break;
}
}
cout<<"\nReady Queue:";
for(p=head;p->next;p=p->next)
if(p->next->state==ready)
cout<<"ID"<
cout<<"\nBlock Queue:";
for(p=head;p->next;p=p->next)
if(p->next->state==blocked)
cout<<"ID"<
cout<<"\n-----------------------------------------------------------------------\n"
<<"|| PID ||has run || still need || when to block || block time || state\n";
for(p=head;p->next;p=p->next)
{
cout<<" "<
switch(p->next->state)
{
case ready:cout<<"ready";break;
case running:cout<<"run";break;
case blocked:cout<<"block";break;
default:exit(0);
}
cout<<'\n';
}
cout<<"------------------------------------------------------------------------\n"
<<"press any key to continue.....";
//cin>>x;
}
void Delete(PCBptr head,PCBptr p)
{//删除以head为头指针的PCB链表中p所指向的结点。
PCBptr q=head;
while(q->next!=p) q=q->next;
q->next=p->next;
delete p;
}
void InsertSort(PCBpp Rdy,PCBpp RdyEd,Policy algthm)
{//直接插入排序。
if(*(Rdy+1))//队列不为空
if(RdyEd-1!=Rdy+1)
{//Ready+1队列中不只一个。
if(algthm==spf)
{
if((*(RdyEd-1))->alltime<(*(RdyEd-2))->alltime)
{
PCBpp tt;
*Rdy=*(RdyEd-1);
*(RdyEd-1)=*(RdyEd-2);
for(tt=RdyEd-3;(*Rdy)->alltime<(*tt)->alltime;tt--)
*(tt+1)=*tt;
*(tt+1)=*Rdy;
}
}
}
}
void RunToBlk(PCBpp Run,PCBpp &BlkEd)
{//定义运行态转为阻塞态。
(*Run)->state=blocked;
*BlkEd=*Run;
BlkEd++;
}
void RdyToRun(PCBpp &Rdy,PCBpp Run,Policy algthm)
{//定义就绪态转为运行态。
if(algthm==spf)
{
if(*(Rdy+1))
{
(*(Rdy+1))->state=running;
*Run=*(Rdy+1);
Rdy++;
}
}
else
{
if(*Rdy)
{
(*Rdy)->state=running;
*Run=*Rdy;
Rdy++;
}
}
}
void RunToRdy(PCBpp Run,PCBpp Rdy,PCBpp &RdyEd,Policy algthm)
{//定义运行态转为就绪态。
(*Run)->state=ready;
*RdyEd=*Run;
RdyEd++;
if(algthm==spf)
InsertSort(Rdy,RdyEd,algthm);
}
int main()
{
cout<<"*********************starting******************\n\nhow many processes you want to create ?";
int n,i,time;
// cout< if(!(cin>>n)) {cerr<<"error"; exit(0);} PCBptr Listhead,Listp,Listq;//建立n个 Listhead=new PCB; // Listp=Listhead; // for(i=0;i<=n-1;i++) // { Listq=new PCB; // Listp->next=Listq; // Listp=Listq; // } Listp->next=0; //PCB的队列。 Policy algorithm; int num1,num2; cout<<"\ninput which algorithm to choose ,please ( SPF:0, RR:1 ) : "; while(1){ if(!(cin>>num1)) {cerr<<"error"; exit(0);} algorithm=Policy(num1); if(algorithm!=spf&&algorithm!=rr) cout<<"input again\n"; else break; } cout<<"\nnow we have "< Listp=Listhead->next; for(i=0;i<=n-1;i++) { cout<<"basic information of PCB about No."<:\n\n"; Listp->id=i; Listp->cputime=rand()%MAX; cout<<"the time has been run: "< Listp->alltime=rand()%MAX; cout<<"the time still need to run : "< Listp->startblock=rand()%MAX; cout<<"when to block: "< Listp->blocktime=rand()%MAX; cout<<"block time : "< cout<<"input the state of process (run:0; ready:1; block:2):"; while(1){ if(!(cin>>num2)) {cerr<<"error";exit(0);} if(num2!=0&&num2!=1&&num2!=2) {cout<<"the state is incorrect,input again:\n";} else break; } Listp->state=Status(num2); Listp=Listp->next; } PCBpp Run=new PCBptr(); // 生成 PCBpp Ready=new PCBptr[100]; // for(i=0;i<=99;i++) *(Ready+i)=0; // 运行 PCBpp ReadyEnd=Ready; // 就绪 PCBpp Block=new PCBptr[100]; // 阻塞 for( i=0;i<=99;i++) *(Block+i)=0; // PCBpp BlockEnd=Block; // 队列 if(algorithm==spf) ReadyEnd++;//如果是Spf算法,用直接插入排序,*Ready是哨兵,Ready+1成为队头。 for(Listp=Listhead;Listp->next;Listp=Listp->next) {//把初始化的进程放进各自队列 switch(Listp->next->state) { case running:*Run=Listp->next;break; case ready: { *ReadyEnd=Listp->next;ReadyEnd++; if(algorithm==spf) InsertSort(Ready,ReadyEnd,algorithm); break; } case blocked:*BlockEnd=Listp->next;BlockEnd++;break; } } cout<<"\nnow the "< // cin>>x; for(time=1;Listhead->next;time++) {//开始并发执行。 cout<<"\nafter "<