#include <stdio.h>
#define getjcb(type) (type*)malloc(sizeof(type))
//#define NULL 0
int n=0, time = 0;
float eti, ewi;
struct jcb
...{ 
char name[10]; /**//* 作业名 */
char state; /**//* 作业状态 W:wait; R:run; F:finish*/
int ts; /**//* 提交时间 */
float super; /**//* 优先权 */
int tb; /**//* 开始运行时间 */
int tc; /**//* 完成时间 */
float ti; /**//* 周转时间 */
float wi; /**//* 带权周转时间 */
int ntime; /**//* 作业所需运行时间 */
char resource[10]; /**//* 所需资源 */
struct jcb *link; /**//* 结构体指针 */
} *p,*q,*head=NULL;
typedef struct jcb JCB;
void inital()
...{
int i;
printf(" Input jcb num ");
scanf("%d", &n);
printf("Input name ts ntime resource ");
for(i = 0; i < n; i++)
...{
p = getjcb(JCB);
scanf("%s %d %d %s", &p->name, &p->ts, &p->ntime, &p->resource);
p->state = 'W';
p->link = NULL;
if(head == NULL)
head = q = p;
else
...{
q->link = p;
q = p;
}
}
}
void fileinput()
...{
FILE *fp;
int i;
if((fp = fopen("os2.txt","r")) == NULL)
printf(" open error! ");
fscanf(fp, "%d ", &n);
for(i = 0; i < n; i++)
...{
p = getjcb(JCB);
fscanf(fp,"%s%d%d%s", &p->name, &p->ts, &p->ntime, &p->resource);
p->state = 'W';
p->link = NULL;
if(head == NULL)
head = q = p;
else
...{
q->link = p;
q = p;
}
}
fclose( fp );
}

void print(JCB *pr, int m)
...{
JCB *p;
printf(" time=%d", time);
if(m == 3)
...{
printf(" name state ts ntime super source tb tc ti wi ");
printf("%s %c %d %d %4.2f %s %d %d %4.2f %4.2f ",
pr->name, pr->state, pr->ts, pr->ntime, pr->super, pr->resource, pr->tb, pr->tc, pr->ti, pr->wi);
}
else
...{
printf(" name state ts ntime source tb tc ti wi ");
printf("%s %c %d %d %s %d %d %4.2f %4.2f ",
pr->name, pr->state, pr->ts, pr->ntime, pr->resource, pr->tb, pr->tc, pr->ti, pr->wi);
}
p = head;
do...{
if(p->state == 'W')
if(m==3)
...{
printf("%s %c %d %d %4.2f %s ",
p->name, p->state, p->ts, p->ntime, p->super, p->resource);
}
else
...{
printf("%s %c %d %d %s ",
p->name, p->state, p->ts, p->ntime, p->resource);
}
p = p->link;
}while( p != NULL );
p = head;
do...{
if(p->state == 'F')
if(m == 3)...{
printf("%s %c %d %d %4.2f %s %d %d %4.2f %4.2f ",
p->name, p->state, p->ts, p->ntime, p->super, p->resource, p->tb, p->tc, p->ti, p->wi);
}
else...{
printf("%s %c %d %d %s %d %d %4.2f %4.2f ",
p->name, p->state, p->ts, p->ntime, p->resource, p->tb, p->tc, p->ti, p->wi);
}
p = p->link;
}while( p != NULL );
}
void last()
...{
eti /= n;
ewi /= n;
printf(" eti=%7.3f ewi=%7.3f ", eti, ewi);
}
void super()
...{
JCB *padv;
padv = head;

do...{
if(padv->state == 'W' && padv->ts <= time)
padv->super = (float) (time - padv->ts + padv->ntime) / padv->ntime;
padv = padv->link;
}while( padv != NULL );
}
running(JCB *p, int m)
...{
p->tb = time;
p->state = 'R';
p->tc = p->tb + p->ntime;
p->ti = (float) (p->tc - p->ts);
p->wi = (float) (p->ti / p->ntime);
eti += p->ti;
ewi += p->wi;
print(p, m);
time += p->ntime;
p->state = 'F';
printf(" %s has been finished! press any key to continue... ", p->name);
getch();
}
void hrn(m)
...{
JCB *min;
int i, iden;
for(i = 0; i < n; i++)
...{
p = min = head;
iden = 1;
super();

do...{
if(p->state == 'W' && p->ts <= time)
if(iden)
...{
min=p;
iden=0;
}
else if(p->super > min->super)
min=p;
p=p->link;
}while( p != NULL );
if( iden )
...{
i--;
time++;
printf(" time=%d: no JCB submib...wait...", time);
if(time>1000)
...{
printf(" runtime is too long...error...");
getch();
}
}
else
...{
running(min, m);
}
}
}
void sjf(int m)
...{
JCB *min;
int i, iden;
for(i = 0; i < n; i++)
...{
p = min = head;
iden = 1;

do...{
if(p->state == 'W' && p->ts <= time)
if( iden )
...{
min = p;
iden = 0;
}
else if(p->ntime < min->ntime)
min = p;
p = p->link;
}while( p != NULL );
if( iden )
...{
i--;
printf(" time=%d: no JCB submib...wait...", time);
time++;
if(time > 100)
...{
printf(" runtime is too long...error");
getch();
}
}
else
...{
running(min, m);
}
}
}
void fcfs(int m)
...{
int i, iden;
printf(" the jcb is runing...");
for(i = 0; i < n; i++)
...{
p = head;
iden = 1;
do...{
if(p->state == 'W' && p->ts <= time)
iden = 0;
if( iden )
p = p->link;
}while(p != NULL && iden);
if( iden )
...{
i--;
printf(" time=%d: no JCB submib...wait...", time);
time++;
if(time > 100)
...{
printf(" runtime is too long...error");
getch();
}
}
else
...{
running(p, m);
}
}
}

void runjcb(int m)
...{
printf(" start running jcb...");
switch( m )
...{
case 1:
fcfs( m );
break;
case 2:
sjf( m );
break;
case 3:
hrn( m );
break;
default:
printf(" runjcb error... ");
return ;
}
}
void fileway()
...{
printf(" adopts the file way to input the datas...y/n:");
if(getch() == 'y')
fileinput();
else
inital();
}
void start()
...{
int m;
char str[100] = " select algorithm 1.FCFS 2.SJF 3.HRN " ;
printf("%s", str);
m=getch()-48;
fileway();
if(1 <= m && m <= 3)
runjcb( m );
else
...{
printf(" select error!try again... ");
start();
}
last();
}

void main()
...{
start();
printf(" finished!");
getch();
}
os2.txt中的内容如下:
7
rody 5 8 will
tom 3 5 bill
king 8 1 pill
kity 9 2 mill
jim 2 3 hill
kelly 4 6 till
bob 6 4 qill

本文介绍了一个基于C语言实现的作业调度程序,该程序包括FCFS(先来先服务)、SJF(最短作业优先)及HRN(最高响应比优先)三种经典作业调度算法。通过用户输入或文件读取的方式获取作业信息,如作业名称、提交时间、所需运行时间等,并计算出每个作业的等待时间和周转时间等关键指标。
3047

被折叠的 条评论
为什么被折叠?



