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

时间:2015-05-14 23:08:58

标签: java c++ c operating-system

学习系统的编程,如何使用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("---------------------------------------------------------- \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();
}

1 个答案:

答案 0 :(得分:2)

您可以将命令发送到硬盘驱动器。协议取决于您的硬盘驱动器。

检查出来:
http://www.ata-atapi.com/pata.html
http://www.t13.org/documents/uploadeddocuments/docs2006/d1699r3f-ata8-acs.pdf

我建议您研究SATA,PATA和ATAPI协议。还要搜索&#34; Petzold写设备驱动程序&#34;。