您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > C语言的磁盘调度算法
#includestdio.h#includestdlib.h#includemalloc.h#definenull0#definelensizeof(structcidaohao)structcidaohao{structcidaohao*pre;intnum;structcidaohao*next;};FCFS(array)intarray[50];{inti,j,sum=0;printf(\nFCFS:);for(i=1;array[i]!=-1;i++){printf(%d,array[i]);}i=0;for(i=0,j=1;array[j]!=-1;i++,j++){if(array[i]array[j])sum+=(array[i]-array[j]);elsesum+=(array[j]-array[i]);}return(sum);}SSTF(head,now)structcidaohao*head;intnow;{structcidaohao*p,*lp,*rp;intsum=0,front,behind;p=head;printf(\nSSTF:);while(p-num!=now)p=p-next;/**/lp=p-pre;rp=p-next;do{if(p-next!=null&&p-pre!=null){front=p-num-lp-num;behind=rp-num-p-num;if(front=behind){sum+=behind;p=rp;printf(%d,p-num);rp=p-next;}else{sum+=front;p=lp;printf(%d,p-num);lp=p-pre;}}else{if(p-next==null){while(lp-num!=0){sum+=p-num-lp-num;p=lp;printf(%d,p-num);lp=p-pre;}return(sum);}if(p-pre==null){while(rp-num!=0){sum+=rp-num-p-num;p=rp;printf(%d,p-num);rp=p-next;}return(sum);}}}while(p-next!=null||p-pre!=null);}SCAN(head,n,m)structcidaohao*head;intn,m;{structcidaohao*p,*pp;intsum=0;printf(\nSCAN:);p=head;while(p-num!=m)p=p-next;/**/pp=p;if(nm){while(pp-next!=null){sum+=pp-next-num-pp-num;pp=pp-next;printf(%d,pp-num);}sum+=pp-num-p-pre-num;pp=p-pre;if(pp-num==0)return(sum);else{while(pp-pre!=null){printf(%d,pp-num);sum+=pp-num-pp-pre-num;pp=pp-pre;}printf(%d,pp-num);return(sum);}}else{while(pp-pre!=null){sum+=pp-num-pp-pre-num;pp=pp-pre;printf(%d,pp-num);}sum+=p-next-num-pp-num;pp=p-next;if(pp-num==0)return(sum);else{while(pp-next!=null){printf(%d,pp-num);sum+=pp-next-num-pp-num;pp=pp-next;}printf(%d,pp-num);return(sum);}}}main(){FILE*fp;charpt;charstr[10];intcidao[100],i,j=1,count1=0,count2=0,count3=0,last,space=0;structcidaohao*p1,*p2,*new,*head;/**/structcidaohao*p,*lp,*rp;for(i=0;i50;i++)cidao[i]=-1;i=0;fp=fopen(cipan.txt,r+);/**/if(fp==NULL){printf(Cann'topenthisfile);exit(0);}printf(\nPleaseinputcidaohaonow:);scanf(%d,&cidao[0]);while((pt=fgetc(fp))!=EOF)/**/{if(pt='0'&&pt='9'){str[i]=pt;i++;space=0;}else{if(pt==''||pt=='\n'){if(space==1)break;else{str[i]='\0';cidao[j]=atoi(str);if(pt=='\n')break;else{space=1;j++;i=0;}}}}}/**/if(pt==EOF){str[i]='\0';cidao[j]=atoi(str);}fclose(fp);i=0;count1=FCFS(cidao);/**/printf(\nTheTotal:%d\n,count1);p1=p2=head=(structcidaohao*)malloc(len);/**/p1-pre=null;p1-num=cidao[0];p1-next=null;i=1;while(cidao[i]!=-1){if(cidao[i]head-num)/**/{p1=(structcidaohao*)malloc(len);p1-next=head;p1-pre=null;p1-num=cidao[i];head-pre=p1;head=p1;}else{while(p1-next!=null&&p1-num=cidao[i])/**/{p2=p1;p1=p1-next;}if(p1-numcidao[i])/**/{new=(structcidaohao*)malloc(len);new-num=cidao[i];new-next=p1;new-pre=p2;p1-pre=new;p2-next=new;}else{/**/new=(structcidaohao*)malloc(len);new-num=cidao[i];new-next=null;new-pre=p1;p1-next=new;}p1=head;/**/}i++;/**/}count2=SSTF(head,cidao[0]);printf(\nTheTotal:%d,count2);printf(\n\nPleastinputlastcipanhao:);scanf(%d,&last);count3=SCAN(head,last,cidao[0]);printf(\nTheTotal:%d\n,count3);}
本文标题:C语言的磁盘调度算法
链接地址:https://www.777doc.com/doc-2010446 .html