如何在此代码中实现循环 SSTF?

How can I implement Circular SSTF in this code?

在我的操作系统 class 中,我们必须提出一个算法作为我们的最终项目。任何 OS 算法的模拟模型。我选择了一种算法,它是常规 SSTF 算法的 improved/variation。它被称为循环 SSTF。我已经在 python 中实现了 SSTF。但我想不出实现循环 SSTF 的方法。任何帮助或提示将不胜感激。

下面是对这两种算法的解释。

给出 假设请求是:98, 183, 37, 122, 14, 124, 65, 67.

基于 SSTF 算法,OS 将按以下方式分配请求,

The order is : 53, 65, 67, 37, 14, 98, 122, 124, 183. Total head

movement : 12+2+30+23+84+24+2+59 = 236 tracks.

在这个算法中,我们首先检查哪个请求更接近当前头部(在本例中为 53)。最近的是65然后65成为当前头,最接近的是67然后67成为当前的头所以继续算法。

基于循环 SSTF 算法,OS 将按以下方式分配请求,

The order is : 53, 65, 67, 37, 14, 183, 124, 122, 98.

Total head movement : 12+2+30+23+31+59+2+24 = 183 tracks.

在 Circular SSTF 中,如果太多请求更接近任何一端 (0-199),算法将切换。因此,算法不是从 14 到 98,而是强制服务 183,因为它离另一端更近。

另外,抱歉我的英语不好。好久没写这么详细和技术性的东西了。

编辑1。 描述了算法并删除了代码,因为它可能会引起不必要的混淆。

SSTF 和循环 SSTF 之间的主要区别在于如何计算头部和扇区之间的差异。虽然它是 SSTF 的简单模块差异,但在 Circular SSTF 中它变成:

def difference(head, sector, end=199):
    diff1 = abs(head-sector) # going straight from head to sector
    diff2 = head + (end-sector) # going to 0 and then from end to sector
    diff3 = (end-head) + sector # going to end and then going from 0 to sector
    return min([diff1, diff2, diff3])

运行 您反对的意见已返回

The order will be: 
65  
67  
37  
14  
183  
124  
122  
98  

Output with SSTF algorithm: 
Head Movement: 182