成都工业学院2021级操作系统专周课程设计FCFS,SSTF,SCAN,LOOK算法的实现

运行环境

操作系统:Windows 11 家庭版

运行软件:CLion 2023.2.2

源代码文件

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
using namespace std;

// 生成随机数
int generateRandomNumber(int min, int max) {
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<> dis(min, max);
    return dis(gen);
}

// 计算引臂移动量
int calculateArmMovement(const vector<int>& movementSequence) {
    int movement = 0;
    for (int i = 1; i < movementSequence.size(); ++i) {
        movement += abs(movementSequence[i] - movementSequence[i-1]);
    }
    return movement;
}

// 计算寻道时间
int calculateSeekTime(int armMovement, int timePerTrack) {
    return armMovement * timePerTrack;
}

// 计算平均旋转延迟时间
int calculateRotationDelay(int armMovement, int diskSpeed) {
    return (armMovement * 60000) / diskSpeed; // 因转速为转/分钟,转成毫秒需要乘以60000
}

// 计算传输时间
int calculateTransferTime(int numRequests, int sectorsPerTrack, int sectorSize, int diskSpeed) {
    int transferTime = (numRequests * sectorsPerTrack * sectorSize * 1000) / diskSpeed; // 字节数除以转速得到毫秒数
    return transferTime;
}

// 计算总处理时间
int calculateTotalProcessingTime(int seekTime, int rotationDelay, int transferTime) {
    return seekTime + rotationDelay + transferTime;
}

// 显示引臂移动序列
void displayArmMovementSequence(const vector<int>& movementSequence) {
    for (int i = 0; i < movementSequence.size(); ++i) {
        cout << movementSequence[i] << " ";
    }
    cout << endl;
}

// SSTF算法
void sstfAlgorithm(vector<int>& ioRequests, int currentTrack, int timePerTrack, int diskSpeed, int sectorsPerTrack, int sectorSize) {
    cout << "SSTF算法:" << endl;
    vector<int> armMovementSequence;
    armMovementSequence.push_back(currentTrack); // 先添加当前磁道到移动序列

    while (!ioRequests.empty()) {
        int minDistance = INT_MAX;
        int nextTrack = -1;

        for (int i = 0; i < ioRequests.size(); ++i) {
            int distance = abs(currentTrack - ioRequests[i]);
            if (distance < minDistance) {
                minDistance = distance;
                nextTrack = ioRequests[i];
            }
        }

        armMovementSequence.push_back(nextTrack);
        currentTrack = nextTrack;
        ioRequests.erase(find(ioRequests.begin(), ioRequests.end(), nextTrack));
    }

    displayArmMovementSequence(armMovementSequence);
    int armMovement = calculateArmMovement(armMovementSequence);
    int seekTime = calculateSeekTime(armMovement, timePerTrack);
    int rotationDelay = calculateRotationDelay(armMovement, diskSpeed);
    int numRequests = ioRequests.size();
    int transferTime = calculateTransferTime(numRequests, sectorsPerTrack, sectorSize, diskSpeed);
    int totalProcessingTime = calculateTotalProcessingTime(seekTime, rotationDelay, transferTime);

    cout << "引臂移动量: " << armMovement << endl;
    cout << "寻道时间: " << seekTime << " 毫秒" << endl;
    cout << "平均旋转延迟时间: " << rotationDelay << " 毫秒" << endl;
    cout << "传输时间: " << transferTime << " 毫秒" << endl;
    cout << "所有访问处理时间: " << totalProcessingTime << " 毫秒" << endl;
}

//SCAN算法
void scanAlgorithm(vector<int>& ioRequests, int currentTrack, int timePerTrack, int diskSpeed, int sectorsPerTrack, int sectorSize) {
    cout << "SCAN算法:" << endl;
    vector<int> scanArmMovementSequence;
    int maxTrack = *max_element(ioRequests.begin(), ioRequests.end());
    int minTrack = *min_element(ioRequests.begin(), ioRequests.end());
    scanArmMovementSequence.push_back(currentTrack);

    vector<int> tempStack;
    vector<bool> visitedTracks(200, false); // 初始化标记数组,200是磁道的数量

    if (currentTrack >= maxTrack) {
        // 先向内扫描
        tempStack.push_back(0); // 添加0进入栈
        visitedTracks[0] = true;

        for (int track = currentTrack - 1; track >= minTrack; --track) {
            if (find(ioRequests.begin(), ioRequests.end(), track) != ioRequests.end() && !visitedTracks[track]) {
                tempStack.push_back(track);
                visitedTracks[track] = true;
            }
        }

        sort(tempStack.begin(), tempStack.end()); // 对栈进行排序

        // 将栈中的磁道添加到移动序列
        for (int track : tempStack) {
            scanArmMovementSequence.push_back(track);
        }

        // 到达最小磁道号后折返,向外扫描
        for (int track = minTrack + 1; track <= maxTrack; ++track) {
            if (find(ioRequests.begin(), ioRequests.end(), track) != ioRequests.end() && !visitedTracks[track]) {
                scanArmMovementSequence.push_back(track);
                visitedTracks[track] = true;
            }
        }
    } else {
        // 先向外扫描
        tempStack.push_back(199); // 添加199进入栈
        visitedTracks[199] = true;

        for (int track = currentTrack + 1; track <= maxTrack; ++track) {
            if (find(ioRequests.begin(), ioRequests.end(), track) != ioRequests.end() && !visitedTracks[track]) {
                tempStack.push_back(track);
                visitedTracks[track] = true;
            }
        }

        sort(tempStack.begin(), tempStack.end()); // 对栈进行排序

        // 将栈中的磁道添加到移动序列
        for (int track : tempStack) {
            scanArmMovementSequence.push_back(track);
        }

        // 到达最大磁道号后折返,向内扫描
        for (int track = maxTrack - 1; track >= minTrack; --track) {
            if (find(ioRequests.begin(), ioRequests.end(), track) != ioRequests.end() && !visitedTracks[track]) {
                scanArmMovementSequence.push_back(track);
                visitedTracks[track] = true;
            }
        }
    }

    displayArmMovementSequence(scanArmMovementSequence);
    int scanArmMovement = calculateArmMovement(scanArmMovementSequence);
    int scanSeekTime = calculateSeekTime(scanArmMovement, timePerTrack);
    int scanRotationDelay = calculateRotationDelay(scanArmMovement, diskSpeed);
    int scanNumRequests = ioRequests.size();
    int scanTransferTime = calculateTransferTime(scanNumRequests, sectorsPerTrack, sectorSize, diskSpeed);
    int scanTotalProcessingTime = calculateTotalProcessingTime(scanSeekTime, scanRotationDelay, scanTransferTime);

    cout << "引臂移动量: " << scanArmMovement << endl;
    cout << "寻道时间: " << scanSeekTime << " 毫秒" << endl;
    cout << "平均旋转延迟时间: " << scanRotationDelay << " 毫秒" << endl;
    cout << "传输时间: " << scanTransferTime << " 毫秒" << endl;
    cout << "所有访问处理时间: " << scanTotalProcessingTime << " 毫秒" << endl;
    // 在最后释放visitedTracks的空间
    visitedTracks.clear();
    displayArmMovementSequence(scanArmMovementSequence);
}

// LOOK算法
void lookAlgorithm(vector<int>& ioRequests, int currentTrack, string direction, int timePerTrack, int diskSpeed, int sectorsPerTrack, int sectorSize) {
    cout << "LOOK算法:" << endl;
    vector<int> armMovementSequence;
    int maxTrack = *max_element(ioRequests.begin(), ioRequests.end());
    int minTrack = *min_element(ioRequests.begin(), ioRequests.end());
    armMovementSequence.push_back(currentTrack); // 先添加当前磁道到移动序列

    if (direction == "outward") {
        // 向外扫描
        for (int track =  currentTrack + 1; track <= maxTrack; ++track) {
            if (find(ioRequests.begin(), ioRequests.end(), track) != ioRequests.end()) {
                armMovementSequence.push_back(track);
            }
        }
        // 向内扫描
        for (int track = currentTrack - 1; track >= minTrack; --track) {
            if (find(ioRequests.begin(), ioRequests.end(), track) != ioRequests.end()) {
                armMovementSequence.push_back(track);
            }
        }
    } else {
        // 向内扫描
        for (int track = currentTrack - 1; track >= minTrack; --track) {
            if (find(ioRequests.begin(), ioRequests.end(), track) != ioRequests.end()) {
                armMovementSequence.push_back(track);
            }
        }
        // 向外扫描
        for (int track = currentTrack + 1; track <= maxTrack; ++track) {
            if (find(ioRequests.begin(), ioRequests.end(), track) != ioRequests.end()) {
                armMovementSequence.push_back(track);
            }
        }
    }

    displayArmMovementSequence(armMovementSequence);
    int armMovement = calculateArmMovement(armMovementSequence);
    int seekTime = calculateSeekTime(armMovement, timePerTrack);
    int rotationDelay = calculateRotationDelay(armMovement, diskSpeed);
    int numRequests = ioRequests.size();
    int transferTime = calculateTransferTime(numRequests, sectorsPerTrack, sectorSize, diskSpeed);
    int totalProcessingTime = calculateTotalProcessingTime(seekTime, rotationDelay, transferTime);

    cout << "引臂移动量: " << armMovement << endl;
    cout << "寻道时间: " << seekTime << " 毫秒" << endl;
    cout << "平均旋转延迟时间: " << rotationDelay << " 毫秒" << endl;
    cout << "传输时间: " << transferTime << " 毫秒" << endl;
    cout << "所有访问处理时间: " << totalProcessingTime << " 毫秒" << endl;
}

// 根据选择的调度算法进行处理
void processAlgorithm(vector<int>& ioRequests, int currentTrack, int timePerTrack, int startupTime, int diskSpeed, int sectorsPerTrack, int sectorSize, const string& algorithmName) {
    vector<int> armMovementSequence;
    if (algorithmName == "FCFS") {
        armMovementSequence = ioRequests;  // 直接按照顺序处理请求
    } else if (algorithmName == "SSTF") {
        sstfAlgorithm(ioRequests, currentTrack, timePerTrack, diskSpeed, sectorsPerTrack, sectorSize);
        return;
    } else if (algorithmName == "SCAN") {
        scanAlgorithm(ioRequests, currentTrack, timePerTrack, diskSpeed, sectorsPerTrack, sectorSize);
        return;
    } else if (algorithmName == "LOOK") {
        lookAlgorithm(ioRequests, currentTrack, "outward", timePerTrack, diskSpeed, sectorsPerTrack, sectorSize);
        return;
    } else {
        cout << "未知的调度算法:" << algorithmName << endl;
        return;
    }

    armMovementSequence.insert(armMovementSequence.begin(), currentTrack);  // 加入初始位置
    displayArmMovementSequence(armMovementSequence);

    int armMovement = calculateArmMovement(armMovementSequence);
    int seekTime = calculateSeekTime(armMovement, timePerTrack);
    int rotationDelay = calculateRotationDelay(armMovement, diskSpeed);
    int numRequests = ioRequests.size();
    int transferTime = calculateTransferTime(numRequests, sectorsPerTrack, sectorSize, diskSpeed);
    int totalProcessingTime = calculateTotalProcessingTime(seekTime, rotationDelay, transferTime);

    cout << "引臂移动量: " << armMovement << endl;
    cout << "寻道时间: " << seekTime << " 毫秒" << endl;
    cout << "平均旋转延迟时间: " << rotationDelay << " 毫秒" << endl;
    cout << "传输时间: " << transferTime << " 毫秒" << endl;
    cout << "所有访问处理时间: " << totalProcessingTime << " 毫秒" << endl;
}

int main() {
    int initialTrack; // 磁头初始位置
    cout << "请输入磁头初始位置:";
    cin >> initialTrack;
    int timePerTrack;  // 跨越1个磁道所用时间(毫秒)
    int startupTime;   // 启动时间(毫秒)
    int diskSpeed;     // 磁盘转速(转/分钟)
    int sectorsPerTrack;  // 每磁道扇区数
    int sectorSize;    // 每扇区字节数

    cout << "请输入跨越1个磁道所用时间(毫秒):";
    cin >> timePerTrack;
    cout << "请输入启动时间(毫秒):";
    cin >> startupTime;
    cout << "请输入磁盘转速(转/分钟):";
    cin >> diskSpeed;
    cout << "请输入每磁道扇区数:";
    cin >> sectorsPerTrack;
    cout << "请输入每扇区字节数:";
    cin >> sectorSize;

    vector<int> ioRequests;
    vector<int> diskTrackNumbers;
    for(int i=1; i<201; i++){
        diskTrackNumbers.push_back(i);
    } // 磁道号固定为0到10
    int currentTrack = initialTrack; // 修改为用户输入的初始位置
    string direction = (generateRandomNumber(0, 1) == 0) ? "outward" : "inward"; // 添加这一行以初始化方向

    // 生成随机磁道I/O请求序列
    cout << "生成的随机磁道I/O请求序列:" << endl;
    for (int i = 0; i < 6; ++i) {
        int track = generateRandomNumber(0, diskTrackNumbers.size() - 1);
        ioRequests.push_back(diskTrackNumbers[track]);
        cout << ioRequests[i] << " ";
    }
    cout << endl;

    // 选择调度算法
    string algorithmName;
    cout << "请选择调度算法(FCFS、SSTF、SCAN、LOOK):";
    cin >> algorithmName;

    // 处理IO请求
    processAlgorithm(ioRequests, currentTrack, timePerTrack, startupTime, diskSpeed, sectorsPerTrack, sectorSize, algorithmName);

    return 0;
}

源代码示例

运行结果截图

FCFS算法

SSTF算法

SCAN算法

LOOK算法

注意事项

1、算法可能有点问题,大多数情况下是没有问题的

2、由于不同编译器可能不兼容,所以本人把代码都写在一起,避免了分文件造成的错误

相关推荐
武昌库里写JAVA14 小时前
使用React Strict DOM改善React生态系统
数据结构·vue.js·spring boot·算法·课程设计
小万编程14 小时前
【2025最新计算机毕业设计】新型吃住玩一体化旅游管理系统【提供源码+答辩PPT+文档+项目部署】
课程设计·旅游
嗨小陈14 小时前
旅游推荐系统设计与实现 计算机毕业设计 有源码 P10090
spring boot·课程设计·旅游·计算机毕业设计
苹果酱05671 天前
【react项目】从零搭建react项目[nodejs安装]
java·spring boot·毕业设计·layui·课程设计
小馒头学python2 天前
【期末大作业】使用Python熟练掌握面向对象
开发语言·python·课程设计
B站计算机毕业设计超人2 天前
计算机毕业设计hadoop+spark视频推荐系统 短视频推荐系统 视频流量预测系统 短视频爬虫 视频数据分析 视频可视化 视频大数据 大数据
大数据·人工智能·python·机器学习·课程设计·数据可视化·推荐算法
工业互联网专业3 天前
Python毕业设计选题:基于Python的社区爱心养老管理系统设计与实现_django
python·django·vue·毕业设计·源码·课程设计
红队it3 天前
【计算机毕设】基于Python预制菜可视化数据分析预测推荐系统(完整系统源码+数据库+详细部署教程)✅
数据库·python·数据挖掘·数据分析·课程设计
代码小鑫3 天前
【毕业设计】A079-基于Java的影院订票系统的设计与实现
java·开发语言·数据库·spring boot·后端·毕业设计·课程设计