操作系统——调度算法FCFS、SSTF、SCAN、C-SCAN(C++实现)
首先,指出一个已经知道的BUG,就是排序用了地址传递使得,先运行SSTF或SCAN或C-SCAN后会影响FCFS
所以要得到正确的FCFS的运行结果,必须第一次运行就用FCFS。注意需要在cpp源文件目录里新建一个cidao.txt文件,然后复制粘贴下面这一段:
98 138 37 122 14 124 65 67
这里给出代码:
#include"stdio.h"
#include"stdlib.h"
#include<iostream>
#define maxsize 100
using namespace std;
void FCFS(int array[], int m)
{
int sum = 0, now;
cout << "请输入当前磁道号:";
cin >> now;
for (int i = 0; i < m; i++)
{
cout << array[i] << " ";
}
sum += abs(now - array[0]);
for (int i = 0, j = 1; j < m; i++, j++)
{
sum += abs(array[j] - array[i]);
}
cout << "移动的总道数:" << sum << endl;
}
void ShortPath(int array[], int m)
{
int temp;
int k = 1;
int now, l, r;
int i, j, sum = 0;
for(i=0;i<m;i++)
for (j = i + 1; j < m; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
for (i = 0; i < m; i++)
{
cout << array[i] << " ";
}
cout << "请输入当前的磁道号:";
cin >> now;
if (array[m - 1] <= now)
{
for (i = m; i >= 0; i--)
cout << array[i] << ' ';
sum = now - array[0];
}
else
if (array[0] >= now)
{
for (i = 0; i < m; i++)
cout << array[i] << ' ';
sum = array[m - 1] - now;
}
else
{
while (array[k] < now)
{
k++;
}
l = k - 1;
r = k;
while ((l >= 0) && (r < m))
{
if ((now - array[l]) <= (array[r] - now))
{
cout << array[l] << " ";
sum += now - array[l];
now = array[l];
l = l - 1;
}
else
{
cout << array[r] << ' ';
sum += array[r] - now;
now = array[r];
r = r + 1;
}
}
if (l = -1)
{
for (j = r; j < m; j++)
{
cout << array[j] << ' ';
}
sum += array[m - 1] - array[0];
}
else
{
for (j = i; j >= 0; j--)
{
cout << array[j] << ' ';
}
sum += array[m - 1] - array[0];
}
}
cout << "移动的总道数:" << sum << endl;
}
void Elevator(int array[], int m)
{
int temp;
int k = 1;
int now, l, r, d;
int i, j, sum = 0;
for(i=0;i<m;i++)
for (j = i + 1; j < m; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
for (i = 0; i < m; i++)
{
cout << array[i] << ' ';
}
cout << "请输入当前的磁道号:";
cin >> now;
if (array[m - 1] <= now)
{
for (i = m - 1; i < m; i++)
cout << array[i] << ' ';
sum = array[m - 1] - now;
}
else
{
while (array[k] < now)
{
k++;
}
l = k - 1;
r = k;
cout << "请输入当前移动臂的移动的方向(1表示向内,0表示向外):";
cin >> d;
if (d == 0)
{
for (j = l; j >= 0; j--)
{
cout << array[j] << ' ';
}
for (j = r; j < m; j++)
{
cout << array[j] << ' ';
}
sum = now - 2 * array[0] + array[m - 1];
}
else
{
for (j = r; j < m; j++)
{
cout << array[j] << ' ';
}
for (j = l; j >= 0; j--)
{
cout << array[j] << ' ';
}
sum = -now - array[0] + 2 * array[m - 1];
}
}
cout << "移动的总道数:" << sum << endl;
}
void CSCAN(int array[], int m)
{
int temp;
int k = 1;
int now, l, r, d;
int i, j, sum = 0;
for (i = 0; i < m; i++)
for (j = i + 1; j < m; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
for (i = 0; i < m; i++)
{
cout << array[i] << ' ';
}
cout << "请输入当前的磁道号:";
cin >> now;
if (array[m - 1] <= now)
{
for (i = m - 1; i < m; i++)
cout << array[i] << ' ';
sum = array[m - 1] - now;
}
else
{
while (array[k] < now)
{
k++;
}
l = k - 1;
r = k;
cout << "请输入当前移动臂的移动的方向(1表示向内,0表示向外):";
cin >> d;
if (d == 0)
{
for (j = l; j >= 0; j--)
{
cout << array[j] << ' ';
}
for (j = m - 1; j >=k; j--)
{
cout << array[j] << ' ';
}
sum = now - 2 * array[0] + array[m - 1];
}
else
{
for (j = r; j < m; j++)
{
cout << array[j] << ' ';
}
for (j = 0; j < k; j++)
{
cout << array[j] << ' ';
}
sum = -now - array[0] + 2 * array[m - 1];
}
}
cout << "移动的总道数:" << sum << endl;
}
int main(void)
{
int c;
FILE *fp;
int cidao[maxsize];
int i = 0, count;
fp = fopen("cidao.txt", "r+");
if (fp == NULL)
{
cout << "文件打不开!" << endl;
exit(0);
}
while (!feof(fp))
{
fscanf(fp, "%d", &cidao[i]);
i++;
}
count = i;
for (i = 0; i < count; i++)
{
printf("%5d", cidao[i]);
}
cout << endl;
while (true)
{
cout << endl << "系统菜单如下:" << endl;
printf("1.先来先服务 2.最短寻道时间优先 3.电梯调度 4.CSCAN");
cout << endl;
cout << "请选择:";
cin >> c;
if (c > 4)break;
switch (c)
{
case 1:
FCFS(cidao, count);
break;
case 2:
ShortPath(cidao, count);
break;
case 3:
Elevator(cidao, count);
break;
case 4:
CSCAN(cidao, count);
break;
}
}
system("pause");
return 0;
}
END

浙公网安备 33010602011771号