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

标签 java c++ c operating-system

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

#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();
}

最佳答案

您可以向硬盘发送命令。该协议(protocol)取决于您的硬盘驱动器。

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

我建议你研究一下SATA、PATA和ATAPI协议(protocol)。另请搜索“Petzold 编写设备驱动程序”。

关于java - 使用c、c++或java实现扫描光盘调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30248950/

相关文章:

java - PaintComponent 未被调用

java - 使用多个参数过滤数据的最有效方法?

c++ - 支持小数点和小数逗号的字符串到 float 的转换

c - 指向给出不兼容指针类型警告的数组的指针

java - 在swing组件中添加flash

java - 如何使用 JPA 检查记录是否存在

c++ - NULL 是否在 C++11 中定义为 nullptr?

c++ - 在类型特征中,为什么人们使用 enum 而不是 static const 作为值?

c - 在 yacc 中返回指针时出现段错误

c - 一次又一次分配相同的内存空间