驱动调度实验报告
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
i++;
}
}
else{
/*当前磁头不在头或尾*/
while(key!=0){
/*向内移动*/
step=R[key]-R[key-1];
count+=step;
printf("\n %d
%d---->%d
%d",i+1,R[key],R[key-1], step); key--;
i++;
}
i++;
step=R[temp+1]-R[key];
scanf("%d",&choose);//输入选择
count+=step;
printf("\n %d
%d---->%d
key=temp+1;
while(key!=request_num){
step=R[key+1]-R[key];
count+=step;
printf("\n %d
%d---->%d
key++;
i++;
}
}
printf("\nTotal moving steps:%d",count);
}
/*电梯扫描算法*/
void SCAN(int *R,int present_disk,int request_num){
int i=0;
int temp,key;
int count=0,step=0;
key=SearchPresentຫໍສະໝຸດ BaiduR,request_num , present_disk); /*查找当前磁道*/
void SetBaseDisk(int *base_num){/*设置磁盘磁道数*/ int i;
printf("\ninput number of base disk(1~%d):",DISKMAX); scanf("%d",base_num); if(*base_num<=0||*base_num>DISKMAX){printf("ERROR!");exit(0);} printf("\nThe base disk is:\n"); for(i=0;i<*base_num;i++){ printf(" %d",i); } }
key=SearchPresent(R,request_num , present_disk);
/*查找当前磁道*/
printf("\nMoving Order Moving Path Moving Steps");
while(key!=0&&key!=request_num){
/*磁道未到头或尾*/
i++;
if((R[key]-R[key-1-down])<(R[key+1+up]-R[key])){ /*左边的磁道比右边的磁道近*/
temp=key-1-down;
/*记录下一个磁道*/
down_step++;
down=0;
up=down_step+up_step;
}
else {
/*右边的磁道比左边的磁道近*/
SetRequestDisk(R,&request_num,base_num);
SetPresentDisk(&present_disk,&base_num);
do{
printf("\n/*********************************/");
printf("\n\n1.先到先服务 FCFS 磁盘调度算法");
temp=key;
if(key==request_num){
/* 当 前 磁 头 在 磁 道 头 */
while(key!=0){
/*向内移动*/
step=R[key]-R[key-1];
count+=step;
printf("\n %d
%d---->%d
%d",i+1,R[key],R[key-1], step); key--;
i++;
}
}
else if(key==0){
/*当前磁头在磁道尾*/
while(key!=request_num){
step=R[key+1]-R[key];
count+=step;
printf("\n %d
%d---->%d
%d",i+1,R[key],R[key+1], step);
key++;
printf("\nAverage moving steps:%d",count/request_num);
}
int main(){
int R[DISKTOTAL];
int base_num,request_num,present_disk;
int choose;
int flag=0;
SetBaseDisk( &base_num);
printf("\n\n2.最短寻道时间优先 SSTF 磁盘调度算法");
printf("\n\n3.电梯算法磁盘调度算法");
printf("\n\n4.退出.");
printf("\n\n/*******************************/");
printf("\n\n 请输入您的选择:(1 , 2 , 3 ,4 )");
key=temp;
while(key!=request_num){
i++;
step=R[key+1]-R[key];
count+=step;
printf("\n %d
%d---->%d
%d",i,R[key],R[key+1], step);
key++;
}
}
else{
/*当前磁头在尾*/
i++;
temp=key-1-down;
void SetRequestDisk(int *R,int *request_num,int base_num){/*设置请求访问磁道数*/ int i; srand(time(NULL)); printf("\ninput number of request disk(1~%d)",DISKTOTAL); scanf("%d",request_num); if(*request_num<=0||*request_num>DISKTOTAL){printf("ERROR!");exit(0);} printf("\nSystem creates stochastic numbers for request disk :\n"); for(i=0;i<*request_num;i++){ R[i]=rand()%(base_num-1);//生成随机数 printf(" %d",R[i]); } } void SetPresentDisk(int *present_disk,int *base_num){/*设置当前磁道*/ printf("\ninput present disk number(0~%d):",*base_num-1); scanf("%d",present_disk); if(*present_disk<0||*present_disk>=*base_num){printf("ERROR!");exit(0);} }
%d---->%d
%d",i+2,R[i],R[i+1], step);
printf("\nTotal moving steps:%d",count);
/*输出磁头总移动次数*/
printf("\nAverage moving steps:%d",count/request_num);
动次数*/
}
/*平均移
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<math.h>
#define DISKMAX 1000 #define DISKTOTAL 1000 #define OK 1 #define ERROR -1
int cmp ( const void *a , const void *b ) /**/ { return *(int *)a - *(int *)b; }
int SearchPresent(int *R,int request_num ,int present_disk){/*二分查找法,寻找当前磁道*/ int left,right,middle; left=0; right=request_num; while(left<=right){ middle=(left+right)/2; if(present_disk==R[middle])return middle; if(present_disk>R[middle])left=middle+1; else right=middle-1; } return -1; }
key=temp;
}
if(key==0){
/*当前磁头在头*/
i++;
temp=key+1+up;
/*记录下一个将要访问的磁道*/
step=abs(R[key]-R[temp]);
count+=step;
printf("\n %d
%d---->%d
%d",i,R[key],R[temp], step);
count+=step;
printf("\n %d
%d---->%d
%d",i,R[key],R[key-1], step);
key--;
}
}
printf("\nTotal moving steps:%d",count);
printf("\nAverage moving steps:%d",count/request_num);
temp=key+1+up;
/*记录下一个磁道*/
up_step++;
up=0;
down=up_step+down_step;
}
step=abs(R[key]-R[temp]);
count+=step;
printf("\n %d
%d---->%d
%d",i,R[key],R[temp], step);
int Sort(int *R,int request_num,int present_disk){/*调用库函数的快速排序函数*/ R[request_num]=present_disk;//加入当前磁道 qsort(R,request_num+1,sizeof(R[0]),cmp); return 1; }
南通大学计算机科学与技术学院
操作系统
驱动调度实验报告
班 级___________计 091___________
姓 名__________ _ _______ _
学 号____________ _
指导教师
戴树贵
一、实验内容
模拟电梯调度算法、先来先服务算法、最短查找时间算法,实现对磁盘的驱 动调度。
/*最短寻道时间优先算法*/ void SSTF(int *R,int present_disk,int request_num) {int i=0; int temp,key; int count=0,step=0; int up=0,down=0; int up_step=0,down_step=0;
/*记录下一个将要访问的磁道*/
step=abs(R[key]-R[temp]);
count+=step;
printf("\n %d
%d---->%d
%d",i,R[key],R[temp], step);
key=temp;
while(key!=0){
i++;
step=R[key]-R[key-1];
step=abs(present_disk-R[0]);
count+=step;
printf("\n
1
/*输出访问轨迹*/
for(i=0;i<request_num-1;i++){
/*绝对值*/ %d---->%d
%d",present_disk,R[0], step); /*遍历数组*/
step=abs(R[i]-R[i+1]); count+=step; printf("\n %d }
/*先到先服务磁盘调度*/
void FCFS(int *R,int present_disk,int request_num){
int i;
int count=0;
//磁头移动总次数
int step;
//访问下一个磁道磁头移动次数
printf("\nMoving Order Moving Path Moving Steps");
二、实验目的
1. 进一步理解磁盘调度算法的相关内容 2. 了解磁盘寻址的方法 3. 通过编程掌握磁盘调度算法的运行方式
三、实验环境
Microsoft Visual Basic 6.0 中文版
四、实验方法内容
1.实验方法 1)使用流程图描述演示程序的设计思想; 2)选取 c/c++、Java 等计算机语言,编程调试,最终给出运行正确的程序。 算法