天津理工大学操作系统实验3:磁盘调度算法的实现

实验报告

学院(系)名称:计算机与通信工程学院

姓名 班级

卢洪利 2014级4班 课程名称

学号 实验项目

操作系统

20116年12月 8 日 第3、4节 20116年12月12日 第7、8节 20116年12月15日 第3、4节 20116年12月19日 第7、8节

20146049

专业

计算机科学与技术

实验三:磁盘调度算法的实现

课程代码

0668036 软件实验室7-220 软件实验室7-220 软件实验室7-220

实验时间 实验地点

批改意见

成绩

教师签字:

实验环境:

●操作系统:Fedora 25 with Linux 4.8.13-300.fc25.x86_64 ●编程语言:Python 3.5.2

●开发环境:PyCharm 2016.3.1

实验内容:

1.本实验是模拟操作系统的磁盘寻道方式,运用磁盘访问顺序的不同来设计磁盘的调度算法。 2.实现的磁盘调度算法有FCFS,SSTF,SCAN,CSCAN和 NStepSCAN算法。

3.设定开始磁道号寻道范围,依据起始扫描磁道号和最大磁道号数,随机产生要进行寻道的磁道号序

列。

4.选择磁盘调度算法,显示该算法的磁道访问顺序,计算出移动的磁道总数和平均寻道总数。 5.按算法的寻道效率进行排序,并对各算法的性能进行分析比较。

实验要求:

1.详细描述实验设计思想、程序结构及各模块设计思路; 2.详细描述程序所用数据结构及算法; 3.明确给出测试用例和实验结果;

4.为增加程序可读性,在程序中进行适当注释说明;

5.认真进行实验总结,包括:设计中遇到的问题、解决方法与收获等; 6.实验报告撰写要求结构清晰、描述准确逻辑性强;

7.实验过程中,同学之间可以进行讨论互相提高,但绝对禁止抄袭。

设计思想:

设计一个类模拟磁头,给它分配运行方向,当前位置,程序的请求序列等属性,并附有各种寻道算法可以被公开调用,模拟的各种算法在执行时,会产生运行轨迹等数据,这些数据算做磁头的数据属性,随时使用随时清空。

FCFS算法按照请求序列依次满足程序的请求。 SSTF算法选择与当前位置最近的请求进行响应。

SCAN算法优先保证磁头的方向不发生改变的前提下,选择最近的请求进行响应。 CSCAN算法在SCAN算法的基础上,固定了磁头的移动方向。

NStepSCAN算法将请求分为多个队列,每个队列使用SCAN算法。一般将请求分成大于2的队列。 算法实现时,对于SCAN和CSCAN算法,先实现CSCAM算法,在其基础上,当磁头到达最接近边界的一个请求时,将磁头方向反转(代码中的reverse()函数实现这个功能)即可。

执行结果及测试用例:

进程的读磁盘请求: [129, 47, 31, 94, 153, 131, 106, 11, 182](所有算法使用同样的测试用例) ====================================================== FCFS算法 初始位置: 28

访问顺序: [129, 47, 31, 94, 153, 131, 106, 11, 182] 移动距离: [101, 82, 16, 63, 59, 22, 25, 95, 171] 移动总数: 634 平均寻道: 70.44

====================================================== SSTF算法

初始位置: 28

访问顺序: [31, 47, 11, 94, 106, 129, 131, 153, 182] 移动距离: [3, 16, 36, 83, 12, 23, 2, 22, 29] 移动总数: 226 平均寻道: 25.11

====================================================== SCAN算法 初始位置: 28

初始移动方向:由里向外

访问顺序: [31, 47, 94, 106, 129, 131, 153, 182, 11] 移动总数: 325 平均寻道: 36.11

====================================================== CSCAN算法 初始位置: 28

移动方向:由里向外

访问顺序: [31, 47, 94, 106, 129, 131, 153, 182, 11] 移动总数: 164 平均寻道: 18.22

程序源代码:

# coding=utf-8 import random

class HEAD: """

magnetic head """

__location = -1 __next = -1 __request = []

# 大于0为由里向外,小于0 为由外向里 __direction = 0

# SCAN算法的磁道访问顺序 __scan_trace = []

# SSTF算法的磁道访问顺序 __sstf_trace = []

# FCFS算法的磁道访问顺序 __fcfs_trace = []

# 每次寻道的移动距离 __move_length = [] # 寻道的移动总距离 __sum_length = 0

def __init__(self):

self.__location = 1 self.__direction = 1

def __init__(self, location): self.__location = location if location > 100:

self.__direction = -1 elif location

self.__direction = 1

def __clear(self):

self.__sum_length = 0

self.__move_length.clear()

self.__sstf_trace.clear() self.__fcfs_trace.clear() self.__scan_trace.clear()

def get_location(self): return self.__location

def reverse(self):

if self.__direction == 1: self.__direction = -1 elif self.__direction == -1: self.__direction = 1

def request(self, rec):

self.__request.append(rec)

def show_request_list(self): print(self.__request)

def get_sstf_trace(self): return self.__sstf_trace

def get_fcfs_trace(self): return self.__fcfs_trace

def get_scan_trace(self): return self.__scan_trace

def get_move_length(self): return self.__move_length

def get_sum_length(self): return self.__sum_length

def get_direction(self): return self.__direction

def fcfs(self): self.__clear()

temp = self.__location

for rec in self.__request:

self.__move_length.append(abs(self.__location-rec)) self.__sum_length += abs(self.__location-rec) self.__location = rec

self.__fcfs_trace.append(self.__location) # 为了方便与其他算法做对比,将磁头复位 self.__location = temp

def sstf(self): self.__clear()

temp_loc = self.__location temp = self.__request.copy() while len(self.__request) != 0: min_length = 200 min_no = 0

for i in range(0, len(self.__request)): req = self.__request[i]

if abs(req - self.__location)

self.__location = self.__request[min_no] self.__sstf_trace.append(self.__location) del self.__request[min_no] self.__request = temp

# 为了方便与其他算法做对比,将磁头复位 self.__location = temp_loc

def cscan(self): self.__clear()

temp_request = self.__request.copy() temp_loc = self.__location

temp_direction = self.__direction while len(self.__request) != 0: min_length = 200 min_no = -1

if self.__direction == 1:

for i in range(0, len(self.__request)): req = self.__request[i]

if abs(req - self.__location) self.__location:

min_length = abs(req - self.__location) min_no = i if min_no == -1:

self.__location = 1 continue

elif self.__direction == -1:

for i in range(0, len(self.__request)): req = self.__request[i]

if abs(req - self.__location)

min_length = abs(req - self.__location) min_no = i if min_no == -1:

self.__location = 200 continue self.__move_length.append(abs(self.__location - self.__request[min_no])) self.__sum_length += abs(self.__location - self.__request[min_no])

self.__location = self.__request[min_no] self.__scan_trace.append(self.__location) del self.__request[min_no]

self.__request = temp_request self.__location = temp_loc

self.__direction = temp_direction

def scan(self): self.__clear()

temp_direction = self.__direction temp_request = self.__request.copy() temp_loc = self.__location

while len(self.__request) != 0: min_length = 200 min_no = -1

if self.__direction == 1:

for i in range(0, len(self.__request)): req = self.__request[i]

if req - self.__location self.__location:

min_length = abs(req - self.__location) min_no = i if min_no == -1: self.reverse() continue

elif self.__direction == -1:

for i in range(0, len(self.__request)): req = self.__request[i]

if self.__location - req

min_length = abs(req - self.__location) min_no = i if min_no == -1: self.reverse() continue self.__move_length.append(abs(self.__location self.__request[min_no])) self.__sum_length += abs(self.__location self.__request[min_no])

self.__location = self.__request[min_no] self.__scan_trace.append(self.__location) del self.__request[min_no]

self.__request = temp_request self.__location = temp_loc

self.__direction = temp_direction

def run_fcfs(head):

print("======================================================") print("FCFS算法")

print("初始位置: " + str(head.get_location())) head.fcfs()

print("访问顺序:", head.get_fcfs_trace()) print("移动距离:", head.get_move_length()) print("移动总数:", head.get_sum_length()) print("平均寻道:", round(head.get_sum_length() len(head.get_move_length()), 2))

def run_sstf(head):

print("======================================================") print("SSTF算法")

print("初始位置: " + str(head.get_location())) head.sstf()

print("访问顺序:", head.get_sstf_trace()) print("移动距离:", head.get_move_length()) print("移动总数:", head.get_sum_length()) print("平均寻道:", round(head.get_sum_length() len(head.get_move_length()), 2))

def run_cscan(head):

print("======================================================")

- -

/

/

print("CSCAN算法")

print("初始位置: " + str(head.get_location())) if head.get_direction() == 1: print("移动方向:由里向外")

elif head.get_direction() == -1: print("移动方向:由外向里") head.cscan()

print("访问顺序:", head.get_scan_trace()) # print("移动距离:", head.get_move_length()) print("移动总数:", head.get_sum_length()) print("平均寻道:", round(head.get_sum_length() len(head.get_move_length()), 2))

def run_scan(head):

print("======================================================") print("SCAN算法")

print("初始位置: " + str(head.get_location())) if head.get_direction() == 1: print("初始移动方向:由里向外") elif head.get_direction() == -1: print("初始移动方向:由外向里") head.scan()

print("访问顺序:", head.get_scan_trace()) # print("移动距离:", head.get_move_length()) print("移动总数:", head.get_sum_length()) print("平均寻道:", round(head.get_sum_length() len(head.get_move_length()), 2))

if __name__ == "__main__":

head = HEAD(random.randint(1, 200)) for i in range(0, 9):

head.request(random.randint(1, 200)) print("进程的读磁盘请求:", end=" ") head.show_request_list() run_fcfs(head) run_sstf(head) run_scan(head) run_cscan(head)

/

/

心得体会:

1.Python是一种动态语言,对数据类型不严格,使用时必需注意数据类型的控制。

2.这个实验中不同的算法可以实现互相调用以提高代码的重用率,比如NStepSCAN可以调用SCAN

算法进行小队列的处理。前提是算法和数据的设计足够清晰,容易分离。如上面我自己写的例子中,因为写前面的算法时没有考虑后续的可能要求,导致代码重用率很低,已经不方便对前几个算法进行修改。

【实验过程记录(源程序、测试用例、测试结果及心得体会等)】

实验报告

学院(系)名称:计算机与通信工程学院

姓名 班级

卢洪利 2014级4班 课程名称

学号 实验项目

操作系统

20116年12月 8 日 第3、4节 20116年12月12日 第7、8节 20116年12月15日 第3、4节 20116年12月19日 第7、8节

20146049

专业

计算机科学与技术

实验三:磁盘调度算法的实现

课程代码

0668036 软件实验室7-220 软件实验室7-220 软件实验室7-220

实验时间 实验地点

批改意见

成绩

教师签字:

实验环境:

●操作系统:Fedora 25 with Linux 4.8.13-300.fc25.x86_64 ●编程语言:Python 3.5.2

●开发环境:PyCharm 2016.3.1

实验内容:

1.本实验是模拟操作系统的磁盘寻道方式,运用磁盘访问顺序的不同来设计磁盘的调度算法。 2.实现的磁盘调度算法有FCFS,SSTF,SCAN,CSCAN和 NStepSCAN算法。

3.设定开始磁道号寻道范围,依据起始扫描磁道号和最大磁道号数,随机产生要进行寻道的磁道号序

列。

4.选择磁盘调度算法,显示该算法的磁道访问顺序,计算出移动的磁道总数和平均寻道总数。 5.按算法的寻道效率进行排序,并对各算法的性能进行分析比较。

实验要求:

1.详细描述实验设计思想、程序结构及各模块设计思路; 2.详细描述程序所用数据结构及算法; 3.明确给出测试用例和实验结果;

4.为增加程序可读性,在程序中进行适当注释说明;

5.认真进行实验总结,包括:设计中遇到的问题、解决方法与收获等; 6.实验报告撰写要求结构清晰、描述准确逻辑性强;

7.实验过程中,同学之间可以进行讨论互相提高,但绝对禁止抄袭。

设计思想:

设计一个类模拟磁头,给它分配运行方向,当前位置,程序的请求序列等属性,并附有各种寻道算法可以被公开调用,模拟的各种算法在执行时,会产生运行轨迹等数据,这些数据算做磁头的数据属性,随时使用随时清空。

FCFS算法按照请求序列依次满足程序的请求。 SSTF算法选择与当前位置最近的请求进行响应。

SCAN算法优先保证磁头的方向不发生改变的前提下,选择最近的请求进行响应。 CSCAN算法在SCAN算法的基础上,固定了磁头的移动方向。

NStepSCAN算法将请求分为多个队列,每个队列使用SCAN算法。一般将请求分成大于2的队列。 算法实现时,对于SCAN和CSCAN算法,先实现CSCAM算法,在其基础上,当磁头到达最接近边界的一个请求时,将磁头方向反转(代码中的reverse()函数实现这个功能)即可。

执行结果及测试用例:

进程的读磁盘请求: [129, 47, 31, 94, 153, 131, 106, 11, 182](所有算法使用同样的测试用例) ====================================================== FCFS算法 初始位置: 28

访问顺序: [129, 47, 31, 94, 153, 131, 106, 11, 182] 移动距离: [101, 82, 16, 63, 59, 22, 25, 95, 171] 移动总数: 634 平均寻道: 70.44

====================================================== SSTF算法

初始位置: 28

访问顺序: [31, 47, 11, 94, 106, 129, 131, 153, 182] 移动距离: [3, 16, 36, 83, 12, 23, 2, 22, 29] 移动总数: 226 平均寻道: 25.11

====================================================== SCAN算法 初始位置: 28

初始移动方向:由里向外

访问顺序: [31, 47, 94, 106, 129, 131, 153, 182, 11] 移动总数: 325 平均寻道: 36.11

====================================================== CSCAN算法 初始位置: 28

移动方向:由里向外

访问顺序: [31, 47, 94, 106, 129, 131, 153, 182, 11] 移动总数: 164 平均寻道: 18.22

程序源代码:

# coding=utf-8 import random

class HEAD: """

magnetic head """

__location = -1 __next = -1 __request = []

# 大于0为由里向外,小于0 为由外向里 __direction = 0

# SCAN算法的磁道访问顺序 __scan_trace = []

# SSTF算法的磁道访问顺序 __sstf_trace = []

# FCFS算法的磁道访问顺序 __fcfs_trace = []

# 每次寻道的移动距离 __move_length = [] # 寻道的移动总距离 __sum_length = 0

def __init__(self):

self.__location = 1 self.__direction = 1

def __init__(self, location): self.__location = location if location > 100:

self.__direction = -1 elif location

self.__direction = 1

def __clear(self):

self.__sum_length = 0

self.__move_length.clear()

self.__sstf_trace.clear() self.__fcfs_trace.clear() self.__scan_trace.clear()

def get_location(self): return self.__location

def reverse(self):

if self.__direction == 1: self.__direction = -1 elif self.__direction == -1: self.__direction = 1

def request(self, rec):

self.__request.append(rec)

def show_request_list(self): print(self.__request)

def get_sstf_trace(self): return self.__sstf_trace

def get_fcfs_trace(self): return self.__fcfs_trace

def get_scan_trace(self): return self.__scan_trace

def get_move_length(self): return self.__move_length

def get_sum_length(self): return self.__sum_length

def get_direction(self): return self.__direction

def fcfs(self): self.__clear()

temp = self.__location

for rec in self.__request:

self.__move_length.append(abs(self.__location-rec)) self.__sum_length += abs(self.__location-rec) self.__location = rec

self.__fcfs_trace.append(self.__location) # 为了方便与其他算法做对比,将磁头复位 self.__location = temp

def sstf(self): self.__clear()

temp_loc = self.__location temp = self.__request.copy() while len(self.__request) != 0: min_length = 200 min_no = 0

for i in range(0, len(self.__request)): req = self.__request[i]

if abs(req - self.__location)

self.__location = self.__request[min_no] self.__sstf_trace.append(self.__location) del self.__request[min_no] self.__request = temp

# 为了方便与其他算法做对比,将磁头复位 self.__location = temp_loc

def cscan(self): self.__clear()

temp_request = self.__request.copy() temp_loc = self.__location

temp_direction = self.__direction while len(self.__request) != 0: min_length = 200 min_no = -1

if self.__direction == 1:

for i in range(0, len(self.__request)): req = self.__request[i]

if abs(req - self.__location) self.__location:

min_length = abs(req - self.__location) min_no = i if min_no == -1:

self.__location = 1 continue

elif self.__direction == -1:

for i in range(0, len(self.__request)): req = self.__request[i]

if abs(req - self.__location)

min_length = abs(req - self.__location) min_no = i if min_no == -1:

self.__location = 200 continue self.__move_length.append(abs(self.__location - self.__request[min_no])) self.__sum_length += abs(self.__location - self.__request[min_no])

self.__location = self.__request[min_no] self.__scan_trace.append(self.__location) del self.__request[min_no]

self.__request = temp_request self.__location = temp_loc

self.__direction = temp_direction

def scan(self): self.__clear()

temp_direction = self.__direction temp_request = self.__request.copy() temp_loc = self.__location

while len(self.__request) != 0: min_length = 200 min_no = -1

if self.__direction == 1:

for i in range(0, len(self.__request)): req = self.__request[i]

if req - self.__location self.__location:

min_length = abs(req - self.__location) min_no = i if min_no == -1: self.reverse() continue

elif self.__direction == -1:

for i in range(0, len(self.__request)): req = self.__request[i]

if self.__location - req

min_length = abs(req - self.__location) min_no = i if min_no == -1: self.reverse() continue self.__move_length.append(abs(self.__location self.__request[min_no])) self.__sum_length += abs(self.__location self.__request[min_no])

self.__location = self.__request[min_no] self.__scan_trace.append(self.__location) del self.__request[min_no]

self.__request = temp_request self.__location = temp_loc

self.__direction = temp_direction

def run_fcfs(head):

print("======================================================") print("FCFS算法")

print("初始位置: " + str(head.get_location())) head.fcfs()

print("访问顺序:", head.get_fcfs_trace()) print("移动距离:", head.get_move_length()) print("移动总数:", head.get_sum_length()) print("平均寻道:", round(head.get_sum_length() len(head.get_move_length()), 2))

def run_sstf(head):

print("======================================================") print("SSTF算法")

print("初始位置: " + str(head.get_location())) head.sstf()

print("访问顺序:", head.get_sstf_trace()) print("移动距离:", head.get_move_length()) print("移动总数:", head.get_sum_length()) print("平均寻道:", round(head.get_sum_length() len(head.get_move_length()), 2))

def run_cscan(head):

print("======================================================")

- -

/

/

print("CSCAN算法")

print("初始位置: " + str(head.get_location())) if head.get_direction() == 1: print("移动方向:由里向外")

elif head.get_direction() == -1: print("移动方向:由外向里") head.cscan()

print("访问顺序:", head.get_scan_trace()) # print("移动距离:", head.get_move_length()) print("移动总数:", head.get_sum_length()) print("平均寻道:", round(head.get_sum_length() len(head.get_move_length()), 2))

def run_scan(head):

print("======================================================") print("SCAN算法")

print("初始位置: " + str(head.get_location())) if head.get_direction() == 1: print("初始移动方向:由里向外") elif head.get_direction() == -1: print("初始移动方向:由外向里") head.scan()

print("访问顺序:", head.get_scan_trace()) # print("移动距离:", head.get_move_length()) print("移动总数:", head.get_sum_length()) print("平均寻道:", round(head.get_sum_length() len(head.get_move_length()), 2))

if __name__ == "__main__":

head = HEAD(random.randint(1, 200)) for i in range(0, 9):

head.request(random.randint(1, 200)) print("进程的读磁盘请求:", end=" ") head.show_request_list() run_fcfs(head) run_sstf(head) run_scan(head) run_cscan(head)

/

/

心得体会:

1.Python是一种动态语言,对数据类型不严格,使用时必需注意数据类型的控制。

2.这个实验中不同的算法可以实现互相调用以提高代码的重用率,比如NStepSCAN可以调用SCAN

算法进行小队列的处理。前提是算法和数据的设计足够清晰,容易分离。如上面我自己写的例子中,因为写前面的算法时没有考虑后续的可能要求,导致代码重用率很低,已经不方便对前几个算法进行修改。

【实验过程记录(源程序、测试用例、测试结果及心得体会等)】


相关文章

  • 课程设计--磁盘调度算法及代码的实现
  • 课程设计报告 <计算机操作系统> 课程设计题目: 磁盘调度算法 学生姓名: 专 业: 班 级: 学 号: 指导教师: 2014年01月10日 目 录 1.需求分析 „„„„„„„„„„„„„„„„„„„„„„„„„„„„01 2 ...查看


  • 大连理工大学操作系统实验报告
  • 大连理工大学 本科实验报告 课程名称:操作系统实验 学院(系):计算机科学与技术学院 专 班 学业:计算机科学与技术级:号:电计 学生姓名: 年月日 实验项目列表序号 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...查看


  • 磁盘调度实验报告
  • 磁盘调度 一 .实验目的: 磁盘是高速.大容量.旋转型.可直接存取的存储设备.它作为计算机系统的辅助存储器,担负着繁重的输入输出工作,在现代计算机系统中往往同时会有若干个要求访问磁盘的输入输出要求.系统可采用一种策略,尽可能按最佳次序执行访 ...查看


  • 操作系统 磁盘管理 实验报告
  • 实 验 报 告 课程名称:院 系:专业班级:姓 名:指导老师: 操作系统 信息与控制工程学院 计算机0801 2010年 12月 31日 目录 一.实验目的 ......................................... ...查看


  • 磁盘调度算法的实现
  • <操作系统--磁盘调度算法> 实验报告 姓 名: 范学升 学 号:1001050903 班 级:电科10-1班 专 业:电子信息科学与技术 一.实验目的: 通过模拟设计磁盘驱动调度程序,观察驱动调度程序的动态运行过程,理解和掌握 ...查看


  • 实验六磁盘调度算法
  • 实验六 磁盘调度算法 一. 需求分析 ..................................................................................................... ...查看


  • 操作系统 实验报告 文件管理
  • 昆明理工大学信息工程与自动化学院学生实验报告 ( 201 -201 学年 第 二 学期 ) 课程名称:操作系统 开课实验室: 年 月 日 一.实验目的 用C或C++语言编写和调试一个简单的文件系统,模拟文件管理的基本功能.从而对各种文件操作 ...查看


  • 实验二___虚拟存储器
  • 实验二 虚拟存储器 一.实验内容 模拟分页式虚拟存储管理中硬件的地址转换和缺页中断,以及选择页面调度算法处理缺 页中断. 二.实验目的 在计算机系统中,为了提高主存利用率,往往把辅助存储器(如磁盘)作为主存储器的 扩充,使多道运行的作业的全 ...查看


  • 磁盘调度算法的编程实现及评估
  • 操作系统课程设计报告 --磁盘调度算法的编程实现及评估 姓名 学号 日期 一.课程设计题目 磁盘调度算法的编程实现及评估 二.课程设计目的 通过编程实现磁盘调度算法设计,加深理解磁盘调度算法的理解及提高编程能力. 三.课程设计内容 编程实现 ...查看


热门内容