#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Check();
typedef struct node
{
char name[10];
int span;//轮转时间
int take;//占用时间
int used;//已用的时间
int need;//还需要的时间
char status;//状态
struct node *next;
}PCB;
PCB *ready,*finish,*tail,*rear;
void Print(PCB *p)
{
while(p!=NULL)
{
printf("%3s%5d%5d%5d%5d%5c\n",p->name,p->used,p->need,p->take,p->span,p->status);
p=p->next;
}
}
void Creat(int N)
{
PCB *p;
int time;
char na[10];
ready=NULL; /*就绪队列头指针*/
finish=NULL; /*完成队列头指针*/
tail=NULL;
rear=NULL;
int i;
printf("Enter name and time of round process \n");
for(i=1;i<=N;i++)//创建就绪队列
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->span=2;
p->take=0;
p->used=0;
p->need=time;
p->status='J';
p->next=NULL;
if(ready==NULL){ready=p;tail=p;}
else{tail->next=p;tail=p;}
}
printf("……………………运行结果……………………\n");
}
void Print1()
{//输出ready与finish队列
PCB *p;
p=ready;
printf("name,used,need,take,span,state\n");
Print(p);
p=finish;
Print(p);
}
void Run()
{
Print1();
ready->used+=1;
ready->need-=1;
ready->take+=1;
ready->status='R';
if(ready->need!=0)//进程时间need片是否为0
{
if(ready->take<ready->span)
/*占用时间是否到轮转时间,没到就继续,到了就排到就绪队列尾*/
{
Run();
}
else
{
tail->next=ready;ready=ready->next;tail=tail->next;
tail->status='J';tail->take=0;tail->next=NULL;
Check();
}
}
else//进程撤销,放入撤销队列
{
if(finish==NULL)
{finish=ready;rear=finish;}
else
{rear->next=ready;rear=rear->next;}
ready=ready->next;rear->next=NULL;rear->status='F';rear->take=0;
Check();
}
//Check();由于上多次文调用Run本身递归,此句也会被多调用,产生在结束时的冗余结果;
}
void Check()
{
if(ready!=NULL) {Run();}
else Print1();
}
int main()
{
int N=0;
printf("输入进程数目:");
scanf("%d",&N);
Creat(N);
Check();
return 0;
}