#include<stdio.h> #include<stdlib.h> #define MAX 100 struct job { char name[10]; //作业名 int reachtime; //作业到达时间 int starttime; //作业开始时间 int needtime; //作业运行时间 int zztime; //作业周转时间 int endtime; //作业完成时间 double zzxs; //周转系数 char state; //作业状态 }; typedef struct job JOB; void input(JOB job[],int *jnum) { int i,num; printf("请输入作业个数:"); scanf("%d",&num); for(i=0;i<num;i++) { printf(" 请输入第%d个作业的名称:",i+1); scanf("%s",&job[i].name); printf(" 请输入第%d个作业的提交时间:",i+1); scanf("%d",&job[i].reachtime); printf(" 请输入第%d个作业的运行时间:",i+1); scanf("%d",&job[i].needtime); job[i].state='W'; } *jnum=num; } void btsort(JOB btjob[],int n) //按作业到达时间排序 { int i,j; JOB jobtemp; for(j=1;j<n;j++) { for(i=0;i<n-1;i++) { if(btjob[i].reachtime>btjob[j].reachtime) { jobtemp=btjob[i]; btjob[i]=btjob[j]; btjob[j]=jobtemp; } } } } void btsortarrived(JOB btjob[],int n) //按作业执行时间排序 { int i,j; JOB jobtemp; for(j=1;j<n;j++) { for(i=0;i<n-1;i++) { if(btjob[i].needtime>btjob[j].needtime) { jobtemp=btjob[i]; btjob[i]=btjob[j]; btjob[j]=jobtemp; } } } } void NotarrToArr(JOB notarrived[],int *pnumnotarr,JOB arrived[],int *pnumarr,int systime)//按照系统时间,将小于等于系统时间的作业放入后背队列 { int j; while((*pnumnotarr>0)&&(notarrived[0].reachtime<=systime)) { arrived[*pnumarr]=notarrived[0]; (*pnumnotarr)--; (*pnumarr)++; for(j=0;j<*pnumnotarr;j++) notarrived[j]=notarrived[j+1]; } } void jobfinprintf(JOB pjob[],int n) { int i; printf("作业名称 提交时间 运行时间 开始时刻 完成时刻 周转时间 周转系数 "); for(i=0;i<n;i++) { printf(" %s %d %d %d %d %d %.1f ",pjob[i].name,pjob[i].reachtime,pjob[i].needtime,pjob[i].starttime,pjob[i].endtime,pjob[i].zztime,pjob[i].zzxs); } } void jobprint(JOB job[],int n) //输出数组中的作业 { int i; printf("作业名称 提交时间 运行时间"); for(i=0;i<n;i++) { printf(" %s %d %d",job[i].name,job[i].reachtime,job[i].needtime); } } void fcfs(JOB jobnotarrived[],int numnotarr) { JOB jobarrived[MAX],jobfinished[MAX]; int numarr=0,numfin=0; int systime=0; JOB jobrunning; int j; while(jobnotarrived[0].reachtime>systime) { printf(" 当前系统时间为:%d ",systime); system("pause"); systime++; } while(numnotarr>0||numarr>0) { printf(" 当前系统时间为:%d ",systime); NotarrToArr(jobnotarrived,&numnotarr,jobarrived,&numarr,systime); system("pause"); if(numnotarr>0) { printf("还未进入后备队列的作业! "); jobprint(jobnotarrived,numnotarr); } if(numarr==0) systime++; if(numarr>0) { printf(" 进入后备队列的作业! "); //input(jobarrived,numarr); jobprint(jobarrived,numarr); jobrunning=jobarrived[0]; for(j=0;j<numarr;j++) jobarrived[j]=jobarrived[j+1]; numarr--; jobrunning.state='R'; jobrunning.starttime=systime; jobrunning.endtime=jobrunning.starttime+jobrunning.needtime; jobrunning.zztime=jobrunning.endtime-jobrunning.reachtime; jobrunning.zzxs=(jobrunning.zztime*1.0)/(jobrunning.needtime*1.0); printf(" 处于运行的作业为:%s ",jobrunning.name); jobfinished[numfin]=jobrunning; numfin++; systime=systime+jobrunning.needtime; printf("已经完成的作业! "); jobfinprintf(jobfinished,numfin); system("pause"); } } } void sjf(JOB jobnotarrived[],int numnotarr) { JOB jobarrived[MAX],jobfinished[MAX]; int numarr=0,numfin=0; int systime=0; JOB jobrunning; int j; while(jobnotarrived[0].reachtime>systime) { printf(" 当前系统时间为:%d ",systime); system("pause"); systime++; } while(numnotarr>0||numarr>0) { printf(" 当前系统时间为:%d ",systime); NotarrToArr(jobnotarrived,&numnotarr,jobarrived,&numarr,systime); system("pause"); if(numnotarr>0) { printf("还未进入后备队列的作业! "); jobprint(jobnotarrived,numnotarr); } if(numarr==0) systime++; if(numarr>0) { printf(" 进入后备队列的作业! "); btsortarrived(jobarrived,numarr); jobprint(jobarrived,numarr); jobrunning=jobarrived[0]; for(j=0;j<numarr;j++) jobarrived[j]=jobarrived[j+1]; numarr--; jobrunning.state='R'; jobrunning.starttime=systime; jobrunning.endtime=jobrunning.starttime+jobrunning.needtime; jobrunning.zztime=jobrunning.endtime-jobrunning.reachtime; jobrunning.zzxs=(jobrunning.zztime*1.0)/(jobrunning.needtime*1.0); printf(" 处于运行的作业为:%s ",jobrunning.name); jobfinished[numfin]=jobrunning; numfin++; systime=systime+jobrunning.needtime; printf("已经完成的作业! "); jobfinprintf(jobfinished,numfin); system("pause"); } } } void mune(JOB jobnotarrived[],int numnotarr) { int m; printf(" ****************************** "); printf(" 作业调度模拟演示 "); printf(" ****************************** "); printf(" 1.先来先服务算法."); printf(" 2.最短作业优先算法."); printf(" 0.退出."); printf(" 选择所要操作:"); scanf("%d",&m); switch(m) { case 1: fcfs(jobnotarrived,numnotarr); break; case 2: sjf(jobnotarrived,numnotarr); break; case 0: break; default: printf("选择错误,请重新选择:"); } } main() { JOB jobarrived[MAX],jobfinished[MAX],jobnotarrived[MAX]; int numarr,numfin,numnotarr; int systime; JOB jobrunning; int j; input(jobnotarrived,&numnotarr); printf("原始数据如下: "); jobprint(jobnotarrived,numnotarr); btsort(jobnotarrived,numnotarr); printf(" 按提交时间排序后,还没进入后背队列的任务! "); jobprint(jobnotarrived,numnotarr); numarr=0; numfin=0; systime=0; mune(jobnotarrived,numnotarr); }