#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"iostream.h"
typedef struct node
{
char name[10];
int prio;
int round;
int cputime;
int needtime;
int count;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run;
int N;
void firstin()
{run=ready;
run->state='R';
ready=ready->next;
}
void prt1(char a)
{if(toupper(a)=='p')
cout<<" "<<endl;
cout<<"进程名 占用CPU时间 到完成还要的时间 优先级 状态"<<endl;
}
void prt2(char a,PCB *q)
{if(toupper(a)=='P')
cout<<q->name<<" "<<q->cputime<<" "<<q->needtime<<" "<<q->prio<<" "<<q->state<<endl;
}
void prt(char algo)
{
PCB *p;
prt1(algo);
if(run!=NULL)
prt2(algo,run);
p=ready;
while(p!=NULL)
{prt2
(algo,p);
p=p->next;
}
p=finish;
while(p!=NULL)
{prt2(algo,p);
p=p->next;
}
getchar();
}
void insert(PCB *q)
{PCB *p1,*s,*r;
int b;
s=q;
p1=ready;
r=p1;
b=1;
while((p1!=NULL)&&b)
if(p1->prio>=s->prio)
{r=p1;
p1=p1->next;
}
else
b=0;
if(r!=p1)
{r->next=s;
s->next=p1;
}
else
{s->next=p1;
ready=s;
}
}
void create(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
cout<<"输入进程名及其需要运行的时间:"<<endl;
for(i=1;i<=N;i++)
{p=new PCB;
cin>>na;
cin>>time;
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='w';
p->prio=100-time;
if(ready!=NULL)
insert(p);
else
{p->next=ready;
ready=p;
}
cout<<"输入进程名及其需要运行的时间:"<<endl;
}
prt(alg);
run=ready;
ready=ready->next;
run->state='R';
}
void priority(char alg)
{
while(run!=NULL)
{run->cputime=run->cputime+10;
run->needtime=run->needtime-10;
run->prio=run->prio-10;
if(run->needtime==0)
{run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
firstin();
}
else
if((ready!=NULL)&&(run->prio<ready->prio))
{
run->state='W';
insert(run);
firstin();
}
prt(alg);
}
}
void main()
{
char algo='p';
cout<<"输入进程的个数:";
cin>>N;
create(algo);
priority(algo);
}