操作系统实验2
#include <stdio.h> #define n 10 struct jcb{ int name; //进程名 float arrtime; //进程到达时间 float reqtime; //进程运行时间 float ftime; //进程完成时间 float cyclingtime; //周转时间 float welght; //带权周转时间(周转系数) int arun; //进程到达运行 }f[n]; void fcfs(struct jcb f[n],int jobnum); void result(struct jcb f[n],int jobnum); void main(){ struct jcb f[n]; int jobnum,i,j,m; float a,r; printf("作业个数:"); scanf("%d",&jobnum); for(i=0;i<jobnum;i++){ printf("\n第%d个作业",i+1); printf("\n输入作业名:"); scanf("%d",&f[i].name); printf("到达时间:"); scanf("%f",&f[i].arrtime); printf("要求服务时间:"); scanf("%f",&f[i].reqtime); } printf("经按到达时间排序后,未到达队列是\n"); //排序算法 for(i=0;i<jobnum;i++){ for(j=i+1;j<jobnum;j++){ if(f[j].arrtime<f[i].arrtime) { m=f[j].name; a=f[j].arrtime; r=f[j].reqtime; f[j].name=f[i].name; f[j].arrtime=f[i].arrtime; f[j].reqtime=f[i].reqtime; f[i].name=m; f[i].arrtime=a; f[i].reqtime=r; } } } printf(" \tname\tarrtime\treqtime\n"); for(i=0;i<jobnum;i++){ printf("N%d\t%d\t%.2f\t%.2f\n",i+1,f[i].name,f[i].arrtime,f[i].reqtime); } fcfs(f,jobnum); printf("FCFS算法作业序列表"); printf("------------------------------------------------\n"); result(f,jobnum); } void fcfs(struct jcb f[n],int jobnum){ int i; for(i=0;i<jobnum;i++){ if(i==0){ //第一个进程 f[i].cyclingtime=f[i].reqtime; f[i].ftime=f[i].arrtime+f[i].reqtime; } else if(f[i].arrtime>f[i-1].ftime){//第i个进程到达系统时,第i-1个进程已运行完毕 f[i].cyclingtime=f[i].reqtime; f[i].ftime=f[i].arrtime+f[i].reqtime; } else{//第i个进程到达系统时,第i-1个进程未运行完毕 f[i].cyclingtime=f[i].reqtime+f[i-1].ftime-f[i].arrtime; f[i].ftime=f[i].arrtime+f[i].cyclingtime; } f[i].welght=f[i].cyclingtime*1.0/f[i].reqtime; } } void result(struct jcb f[n],int jobnum){ int i; float avercyclingtime; float averwelght; int sum_cyclingtime=0; float sum_welght=0.00; printf("\n作业名\t到达系统时间\tCPU所需时间/h\t结束时间\t周转时间/h\n"); for(i=0;i<jobnum;i++){ printf("%d \t%.2f \t%.2f \t%.2f \t%.2f\n",f[i].name,f[i].arrtime,f[i].reqtime,f[i].ftime,f[i].cyclingtime); sum_cyclingtime=sum_cyclingtime+f[i].cyclingtime; sum_welght=sum_welght+f[i].welght; } avercyclingtime=sum_cyclingtime*1.0/jobnum; averwelght=sum_welght*1.00/jobnum; printf("\n平均周转时间:%.2f\n",avercyclingtime); printf("平均带权周转时间:%.2f\n",averwelght); }