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

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

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

#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("---------------------------------------------------------- \n\n");
 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(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn;
 }while(nposn!=cyposn[req]);
 printf("---------------------------------------------------------- \n\n");
 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".