操作系统操作系统 实验二 作业调度模拟程序
#include<stdio.h> #include<string.h> #include<windows.h> typedef struct jcb { char name[10]; char status; //状态:开始s,等待d,就绪j int id; int arrtime; //到达时间 int reqtime; //要求服务时间 int startime; //开始时间 int finitime; //完成时间 float TAtime,TAWtime; //周转时间、带权周转时间 float prio; //优先级 float HRRF; //响应比 }jcb; jcb jcbs[100]; //最多100个作业 int systime=0,bfsum,add,del; int intarr,infin,intjob,sumjcb; char T='A'; //T的默认值是A void ins() //插入 { int i; while(T!='E'&&T!='e') { printf("'I'nsert or 'D'elete or 'E'xit?\n"); getchar(); scanf("%c",&T); if(T=='I'||T=='i') { printf("你想插入多少个作业?\n"); scanf("%d",&add); bfsum=sumjcb; sumjcb=sumjcb+add; for(i=bfsum+1;i<=sumjcb;i++) { printf("\n第%d个作业:\n",i); getchar(); printf("输入作业名:\n"); gets(jcbs[i].name); printf("到达时间:\n"); scanf("%d",&jcbs[i].arrtime); printf("要求服务时间:\n"); scanf("%d",&jcbs[i].reqtime); } } else if(T=='D'||T=='d') { printf("你想删除第几组?"); scanf("%d",&del); for(i=del;i<=sumjcb-1;i++) { strcpy(jcbs[i].name,jcbs[i+1].name); jcbs[i].arrtime=jcbs[i+1].arrtime; jcbs[i].reqtime=jcbs[i+1].reqtime; } sumjcb--; } } } void jisuan() { int i; jcbs[1].startime=jcbs[1].arrtime; jcbs[1].finitime=jcbs[1].arrtime+jcbs[1].reqtime; jcbs[1].TAtime=(float)(jcbs[1].finitime-jcbs[1].arrtime); jcbs[1].TAWtime=(float)(jcbs[1].TAtime/jcbs[1].reqtime); jcbs[1].HRRF=(float)(jcbs[1].TAtime/jcbs[1].reqtime); for(i=2;i<=sumjcb;i++) { jcbs[i].startime=jcbs[i-1].finitime; jcbs[i].finitime=jcbs[i].startime+jcbs[i].reqtime; jcbs[i].TAtime=(float)(jcbs[i].finitime-jcbs[i].arrtime); jcbs[i].TAWtime=(float)(jcbs[i].TAtime/jcbs[i].reqtime); jcbs[i].HRRF=(float)(jcbs[i].TAtime/jcbs[i].reqtime); } } void input() { int i; printf("作业个数:\n"); scanf("%d",&sumjcb); for(i=1;i<=sumjcb;i++) { printf("\n第%d个作业:\n",i); getchar(); printf("输入作业名:\n"); gets(jcbs[i].name); printf("到达时间:\n"); scanf("%d",&jcbs[i].arrtime); printf("要求服务时间:\n"); scanf("%d",&jcbs[i].reqtime); } } void Pseudo_random_number() { int i; srand((unsigned)time(0)); //参数seed是rand()的种子,用来初始化rand()的起始值。 sumjcb=rand()%23+5; for(i=1;i<=sumjcb;i++) { itoa(i,jcbs[i].name, 2); jcbs[i].arrtime=rand()%29+1; jcbs[i].reqtime=rand()%7+1; } printf("\n \tid \t作业到达时间 \t作业运行所需要时间\n"); for(i=1; i<=sumjcb; i++) { printf("\n\t%s\t%12d\t%15d",jcbs[i].name,jcbs[i].arrtime,jcbs[i].reqtime); } printf("\n"); } int readfiles() { int m=0; int i=1; FILE *fp; //定义文件指针 fp=fopen("C:\\3.txt","r"); //打开文件 if(fp==NULL) { printf("File open error !\n"); exit(0); } printf("\n id 作业到达时间 作业运行所需要时间\n"); while(!feof(fp)) { fscanf(fp,"%d%d%d",&jcbs[i].id,&jcbs[i].arrtime,&jcbs[i].reqtime); //fscanf()函数将数据读入 printf("\n%3d%12d%15d\n",jcbs[i].id,jcbs[i].arrtime,jcbs[i].reqtime); //输出到屏幕 itoa(jcbs[i].id,jcbs[i].name,10); i++; } sumjcb=i-1; if(fclose(fp)) //关闭文件 { printf("Can not close the file !\n"); exit(0); } m=i-1; return m; } /*void printarr() //打印 { int i; printf("\t\tname\tartime\trqtime\n"); for(i=1;i<=sumjcb;i++) { printf("N %d\t",i); printf("\t%s",jcbs[i].name); printf("\t%d",jcbs[i].arrtime); printf("\t%d\n",jcbs[i].reqtime); } printf("\t\t\t\t\t\t现在系统时间%d\n",systime); }*/ void suanfa(int choose) //排序 { int i,j; jcb temp; for(i=1;i<=sumjcb;i++) { for(j=i+1;j<=sumjcb;j++) { if(choose==1) { if(jcbs[i].arrtime>jcbs[j].arrtime) { strcpy(temp.name,jcbs[i].name); temp.status=jcbs[i].status; temp.arrtime=jcbs[i].arrtime; temp.reqtime=jcbs[i].reqtime; temp.startime=jcbs[i].startime; temp.finitime=jcbs[i].finitime; temp.TAtime=jcbs[i].TAtime; temp.TAWtime=jcbs[i].TAWtime; temp.prio=jcbs[i].prio; temp.id=jcbs[i].id; temp.HRRF=jcbs[i].HRRF; strcpy(jcbs[i].name,jcbs[j].name); jcbs[i].status=jcbs[j].status; jcbs[i].arrtime=jcbs[j].arrtime; jcbs[i].reqtime=jcbs[j].reqtime; jcbs[i].startime=jcbs[j].startime; jcbs[i].finitime=jcbs[j].finitime; jcbs[i].TAtime=jcbs[j].TAtime; jcbs[i].TAWtime=jcbs[j].TAWtime; jcbs[i].prio=jcbs[j].prio; jcbs[i].id=jcbs[j].id; jcbs[i].HRRF=jcbs[j].HRRF; strcpy(jcbs[j].name,temp.name); jcbs[j].status=temp.status; jcbs[j].arrtime=temp.arrtime; jcbs[j].reqtime=temp.reqtime; jcbs[j].startime=temp.startime; jcbs[j].finitime=temp.finitime; jcbs[j].TAtime=temp.TAtime; jcbs[j].TAWtime=temp.TAWtime; jcbs[j].prio=temp.prio; jcbs[j].id=temp.id; jcbs[j].HRRF=temp.HRRF; } } else if(choose==2) { if(jcbs[i].reqtime>jcbs[j].reqtime) { strcpy(temp.name,jcbs[i].name); temp.status=jcbs[i].status; temp.arrtime=jcbs[i].arrtime; temp.reqtime=jcbs[i].reqtime; temp.startime=jcbs[i].startime; temp.finitime=jcbs[i].finitime; temp.TAtime=jcbs[i].TAtime; temp.TAWtime=jcbs[i].TAWtime; temp.prio=jcbs[i].prio; temp.id=jcbs[i].id; temp.HRRF=jcbs[i].HRRF; strcpy(jcbs[i].name,jcbs[j].name); jcbs[i].status=jcbs[j].status; jcbs[i].arrtime=jcbs[j].arrtime; jcbs[i].reqtime=jcbs[j].reqtime; jcbs[i].startime=jcbs[j].startime; jcbs[i].finitime=jcbs[j].finitime; jcbs[i].TAtime=jcbs[j].TAtime; jcbs[i].TAWtime=jcbs[j].TAWtime; jcbs[i].prio=jcbs[j].prio; jcbs[i].id=jcbs[j].id; jcbs[i].HRRF=jcbs[j].HRRF; strcpy(jcbs[j].name,temp.name); jcbs[j].status=temp.status; jcbs[j].arrtime=temp.arrtime; jcbs[j].reqtime=temp.reqtime; jcbs[j].startime=temp.startime; jcbs[j].finitime=temp.finitime; jcbs[j].TAtime=temp.TAtime; jcbs[j].TAWtime=temp.TAWtime; jcbs[j].prio=temp.prio; jcbs[j].id=temp.id; jcbs[j].HRRF=temp.HRRF; } } else if(choose==3) { if(jcbs[i].HRRF<jcbs[j].HRRF) { strcpy(temp.name,jcbs[i].name); temp.status=jcbs[i].status; temp.arrtime=jcbs[i].arrtime; temp.reqtime=jcbs[i].reqtime; temp.startime=jcbs[i].startime; temp.finitime=jcbs[i].finitime; temp.TAtime=jcbs[i].TAtime; temp.TAWtime=jcbs[i].TAWtime; temp.prio=jcbs[i].prio; temp.id=jcbs[i].id; temp.HRRF=jcbs[i].HRRF; strcpy(jcbs[i].name,jcbs[j].name); jcbs[i].status=jcbs[j].status; jcbs[i].arrtime=jcbs[j].arrtime; jcbs[i].reqtime=jcbs[j].reqtime; jcbs[i].startime=jcbs[j].startime; jcbs[i].finitime=jcbs[j].finitime; jcbs[i].TAtime=jcbs[j].TAtime; jcbs[i].TAWtime=jcbs[j].TAWtime; jcbs[i].prio=jcbs[j].prio; jcbs[i].id=jcbs[j].id; jcbs[i].HRRF=jcbs[j].HRRF; strcpy(jcbs[j].name,temp.name); jcbs[j].status=temp.status; jcbs[j].arrtime=temp.arrtime; jcbs[j].reqtime=temp.reqtime; jcbs[j].startime=temp.startime; jcbs[j].finitime=temp.finitime; jcbs[j].TAtime=temp.TAtime; jcbs[j].TAWtime=temp.TAWtime; jcbs[j].prio=temp.prio; jcbs[j].id=temp.id; jcbs[j].HRRF=temp.HRRF; } } else printf("Error!\n"); } } } void printz() { int i; float sum1=0,sum2=0; //计算平均周转时间、平均带权周转时间 printf("\tname\tartime\trqtime\tstime\tftime\tTAtime\t\tTAWtime\n"); for(i=1;i<=sumjcb;i++) { printf("N %d\t",i); printf("%s",jcbs[i].name); printf("\t%d",jcbs[i].arrtime); printf("\t%d",jcbs[i].reqtime); printf("\t%d",jcbs[i].startime); printf("\t%d",jcbs[i].finitime); printf("\t%f",jcbs[i].TAtime); printf("\t%f\n",jcbs[i].TAWtime); } for(i=1;i<=sumjcb;i++) { sum1=sum1+jcbs[i].TAtime; } for(i=1;i<=sumjcb;i++) { sum2=sum2+jcbs[i].TAWtime; } printf("\n平均周转时间=%f\n",sum1/sumjcb); printf("\n平均带权周转时间=%f\n",sum2/sumjcb); printf("\t\t\t\t\t\t现在系统时间%d\n",systime); } void startit() { int n; printf("\n"); printf("**************************\n"); printf("**1.调用文本写入数据******\n"); printf("**2.调用伪随机数产生数据**\n"); printf("**3.调用自己输入模拟数据**\n"); printf("**************************\n"); scanf("%d",&n); switch(n) { case 1: readfiles(); jisuan(); break; case 2: Pseudo_random_number(); jisuan(); break; case 3: input(); jisuan(); break; default: { printf("输入有误,请重新输入"); break; } } } void choosesuanfa() { int n; printf("*****************************\n"); printf("*******1.FCFS算法调度********\n"); printf("*******2.SJF算法调度*********\n"); printf("*******3.HRRF算法调度********\n"); printf("*******4.调用系统清屏********\n"); printf("*******5.退出算法调度********\n"); printf("*****************************\n"); scanf("%d",&n); switch(n) { case 1: printf("运行先来先服务算法FCFS\n"); suanfa(1); printf("\n经按到达时间排序后,未达到队列是\n"); printz(); break; case 2: printf("运行最短作业优先算法FJS\n"); suanfa(2); printf("\n经按到达时间排序后,未达到队列是\n"); printz(); break; case 3: printf("运行最高响应比优先算法HRRF\n"); suanfa(3); printf("\n经按到达时间排序后,未达到队列是\n"); printz(); break; case 4: system("cls"); break; case 5: exit(0); break; } } void main() { while(1){ startit(); choosesuanfa(); } }