磁盘寻道算法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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值