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

本文主要是介绍成都工业学院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到10int 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、由于不同编译器可能不兼容,所以本人把代码都写在一起,避免了分文件造成的错误

这篇关于成都工业学院2021级操作系统专周课程设计FCFS,SSTF,SCAN,LOOK算法的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/500775

相关文章

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

PyCharm接入DeepSeek实现AI编程的操作流程

《PyCharm接入DeepSeek实现AI编程的操作流程》DeepSeek是一家专注于人工智能技术研发的公司,致力于开发高性能、低成本的AI模型,接下来,我们把DeepSeek接入到PyCharm中... 目录引言效果演示创建API key在PyCharm中下载Continue插件配置Continue引言

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

基于Go语言实现一个压测工具

《基于Go语言实现一个压测工具》这篇文章主要为大家详细介绍了基于Go语言实现一个简单的压测工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录整体架构通用数据处理模块Http请求响应数据处理Curl参数解析处理客户端模块Http客户端处理Grpc客户端处理Websocket客户端

Java CompletableFuture如何实现超时功能

《JavaCompletableFuture如何实现超时功能》:本文主要介绍实现超时功能的基本思路以及CompletableFuture(之后简称CF)是如何通过代码实现超时功能的,需要的... 目录基本思路CompletableFuture 的实现1. 基本实现流程2. 静态条件分析3. 内存泄露 bug