使用c、c++或java实现扫描磁盘调度

Implementing scan disc scheduling using c, c++ or java

本文关键字:实现 扫描 磁盘 调度 java c++ 使用      更新时间:2023-10-16

学习系统的编程,如何使用C、c++或java实现扫描磁盘调度算法。这段代码需要实际访问磁盘句柄。下面是我一直在处理的代码示例,但问题是它仅仅是扫描磁盘算法运行时实际发生的模拟。头位置和输入数据只是我作为用户插入到程序中的值。我希望它能够实际读取当前标头位置,并将请求排队并实现扫描磁盘调度或任何其他调度算法

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 15
#define cymax 249
int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
int cyposn[max],temp;
void input()
{
 do
 {
  clreol();
  printf("n Enter the current header position : ");
  scanf("%d",&cposn);
  /*cposn current cylinder position which in this case is the same
  as the current header position*/
 }while(cposn>cymax || cposn <=0);
 printf("n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)     /*This for loop helps to store the different requests
 inputs in the array of cylinder positions cyposn: Note that the initial array
 cyposn stores the value of the initial header postion  and that is why the for
 loop begins with 1
 */
  scanf("%d",&cyposn[i]);

}
void SCAN()        /*function for the scanning schedule*/
{
int tmp = cp;  /*the tmp integer is used for swapping values, from the current
cylinder position to the next*/
 int ind = 0;
 for(i=0;i<=req;i++)/*this outer loops counts the number of requests*/
 {
  for(j=0;j<req-i;j++)   /*this inner loop walks through different values in the
  cylinder array which would later become sorted */
  {
   if(cyposn[j] > cyposn[j+1])/*compares the two values previous position
   and the next position, if the previous is greater than the next position, the
   positions are swapped, taking the next position tobecome the current position
   a situation which would always ensure that the current position will become
   as small as possible:
   HENCE THE HANDLE WILL MOVE TO THE LEFT
   */
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;  /*when  the loop finishes untill it finds the most minimal value:
 the handle is assigned to position '0' making the current position to be zero*/
 do
 {
  if(cyposn[cp] == cposn)
   break;    /*if it reaches the possible maximum cylinder value
   it breaks else it increments the values*/
  cp++;
 }while(cp!=req);

 printf("nS.No.  Current Position    Next Position   Displacement n");
 printf("---------------------------------------------------------- nn");
 i=0;
 cposn = cyposn[cp];
 do
 {
  if(ind == 0)
  {
   if(cp == 0)
   { nposn = 0; ind = 1; }
   else
    nposn = cyposn[--cp];
  }
  else
  {
   if(cp == 0)
    cp = tmp;
   nposn = cyposn[++cp];
  }

  printf(" %dtt%dtt%dtt%dn",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn;
 }while(nposn!=cyposn[req]);
 printf("---------------------------------------------------------- nn");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}
void main()
{
 do
 {
  clrscr();
  printf("n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 SCAN();
 getch();
}

可以向硬盘发送命令。协议取决于你的硬盘。

查看这些:
http://www.ata-atapi.com/pata.html
http://www.t13.org/documents/uploadeddocuments/docs2006/d1699r3f-ata8-acs.pdf

我建议你研究SATA, PATA和ATAPI协议。也可以搜索"Petzold writing device drivers"