使用c、c++或java实现扫描磁盘调度
Implementing scan disc scheduling using c, c++ or java
学习系统的编程,如何使用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"
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 如何正确实现和访问运算符的各种自定义枚举器
- 检查不带转换的扫描格式
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 用于AVX的ln(x)的实现,m256
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用c、c++或java实现扫描磁盘调度