#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
struct pcb{
char name[5];
char state;
int super;
int needtime;
int rtime;
int tijiaotime;
int starttime;
int ftime;
int zztime;
int dzztime;
struct pcb* link;
}*ready=NULL, *p;
typedef struct pcb PCB;
float pzztime=0;
float pdzztime=0;
int n;
int time=20;
void sort()
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->super)<(ready->super)))
{
p->link=ready;
ready=p;
}
else
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->super)<(second->super))
{
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else
{
first=first->link;
second=second->link;
}
}
if(insert==0) first->link=p;
}
}
void input()
{
int i;
PCB *q;
printf("请输入进程数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n第%d个进程:\n",i);
p=getpch(struct pcb);
printf("\n 进程名:");
scanf("%s",&p->name);
printf("\n 优先级:");
scanf("%d",&p->super);
printf("\n 运行时间:");
scanf("%d",&p->needtime);
printf("\n提交时间:");
scanf("%d",&p->tijiaotime);
p->rtime=0;
p->state=‘R‘;
p->link=NULL;
sort();
}
q=ready;
}
int space()
{
int l=0;
PCB *pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void disp(PCB *p2)
{
time = p2->tijiaotime > time? p2->tijiaotime:time;
p2->starttime=time;
time+=p2->needtime;
p2->state=‘R‘;
p2->ftime=time;
p2->zztime=p2->ftime-p2->tijiaotime;
p2->dzztime=p2->zztime/p2->needtime;
pzztime=p2->zztime+pzztime;
pdzztime=p2->dzztime+pdzztime;
printf("周转时间:%d\n",p->zztime);
printf("带权周转时间为 %d\n",p->dzztime);
}
void destroy()
{
free(p);
}
void running()
{
p->rtime=p->rtime+1;
if(p->rtime==p->needtime)
{ p->state=‘C‘;
printf("%s",p->name);
printf("运行完毕\n");
disp(p);
destroy();
}
else
{
(p->super)++;
sort();
printf("正在运行进程%s\n",p->name);
}
}
void main()
{
int len,h=0;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
h++;
p=ready;
ready=p->link;
p->link=NULL;
running();
}
pzztime=pzztime/n;
pdzztime=pdzztime/n;
printf("\n平均周转时间:%f\n平均带权周转时间为%f\n",pzztime,pdzztime);
}