SSTF Disk Scheduling
SSTF (Shortest Seek Time First) is a disk scheduling algorithm used by operating systems to optimize the movement of the read/write heads of a hard disk.
In this algorithm, the operating system selects the request for I/O (input/output) that has the shortest seek time from the current position of the head. Seek time is the time required for the head to move from its current position to the location of the requested sector on the disk. By selecting the request with the shortest seek time, the algorithm minimizes the distance the head has to travel and therefore reduces the overall access time.
SSTF is considered to be a fast and efficient algorithm since it reduces the average waiting time and access time of the disk. However, it can result in starvation for requests that are far from the current position of the head, which may cause a performance issue in some situations.
Here given code implementation process.
// C program
// SSTF disk scheduling algorithm
#include <stdio.h>
#include <limits.h>
//Implement SSTF disk scheduling algorithm
void sstf_disk_scheduling(int queue[], int head, int n)
{
if (n <= 0)
{
return;
}
double seek_time = 0.0;
double minimum = 0.0;
//This are storing the information of seek sequence
int skeek[n + 1];
//Create 2d array which is used to store distance and visited status
int auxiliary[n][2];
for (int i = 0; i < n; ++i)
{
// set initial distance
auxiliary[i][0] = 0;
// set the visiting element status
auxiliary[i][1] = 0;
}
// Loop controlling variable
int i = 0;
int j = 0;
int location = 0;
for (i = 0; i < n; i++)
{
skeek[i] = head;
// Find distance using head value
for (j = 0; j < n; ++j)
{
auxiliary[j][0] = queue[j] - head;
if (auxiliary[j][0] < 0)
{
auxiliary[j][0] = -auxiliary[j][0];
}
}
minimum = INT_MAX;
location = -1;
//Find the minimum element location that is not visited
for (j = 0; j < n; ++j)
{
if (auxiliary[j][1] == 0 && auxiliary[j][0] <= minimum)
{
// Get the new minimum distance element location
location = j;
minimum = auxiliary[j][0];
}
}
// Update the visited status of new get element
auxiliary[location][1] = 1;
// Update head data into current track value
head = queue[location];
// Add current distance into seek
seek_time += auxiliary[location][0];
}
if (head != 0)
{
// Add last skee info
skeek[n] = head;
}
printf("\n Seek Sequence : ");
//Display given queue elements
for (i = 0; i <= n; i++)
{
printf(" %d", skeek[i]);
}
//Display result
printf("\n Total Seek Time : %lf", seek_time);
printf("\n Average Seek Time : %lf\n", seek_time / n);
}
int main()
{
// Request queue elements
int queue[] = {
63,
12,
32,
43,
54,
61,
59,
34,
52,
24
};
//Get the number of elements in request queue
int n = sizeof(queue) / sizeof(queue[0]);
//Initial head position
int head = 13;
sstf_disk_scheduling(queue, head, n);
return 0;
}
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52.000000
Average Seek Time : 5.200000
// Java program
// SSTF disk scheduling algorithm
class SstfScheduling
{
//Implement SSTF disk scheduling algorithm
public void sstf_disk_scheduling(int[] queue, int head, int n)
{
if (n <= 0)
{
return;
}
double seek_time = 0.0;
double minimum = 0.0;
//This are storing the information of seek sequence
int[] skeek = new int[n + 1];
//Create 2d array which is used to store distance and visited status
int[][] auxiliary = new int[n][2];
for (int i = 0; i < n; ++i)
{
// set initial distance
auxiliary[i][0] = 0;
// set the visiting element status
auxiliary[i][1] = 0;
}
// Loop controlling variable
int i = 0;
int j = 0;
int location = 0;
for (i = 0; i < n; i++)
{
skeek[i] = head;
// Find distance using head value
for (j = 0; j < n; ++j)
{
auxiliary[j][0] = queue[j] - head;
if (auxiliary[j][0] < 0)
{
auxiliary[j][0] = -auxiliary[j][0];
}
}
minimum = Integer.MAX_VALUE;
location = -1;
//Find the minimum element location that is not visited
for (j = 0; j < n; ++j)
{
if (auxiliary[j][1] == 0 && auxiliary[j][0] <= minimum)
{
// Get the new minimum distance element location
location = j;
minimum = auxiliary[j][0];
}
}
// Update the visited status of new get element
auxiliary[location][1] = 1;
// Update head data into current track value
head = queue[location];
// Add current distance into seek
seek_time += auxiliary[location][0];
}
if (head != 0)
{
// Add last skee info
skeek[n] = head;
}
System.out.print("\n Seek Sequence : ");
//Display given queue elements
for (i = 0; i <= n; i++)
{
System.out.print(" " + skeek[i] + "");
}
//Display result
System.out.print("\n Total Seek Time : " + seek_time);
System.out.print("\n Average Seek Time : " + seek_time / n + "\n");
}
public static void main(String[] args)
{
SstfScheduling obj = new SstfScheduling();
// Request queue elements
int[] queue = {
63,
12,
32,
43,
54,
61,
59,
34,
52,
24
};
//Get the number of elements in request queue
int n = queue.length;
//Initial head position
int head = 13;
obj.sstf_disk_scheduling(queue, head, n);
}
}
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52.0
Average Seek Time : 5.2
//Include header file
#include <iostream>
#include<limits.h>
using namespace std;
// C++ program
// SSTF disk scheduling algorithm
class SstfScheduling
{
public:
//Implement SSTF disk scheduling algorithm
void sstf_disk_scheduling(int queue[], int head, int n)
{
if (n <= 0)
{
return;
}
double seek_time = 0.0;
double minimum = 0.0;
//This are storing the information of seek sequence
int skeek[n + 1];
//Create 2d array which is used to store distance and visited status
int auxiliary[n][2];
for (int i = 0; i < n; ++i)
{
// set initial distance
auxiliary[i][0] = 0;
// set the visiting element status
auxiliary[i][1] = 0;
}
// Loop controlling variable
int i = 0;
int j = 0;
int location = 0;
for (i = 0; i < n; i++)
{
skeek[i] = head;
// Find distance using head value
for (j = 0; j < n; ++j)
{
auxiliary[j][0] = queue[j] - head;
if (auxiliary[j][0] < 0)
{
auxiliary[j][0] = -auxiliary[j][0];
}
}
minimum = INT_MAX;
location = -1;
//Find the minimum element location that is not visited
for (j = 0; j < n; ++j)
{
if (auxiliary[j][1] == 0 && auxiliary[j][0] <= minimum)
{
// Get the new minimum distance element location
location = j;
minimum = auxiliary[j][0];
}
}
// Update the visited status of new get element
auxiliary[location][1] = 1;
// Update head data into current track value
head = queue[location];
// Add current distance into seek
seek_time += auxiliary[location][0];
}
if (head != 0)
{
// Add last skee info
skeek[n] = head;
}
cout << "\n Seek Sequence : ";
//Display given queue elements
for (i = 0; i <= n; i++)
{
cout << " " << skeek[i] << "";
}
//Display result
cout << "\n Total Seek Time : " << seek_time;
cout << "\n Average Seek Time : " << seek_time / n << "\n";
}
};
int main()
{
SstfScheduling obj = SstfScheduling();
// Request queue elements
int queue[] = {
63 , 12 , 32 , 43 , 54 , 61 , 59 , 34 , 52 , 24
};
//Get the number of elements in request queue
int n = sizeof(queue) / sizeof(queue[0]);
//Initial head position
int head = 13;
obj.sstf_disk_scheduling(queue, head, n);
return 0;
}
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52
Average Seek Time : 5.2
//Include namespace system
using System;
// C# program
// SSTF disk scheduling algorithm
class SstfScheduling
{
//Implement SSTF disk scheduling algorithm
public void sstf_disk_scheduling(int[] queue, int head, int n)
{
if (n <= 0)
{
return;
}
double seek_time = 0.0;
double minimum = 0.0;
//This are storing the information of seek sequence
int[] skeek = new int[n + 1];
//Create 2d array which is used to store distance and visited status
int[,] auxiliary = new int[n,2];
// Loop controlling variable
int i = 0;
int j = 0;
int location = 0;
for (i = 0; i < n; ++i)
{
// set initial distance
auxiliary[i,0] = 0;
// set the visiting element status
auxiliary[i,1] = 0;
}
for (i = 0; i < n; i++)
{
skeek[i] = head;
// Find distance using head value
for (j = 0; j < n; ++j)
{
auxiliary[j,0] = queue[j] - head;
if (auxiliary[j,0] < 0)
{
auxiliary[j,0] = -auxiliary[j,0];
}
}
minimum = int.MaxValue;
location = -1;
//Find the minimum element location that is not visited
for (j = 0; j < n; ++j)
{
if (auxiliary[j,1] == 0 && auxiliary[j,0] <= minimum)
{
// Get the new minimum distance element location
location = j;
minimum = auxiliary[j,0];
}
}
// Update the visited status of new get element
auxiliary[location,1] = 1;
// Update head data into current track value
head = queue[location];
// Add current distance into seek
seek_time += auxiliary[location,0];
}
if (head != 0)
{
// Add last skee info
skeek[n] = head;
}
Console.Write("\n Seek Sequence : ");
//Display given queue elements
for (i = 0; i <= n; i++)
{
Console.Write(" " + skeek[i] + "");
}
//Display result
Console.Write("\n Total Seek Time : " + seek_time);
Console.Write("\n Average Seek Time : " + seek_time / n + "\n");
}
public static void Main(String[] args)
{
SstfScheduling obj = new SstfScheduling();
// Request queue elements
int[] queue = {
63 , 12 , 32 , 43 , 54 , 61 , 59 , 34 , 52 , 24
};
//Get the number of elements in request queue
int n = queue.Length;
//Initial head position
int head = 13;
obj.sstf_disk_scheduling(queue, head, n);
}
}
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52
Average Seek Time : 5.2
<?php
// Php program
// SSTF disk scheduling algorithm
class SstfScheduling
{
//Implement SSTF disk scheduling algorithm
public function sstf_disk_scheduling( & $queue, $head, $n)
{
if ($n <= 0)
{
return;
}
$seek_time = 0.0;
$minimum = 0.0;
//This are storing the information of seek sequence
$skeek = array_fill(0, $n + 1, 0);
//Create 2d array which is used to store distance and visited status
$auxiliary = array_fill(0, $n, array_fill(0, $n, 0));
// Loop controlling variable
$i = 0;
$j = 0;
$location = 0;
for ($i = 0; $i < $n; ++$i)
{
// set initial distance
$auxiliary[$i][0] = 0;
// set the visiting element status
$auxiliary[$i][1] = 0;
}
for ($i = 0; $i < $n; $i++)
{
$skeek[$i] = $head;
// Find distance using head value
for ($j = 0; $j < $n; ++$j)
{
$auxiliary[$j][0] = $queue[$j] - $head;
if ($auxiliary[$j][0] < 0)
{
$auxiliary[$j][0] = -$auxiliary[$j][0];
}
}
$minimum = PHP_INT_MAX;
$location = -1;
//Find the minimum element location that is not visited
for ($j = 0; $j < $n; ++$j)
{
if ($auxiliary[$j][1] == 0 && $auxiliary[$j][0] <= $minimum)
{
// Get the new minimum distance element location
$location = $j;
$minimum = $auxiliary[$j][0];
}
}
// Update the visited status of new get element
$auxiliary[$location][1] = 1;
// Update head data into current track value
$head = $queue[$location];
// Add current distance into seek
$seek_time += $auxiliary[$location][0];
}
if ($head != 0)
{
// Add last skee info
$skeek[$n] = $head;
}
echo "\n Seek Sequence : ";
//Display given queue elements
for ($i = 0; $i <= $n; $i++)
{
echo " ". $skeek[$i] ."";
}
//Display result
echo "\n Total Seek Time : ". $seek_time;
echo "\n Average Seek Time : ". ($seek_time / $n) ."\n";
}
}
function main()
{
$obj = new SstfScheduling();
// Request queue elements
$queue = array(63, 12, 32, 43, 54, 61, 59, 34, 52, 24);
//Get the number of elements in request queue
$n = count($queue);
//Initial head position
$head = 13;
$obj->sstf_disk_scheduling($queue, $head, $n);
}
main();
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52
Average Seek Time : 5.2
// Node Js program
// SSTF disk scheduling algorithm
class SstfScheduling
{
//Implement SSTF disk scheduling algorithm
sstf_disk_scheduling(queue, head, n)
{
if (n <= 0)
{
return;
}
var seek_time = 0.0;
var minimum = 0.0;
//This are storing the information of seek sequence
var skeek = Array(n + 1).fill(0);
//Create 2d array which is used to store distance and visited status
var auxiliary = Array(n).fill(0).map(() => new Array(n).fill(0));
// Loop controlling variable
var i = 0;
var j = 0;
var location = 0;
for (i = 0; i < n; ++i)
{
// set initial distance
auxiliary[i][0] = 0;
// set the visiting element status
auxiliary[i][1] = 0;
}
for (i = 0; i < n; i++)
{
skeek[i] = head;
// Find distance using head value
for (j = 0; j < n; ++j)
{
auxiliary[j][0] = queue[j] - head;
if (auxiliary[j][0] < 0)
{
auxiliary[j][0] = -auxiliary[j][0];
}
}
minimum = Number.MAX_VALUE;
location = -1;
//Find the minimum element location that is not visited
for (j = 0; j < n; ++j)
{
if (auxiliary[j][1] == 0 && auxiliary[j][0] <= minimum)
{
// Get the new minimum distance element location
location = j;
minimum = auxiliary[j][0];
}
}
// Update the visited status of new get element
auxiliary[location][1] = 1;
// Update head data into current track value
head = queue[location];
// Add current distance into seek
seek_time += auxiliary[location][0];
}
if (head != 0)
{
// Add last skee info
skeek[n] = head;
}
process.stdout.write("\n Seek Sequence : ");
//Display given queue elements
for (i = 0; i <= n; i++)
{
process.stdout.write(" " + skeek[i] + "");
}
//Display result
process.stdout.write("\n Total Seek Time : " + seek_time);
process.stdout.write("\n Average Seek Time : " + (seek_time / n) + "\n");
}
}
function main()
{
var obj = new SstfScheduling();
// Request queue elements
var queue = [63, 12, 32, 43, 54, 61, 59, 34, 52, 24];
//Get the number of elements in request queue
var n = queue.length;
//Initial head position
var head = 13;
obj.sstf_disk_scheduling(queue, head, n);
}
main();
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52
Average Seek Time : 5.2
import sys
# Python 3 program
# SSTF disk scheduling algorithm
class SstfScheduling :
# Implement SSTF disk scheduling algorithm
def sstf_disk_scheduling(self, queue, head, n) :
if (n <= 0) :
return
seek_time = 0.0
minimum = 0.0
# This are storing the information of seek sequence
skeek = [0] * (n + 1)
# Create 2d array which is used to store distance and visited status
auxiliary = [[0 for _ in range(n)] for _ in range(n)]
# Loop controlling variable
i = 0
j = 0
location = 0
while (i < n) :
# set initial distance
auxiliary[i][0] = 0
# set the visiting element status
auxiliary[i][1] = 0
i += 1
i = 0
while (i < n) :
skeek[i] = head
# Find distance using head value
j = 0
while (j < n) :
auxiliary[j][0] = queue[j] - head
if (auxiliary[j][0] < 0) :
auxiliary[j][0] = -auxiliary[j][0]
j += 1
minimum = sys.maxsize
location = -1
# Find the minimum element location that is not visited
j = 0
while (j < n) :
if (auxiliary[j][1] == 0 and auxiliary[j][0] <= minimum) :
# Get the new minimum distance element location
location = j
minimum = auxiliary[j][0]
j += 1
# Update the visited status of new get element
auxiliary[location][1] = 1
# Update head data into current track value
head = queue[location]
# Add current distance into seek
seek_time += auxiliary[location][0]
i += 1
if (head != 0) :
# Add last skee info
skeek[n] = head
print("\n Seek Sequence : ", end = "")
# Display given queue elements
i = 0
while (i <= n) :
print(" ", skeek[i] ,"", end = "")
i += 1
# Display result
print("\n Total Seek Time : ", seek_time, end = "")
print("\n Average Seek Time : ", (seek_time / n) )
def main() :
obj = SstfScheduling()
# Request queue elements
queue = [63, 12, 32, 43, 54, 61, 59, 34, 52, 24]
# Get the number of elements in request queue
n = len(queue)
# Initial head position
head = 13
obj.sstf_disk_scheduling(queue, head, n)
if __name__ == "__main__": main()
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52.0
Average Seek Time : 5.2
# Ruby program
# SSTF disk scheduling algorithm
class SstfScheduling
# Implement SSTF disk scheduling algorithm
def sstf_disk_scheduling(queue, head, n)
if (n <= 0)
return
end
seek_time = 0.0
minimum = 0.0
# This are storing the information of seek sequence
skeek = Array.new(n + 1) {0}
# Create 2d array which is used to store distance and visited status
auxiliary = Array.new(n) {Array.new(n,0)}
# Loop controlling variable
i = 0
j = 0
location = 0
while (i < n)
# set initial distance
auxiliary[i][0] = 0
# set the visiting element status
auxiliary[i][1] = 0
i += 1
end
i = 0
while (i < n)
skeek[i] = head
# Find distance using head value
j = 0
while (j < n)
auxiliary[j][0] = queue[j] - head
if (auxiliary[j][0] < 0)
auxiliary[j][0] = -auxiliary[j][0]
end
j += 1
end
minimum = (2 ** (0. size * 8 - 2))
location = -1
# Find the minimum element location that is not visited
j = 0
while (j < n)
if (auxiliary[j][1] == 0 && auxiliary[j][0] <= minimum)
# Get the new minimum distance element location
location = j
minimum = auxiliary[j][0]
end
j += 1
end
# Update the visited status of new get element
auxiliary[location][1] = 1
# Update head data into current track value
head = queue[location]
# Add current distance into seek
seek_time += auxiliary[location][0]
i += 1
end
if (head != 0)
# Add last skee info
skeek[n] = head
end
print("\n Seek Sequence : ")
# Display given queue elements
i = 0
while (i <= n)
print(" ", skeek[i] ,"")
i += 1
end
# Display result
print("\n Total Seek Time : ", seek_time)
print("\n Average Seek Time : ", seek_time / n ,"\n")
end
end
def main()
obj = SstfScheduling.new()
# Request queue elements
queue = [63, 12, 32, 43, 54, 61, 59, 34, 52, 24]
# Get the number of elements in request queue
n = queue.length
# Initial head position
head = 13
obj.sstf_disk_scheduling(queue, head, n)
end
main()
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52.0
Average Seek Time : 5.2
// Scala program
// SSTF disk scheduling algorithm
class SstfScheduling
{
//Implement SSTF disk scheduling algorithm
def sstf_disk_scheduling(queue: Array[Int], initial_head: Int, n: Int): Unit = {
if (n <= 0)
{
return;
}
var head = initial_head;
var seek_time: Double = 0.0;
var minimum: Double = 0.0;
//This are storing the information of seek sequence
var skeek: Array[Int] = Array.fill[Int](n + 1)(0);
//Create 2d array which is used to store distance and visited status
var auxiliary: Array[Array[Int]] = Array.fill[Int](n,n)(0);
// Loop controlling variable
var i: Int = 0;
var j: Int = 0;
var location: Int = 0;
while (i < n)
{
// set initial distance
auxiliary(i)(0) = 0;
// set the visiting element status
auxiliary(i)(1) = 0;
i += 1;
}
i = 0;
while (i < n)
{
skeek(i) = head;
// Find distance using head value
j = 0;
while (j < n)
{
auxiliary(j)(0) = queue(j) - head;
if (auxiliary(j)(0) < 0)
{
auxiliary(j)(0) = -auxiliary(j)(0);
}
j += 1;
}
minimum = Int.MaxValue;
location = -1;
//Find the minimum element location that is not visited
j = 0;
while (j < n)
{
if (auxiliary(j)(1) == 0 && auxiliary(j)(0) <= minimum)
{
// Get the new minimum distance element location
location = j;
minimum = auxiliary(j)(0);
}
j += 1;
}
// Update the visited status of new get element
auxiliary(location)(1) = 1;
// Update head data into current track value
head = queue(location);
// Add current distance into seek
seek_time += auxiliary(location)(0);
i += 1;
}
if (head != 0)
{
// Add last skee info
skeek(n) = head;
}
print("\n Seek Sequence : ");
//Display given queue elements
i = 0;
while (i <= n)
{
print(" " + skeek(i) + "");
i += 1;
}
//Display result
print("\n Total Seek Time : " + seek_time);
print("\n Average Seek Time : " + (seek_time / n) + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: SstfScheduling = new SstfScheduling();
// Request queue elements
var queue: Array[Int] = Array(63, 12, 32, 43, 54, 61, 59, 34, 52, 24);
//Get the number of elements in request queue
var n: Int = queue.length;
//Initial head position
var head: Int = 13;
obj.sstf_disk_scheduling(queue, head, n);
}
}
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52.0
Average Seek Time : 5.2
// Swift program
// SSTF disk scheduling algorithm
class SstfScheduling
{
//Implement SSTF disk scheduling algorithm
func sstf_disk_scheduling(_ queue: [Int], _ head: inout Int, _ n: Int)
{
if (n <= 0)
{
return;
}
var seek_time: Double = 0.0;
var minimum: Int = 0;
//This are storing the information of seek sequence
var skeek: [Int] = Array(repeating: 0, count: n + 1);
//Create 2d array which is used to store distance and visited status
var auxiliary: [[Int]] = Array(repeating : Array(repeating : 0, count : n), count : n);
// Loop controlling variable
var i: Int = 0;
var j: Int = 0;
var location: Int = 0;
while (i < n)
{
// set initial distance
auxiliary[i][0] = 0;
// set the visiting element status
auxiliary[i][1] = 0;
i += 1;
}
i = 0;
while (i < n)
{
skeek[i] = head;
// Find distance using head value
j = 0;
while (j < n)
{
auxiliary[j][0] = queue[j] - head;
if (auxiliary[j][0] < 0)
{
auxiliary[j][0] = -auxiliary[j][0];
}
j += 1;
}
minimum = Int.max;
location = -1;
//Find the minimum element location that is not visited
j = 0;
while (j < n)
{
if (auxiliary[j][1] == 0 && auxiliary[j][0] <= minimum)
{
// Get the new minimum distance element location
location = j;
minimum = auxiliary[j][0];
}
j += 1;
}
// Update the visited status of new get element
auxiliary[location][1] = 1;
// Update head data into current track value
head = queue[location];
// Add current distance into seek
seek_time += Double(auxiliary[location][0]);
i += 1;
}
if (head != 0)
{
// Add last skee info
skeek[n] = head;
}
print("\n Seek Sequence : ", terminator: "");
//Display given queue elements
i = 0;
while (i <= n)
{
print(" ", skeek[i] ,"", terminator: "");
i += 1;
}
//Display result
print("\n Total Seek Time : ", seek_time, terminator: "");
print("\n Average Seek Time : ", seek_time / Double(n) ,"\n", terminator: "");
}
}
func main()
{
let obj: SstfScheduling = SstfScheduling();
// Request queue elements
let queue: [Int] = [63, 12, 32, 43, 54, 61, 59, 34, 52, 24];
//Get the number of elements in request queue
let n: Int = queue.count;
//Initial head position
var head: Int = 13;
obj.sstf_disk_scheduling(queue, &head, n);
}
main();
Output
Seek Sequence : 13 12 24 32 34 43 52 54 59 61 63
Total Seek Time : 52.0
Average Seek Time : 5.2
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment