磁盘寻道算法FCFS与SSTF

#include <stdio.h>
#include <stdlib.h>
int tracks[200];//track number

void Init(int start,int num);//initialize track sequence
void FCFS(int start,int num);//First come first serve algorithm
void SSTF(int start,int num);//Shortest seek time first algorithm

void Init(int start,int num)
{
	int i;
	printf("----------Input Track Number---------\n");
	for(i=0;i<num;i++)
	{
		printf("Please enter track number %d : ",i+1);
		scanf("%d",&tracks[i]);
	}

}
void FCFS(int start,int num)
{
	double sum=0;
	int temp,i;
	printf("==========================\n");
	printf("Starting from track number %d\n",start);
	printf("Next track number:\t seek length:\n");
	for(i=0;i<num;i++)
	{
		temp=abs(start-tracks[i]);
		printf("%d\t\t\t %d\n",tracks[i],temp);
		sum+=temp;
		start=tracks[i];
	}
	printf("Average seek length:%.3lf\n",sum/num);
	printf("==========================\n");
}
void SSTF(int start,int num)
{
	int i,j,temp,min,mint;
	bool table[100];
	double sum=0;
	for(i=0;i<num;i++)
	{
		table[i]=0;
	}
	printf("==========================\n");
	printf("Starting from track number %d\n",start);
	printf("Next track number:\t seek length:\n");
	for(i=0;i<num;i++)
	{
		min=-1;
		for(j=0;j<num;j++)
		{
			if(!table[j])
			{
				temp=abs(start-tracks[j]);
				if(min==-1||min>temp)
				{
					min=temp;
					mint=j;
				}
			}
		}
		printf("%d\t\t\t %d\n",tracks[mint],min);
		start=tracks[mint];
		table[mint]=1;
		sum+=min;
	}
	printf("Average seek length:%.3lf\n",sum/num);
	printf("==========================\n");
}



int main()
{
	int start;//Initial track number
	int num;//visit times
	int choose;
	printf("\t\t\t Disk Scheduling Algorithms\n");


	do
	{
		printf("Please enter the initial track number(0~200):");
		scanf("%d",&start);
	}while(start<0||start>200);
	do
	{
		printf("Please enter the number of disk accesses(1~10):");
		scanf("%d",&num);
	}while(num<1||num>10);
	
	Init(start,num);
	do
	{
		do
		{
			printf("1.FCFS\n");
			printf("2.SSTF\n");
			printf("3.exit\n");
			printf("Please select:");
			scanf("%d",&choose);
		}while(choose<1||choose>5);
		switch(choose)
		{
			case 1:
				FCFS(start,num);
				break;
			case 2:
				SSTF(start,num);
				break;
			case 3: exit(0);
		}
	}while(choose!=5);
	return 0;
}



  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值