#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"iostream.h"
typedef struct pcb
{
char pname[10];
int prio;
//int round;
int cputime;
int needtime;
int servertime;
char state;
struct pcb *next;
}PCB;
PCB *finish,*ready,*run;
int N;
void printready()
{
PCB *p;
if(ready==NULL) printf("******就绪队列为空! \n");
else
{
printf("******就绪队列为:");
p=ready;
while(p!=NULL)
{
printf("%s---",p->pname);
p=p->next;
}
printf(" \n ");
}
}
void prt1()
{
//if(toupper(a)=='p')
//cout<<" "<<endl;
printf("进程名 已运行时间 剩余时间 要求服务时间 状态\n");
}
void prt2(PCB *q) /*轮转法的输出*/
{
//if(toupper(a)=='P')
//printf("进程名 已运行时间 剩余时间 要求服务时间 状态");
printf("%3s%10d%12d%12d%12c\n",q->pname,q->cputime,q->needtime,q->servertime,q->state);
}
void prt()
{
PCB *p;
prt1();
if(run!=NULL) prt2(run);
p=ready;
while(p!=NULL)
{
prt2(p);
p=p->next;
}
p=finish;
while(p!=NULL)
{
prt2(p);
p=p->next;
}
//getchar();
printready();
}
void insert(PCB *q)
{
PCB *p1,*s,*r;
s=q;
p1=ready;
r=p1;
while(p1!=NULL)
{
r=p1;
p1=p1->next;
}
if(r!=p1)
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1;
ready=s;
}
}
void create()
{
PCB *p;
int i,time;
//char name[10];
ready=NULL;
finish=NULL;
run=NULL;
// printf("请逐个输入进程名及进程要求运行的时间:\n");
for(i=1;i<=N;i++)
{
p=new PCB;
printf("*****请逐个输入进程名及进程要求运行的时间:");
scanf("%s",p->pname);
scanf("%d",&time);
//strcpy(p->pname,name);
printf("\n");
p->cputime=0;
p->needtime=time;
p->servertime=time;
p->state='w';
p->prio=i;
if(ready!=NULL) insert(p);
else
{
p->next=ready;
ready=p;
}
}
printready();
printf("*****************具体过程如下:******************\n");
run=ready;
ready=ready->next;
run->state='R';
prt();
}
void priority(int T)
{
while(run!=NULL)
{
run->cputime=run->cputime+T;
run->needtime=run->needtime-T;
run->prio=run->prio+N;
if(run->needtime==0)
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
{
run=ready;
run->state='R';
ready=ready->next;
}
}
else
if(ready!=NULL)
{
run->state='W';
insert(run);
run=ready;
run->state='R';
ready=ready->next;
}
prt();
}
}
void main()
{
int T;
//char algo='p';
printf("***************时间片轮转调度算法模拟********************\n");
printf("*****请输入进程的个数:");
scanf("%d",&N);
printf("*****请输入轮转时间片周期T:");
scanf("%d",&T);
create();
priority(T);
}