📄 212.c
字号:
#include "stdio.h"
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct jcb {
char name[10];
char state;/*状态*/
float super;/*响应时间*/
int atime; /*到达时刻*/
int ntime; /*所需时间*/
int ctime; /*完成时刻*/
int stime; /*开始时刻*/
int ttime; /*周转时间*/
float bttime;/*带权周转时间 */
struct jcb* link;
}*ready=NULL,*p,*q;
typedef struct jcb JCB;
float wtime=0,wttime=0,wbttime=0;
int len;
queue()
{ if(ready==NULL)
{
p->link=ready;
ready=p;
q=p;
}
else
{ q->link=p;
q=p;
}
}
input()
{ int i,num;
clrscr(); /*清屏*/
printf("\n 请输入作业数:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n 作业号No.%d:\n",i);
p=getpch(JCB);
printf("\n 输入作业名:");
scanf("%s",p->name);
printf("\n 输入作业所需时间:");
scanf("%d",&p->ntime);
printf("\n");
p->atime=i;
p->state='W';
p->link=NULL;
queue(); /* 调用queue函数*/
}
}
int space()
{
int l=0; JCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void print(JCB* pr)
{ printf("\n完成时刻:%d",wtime);
printf("\n作业名|状态|提交时刻|所需时间|优先级|开始时刻|完成时刻|周转时间|带权周转时间\n");
printf("%s\t%c\t%d\t%d\t%4.2f\t%d\t%d\t%d\t%4.2f\n",
pr->name,pr->state,pr->atime,pr->ntime,pr->super,pr->stime,pr->ctime,pr->ttime,pr->bttime);
p=ready;
if(p!=NULL)
printf("\n作业名|\t状态| 提交时刻|\ 所需时间| 优先级\n");
while(p!=NULL)
{ if(p->state=='W')
printf("%s\t%c\t%d\t%d\t%4.2f\n", p->name,p->state,p->atime,p->ntime,p->super);
p=p->link;
}
}
super()
{ JCB *padv;
padv=ready;
do{
if(padv->state=='W'&&padv->atime<=wtime)
padv->super=(wtime-(float)padv->atime+(float)padv->ntime)/(float)padv->ntime;
padv=padv->link;
}while(padv!=NULL);
}
running(JCB *pr)
{ pr->stime=wtime;pr->state='R';
pr->ctime=pr->stime+pr->ntime;
pr->ttime=pr->ctime-pr->atime;
pr->bttime=(float)pr->ttime/(float)pr->ntime;
wttime+=pr->ttime;
wbttime+=pr->bttime;
print(pr);
wtime+=pr->ntime;
pr->state='F';
printf("\n%s 已经完成!\npress any key to continue...\n",pr->name);
getch();
}
void hrn()
{ JCB *min;
int i,iden;
iden=1;
for(i=0;i<len;i++)
{ p=min=ready;
super();
do{
if(p->state=='W'&&p->atime<=wtime)
{if(iden)
{ min=p;
iden=0;
}
else
if(p->super>min->super)
min=p;
}
p=p->link;
}while(p!=NULL);
if(iden)
{ i--;
wtime++;
printf("\ntime=%d:\tno JCB submib...wait...",wtime);
if(wtime>1000)
{ printf("\n运行时间太长...error...");
getch();
}
}
else
running(min);
}
}
void main()
{char ch;
input();
len=space();
hrn();
printf("\n\n 作业已经完成.\n");
printf("\n该次作业调度平均周转时间:%f\n",wttime/len);
printf("\n该次作业调度带权平均周转时间:%f\n",wbttime/len);
ch=getchar();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -