?? 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 + -