Operating system experiment report-disk scheduling algorithm

Operating system experiment report-disk scheduling algorithm

1. Experiment name: Implementation of disk scheduling algorithm
2. Experimental requirements:
(1) Understand the concept of disk scheduling,
(2) Master the three algorithms of disk scheduling;
(3) Use C or C++ language to program the algorithm.
3. Experimental method: Debug and run the program through the computer.
4. Experimental environment:
(1) Hardware environment:
(2) Software environment: Windows 10 operating system, C or C++ programming language.
5. Experimental process:
(1) Experimental description:
The typical representative of shared equipment is the disk. The address of the physical block of the disk is specified by the cylinder number, head number, and sector number. There are three stages to complete the access to a certain physical block of the disk. : Seek time Ts, rotation delay time Tw and read and write time Trw.
The seek time Ts is the time required for the head to move from the current track to the target track; the rotation delay time Tw is the time for the target physical block to rotate from the current position to the head position after the head stays on the target track; the read and write time Trw is the target The time for the content of the physical block to be exchanged with the corresponding memory. The principle of disk scheduling is fairness and high throughput. The measurement indicators are access time T and average access time Ta:
T=Ts+Tw+Trw
Ta=Tsa+Twa+Trwa Seek
time and rotation delay time become the main considerations of the scheduling algorithm factor. To reduce access time is to reduce seek time and rotation delay time.
FCFS algorithm: scheduling according to the order in which the process requests to access the disk;
SSTF algorithm: the track to be accessed is required to be the closest to the track where the current head is located, so that the seek time is the shortest;
SCAN algorithm: not only considers the track to be accessed and The current distance between the tracks, the more priority is given to the current moving direction of the head.
(2) Source code:

#include<stdio.h>
int num,sum,kai,max;
int m=0;
int n=0;
int s[100];
int s1[100];
int c1[50];
int c2[50]; 
void creat(){
    
    
  printf("请输入从哪个磁道开始:");
  scanf("%d",&kai);
  printf("请输入最长磁道号:"); 
  scanf("%d",&max); 
  printf("请输入磁道的个数:");
  scanf("%d",&num);
  for(int j=0;j<num;j++){
    
    
    printf("请输入第%d个磁道:",j+1);
    scanf("%d",&s[j]);
    if(s[j]>max) {
    
    
		printf("ERROR\n");
		break;
	}
    for(int i=0;i<j;i++)
        if(s[j]==s[i])
            j--;
} 
printf("被访问的下一个磁道\n");
for(int i=0;i<num;i++){
    
    
    printf("\t%d\t\n",s[i]);
}
int su=kai;
int t;
for(int i=0;i<num;i++)
   if(su>s[i])
      c1[m++]=s[i];
else
      c2[n++]=s[i];
  for(int i=0;i<m;i++)
     for(int j=i;j<m;j++)
         if(c1[i]<c1[j])
            {
    
    t=c1[i];c1[i]=c1[j];c1[j]=t;}
     for(int i=0;i<n;i++)
        for(int j=i;j<n;j++)
            if(c2[i]>c2[j])
               {
    
    t=c2[i];c2[i]=c2[j];c2[j]=t;}
}
void FCFS(){
    
    
  printf("先来先服务 FCFS:\n");
  printf("被访问的下一个磁道\t\t\t磁道号移动距离:\n");
  int su=kai;
  sum=0;
  for(int i=0;i<num;i++){
    
     
if(su<s[i])
         s1[i]=s[i]-su;
      else
         s1[i]=su-s[i];
      su=s[i];
      sum+=s1[i];
  }
for(int i=0;i<num;i++){
    
    
         printf("\t%d\t\t\t\t\t%d\t\t\n",s[i],s1[i]);
  }
  printf("寻道长度:%d\n",sum);
}
void SSTF(){
    
    
      printf("最短寻道 SSTF:\n");
      printf("被访问的下一个磁道\t\t\t磁道号移动距离\n");
      int su=kai;
      int s2[100];
      sum=0;
      for(int i=0;i<m;i++)
         s2[i]=c1[i];
      for(int i=0;i<n;i++)
         s2[i+m]=c2[i];
      for(int i=0;i<num;i++){
    
     
if(su<s2[i])
           s1[i]=s2[i]-su;
        else
           s1[i]=su-s2[i];
        su=s2[i];
        sum+=s1[i];
      }
      for(int i=0;i<num;i++){
    
    
         printf("\t%d\t\t\t\t\t%d\t\t\n",s2[i],s1[i]);
      }
      printf("寻道长度:%d\n",sum);
}
void SCAN(){
    
    
     printf("扫描算法 SCAN:\n");
     printf("被访问的下一个磁道:\t\t\t磁道号移动距离:\n");
     int su=kai;
     int s2[100];
     sum=0;
     for(int i=0;i<n;i++)
        s2[i]=c2[i];
     for(int i=0;i<m;i++)
        s2[i+n]=c1[i];
     for(int i=0;i<num;i++){
    
    
 if(su<s2[i])
             s1[i]=s2[i]-su;
         else
             s1[i]=su-s2[i];
         su=s2[i];
         sum+=s1[i];
     }
     for(int i=0;i<num;i++){
    
    
         printf("\t%d\t\t\t\t\t%d\t\t\n",s2[i],s1[i]);
     }
     printf("寻道长度:%d\n",sum);
} 
void MENU(){
    
    
printf("磁盘调度\n");
printf("------------------------------------------\n");
printf("              1.创建磁道\n");
printf("              2.先来先服务 FCFS\n");
printf("              3.最短寻道 SSTF\n");
printf("              4.扫描算法 SCAN\n");
printf("              5.退出 EXIT\n");
printf("------------------------------------------\n");
int menuchoice;
scanf("%d",&menuchoice);
if(menuchoice!=1&&menuchoice!=6){
    
    
printf("请先创建磁道\n");
}
if(menuchoice==6){
    
    
printf("谢谢使用!");
}
else{
    
    
creat();
printf("磁盘调度\n");
P:printf("------------------------------------------\n");
printf("              1.创建磁道\n");
printf("              2.先来先服务 FCFS\n");
printf("              3.最短寻道 SSTF\n");
printf("              4.扫描算法 SCAN\n");
printf("              5.退出 EXIT\n");
printf("------------------------------------------\n");
scanf("%d",&menuchoice);
 		if(menuchoice>6||menuchoice<1){
    
    
printf("没看到菜单只有1-5吗?搞事情啊!!!\n");
 		goto P;
}
switch(menuchoice){
    
    
case 2:
FCFS();
  			goto P;
case 3:
 			SSTF();
goto P;
case 4:
SCAN();
goto P;
case 5:
printf("谢谢使用!");
break;
}
}
}
int main(){
    
    
MENU();
return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44145894/article/details/106708822