20Z352 - OS Lab Report
20Z352 - OS Lab Report
Introduction
Any program before execution resides in secondary memory or the hard disk drive
(HDD). Back when they were introduced, HDDs were a revolution. They were the
go-to secondary storage mechanism for computers. With the advent of solid state
drives, HDDs have however now fallen out of use, although they remain contextual
relevant in the history of computer evolution.
HDDs are non-volatile memory storage devices, meaning that they retain the data
written onto them even after termination of power supply to the computer. HDDs
OS Laboratory Report 1
contain platters made out of non-magnetic material, coated with a shallow layer of
magnetic material. Data is stored on the platters by rotating them at high speeds with
read and write heads modifying the magnetisation of the material that passes
beneath them. The varying magnetisations of the material are interpreted as data.
The whole idea immediately suggests that a lot of energy is spent in running HDDs.
A powerful computer typically encounters situations where several HDD access
requests are made at a time. Responding to these requests irresponsibly will lead to
the computer bearing the brunt of resource misuse. This called for the development
of algorithms that optimised the way HDD access requests were handled by the
computer, and thus Disk Scheduling algorithms were born.
HDDs are some of the slowest parts of the computer which need to be used
responsibly.
Although multiple HDD access requests are possible, only one can be served at
a time by a disk controller.
While one request is being handled by the disk controller, the other requests
have to wait to be served the disk controller.
The further two requested memory locations are physically, the further the
needle to the HDD travels to address the later requests after the earlier request.
OS Laboratory Report 2
A hard disk controller, the hardware the mediates between I/O requests and disk drives. Credits:
Wikipedia
Important Terminologies
Parts of an HDD
OS Laboratory Report 3
The internal structure of the hard disk drive showing spindle, arms with read-write heads, platters,
tracks and sectors. Credits:
https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/10_MassStorage.html
Platter: Disk shaped structures which are rotated in an HDD. Data is stored on
them
Track: Concentric circles around the spindle, along which data is stored
Cylinder: A collection of all the tracks of a given distance from the spindle
Arm Assembly: The apparatus against which the platters are rotated
Seek Time
The time taken to change the location of the disk arm to the requested memory
location is called seek time.
OS Laboratory Report 4
Seek time is directly proportional to the distance of separation between the
memory locations of the current request and the next request to be handled.
Rotational latency
The time taken by the desired sector of a disk to rotate into a position so that it can
access the read-write heads is called rotational latency.
Transfer Time
Transfer time is the time to transfer the data. It depends on the rotating speed of
the disk and number of bytes to be transferred.
All the desired factors may not achievable in a single algorithm. Hence, there are a
number of algorithms giving precedence to selected factors and making certain
trade-offs between them. These algorithms may not suit all scenarios and may work
efficiently for special scenarios only. The following are some algorithms that have
been developed for disk scheduling:
FCFS (First-come-first-served)
OS Laboratory Report 5
It processes requests in the order of arrival of the requests into the disk queue.
LOOK
It is a slightly more optimised form of the SCAN algorithm, where instead of
traversing to the ends of the memory, traversal is made only up to the requests
closest to the ends of the memory.
LIFO (Last-in-first-out)
FSCAN
SCAN Algorithm
The SCAN algorithm is also termed the elevator algorithm. Elevators service stop
requests only as the floors appear during their ascent or descent. For example, a
OS Laboratory Report 6
press of buttons 2, 3, 4, and 5 at the first floor will lead to the elevator stopping at all
the 4 floors in the order of arrival of the floors; however, a press of 1 while the lift is
on the third floor will be not be serviced during the ascent and will only be serviced
on descent, after the 4th floor call is serviced. Much akin to this, the SCAN algorithm
works as follows:
Algorithm
Assume a stretch of memory from address 0 to address x.
2. If the previously serviced request is closer to address 0 than curr, then the
servicing of requests happens in the direction moving towards x. Else, traversal
happens towards address 0.
3. Whilst traversing till either end (decided based on step 2), service requests that
appear along the way.
4. Traverse until the chosen end and complete all the requests encountered in this
traversal. The ignored requests will be serviced in the next traversal.
5. Reverse directions and traverse through the memory, heading to the opposite
end. In this traversal, service the remaining requests that appear along the way.
Disadvantages
1. It is slightly complex to implement.
2. Wasteful traversal till any one end even after reaching furthest request.
3. Requests that arrive just after the arm proceeds in the opposite direction are
serviced unfairly late.
Question
Suppose that a disk drive has 5000 cylinders, numbered 0 to 4999. The drive is
currently serving
a request at cylinder 143, and the previous request was at cylinder 125. The queue
of pending
requests, in FIFO order, is 86, 1470, 913, 1774, 948, 1509, 1022, 1750, 130. Starting
from the
current head position, what is the total distance (in cylinders) that the disk arm
moves to satisfy
OS Laboratory Report 7
all the pending requests, using SCAN disk-scheduling algorithms? Write a C
program to
implement the same.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int requests[100], i, j, n, start, maxSize, traversals, prev, seekTime = 0;
printf("Enter the number of requests:\n");
scanf("%d", &n);
printf("Enter the requested addresses:\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &requests[i]);
}
printf("Enter position where head must begin: \n");
scanf("%d", &start);
printf("Enter total disk size: \n");
scanf("%d", &maxSize);
printf("Enter the previously serviced request: \n");
scanf("%d", &prev);
traversals = (prev < start) ? 1 : 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (requests[j] > requests[j + 1])
{
int temp;
temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
int index;
for (i = 0; i < n; i++)
{
if (start < requests[i])
{
index = i;
break;
}
}
// if traversal is towards high value
if (traversals == 1)
{
for (i = index; i < n; i++)
{
seekTime = seekTime + abs(requests[i] - start);
start = requests[i];
}
// last traversal for maxSize
seekTime = seekTime + abs(maxSize - requests[i - 1] - 1);
OS Laboratory Report 8
start = maxSize - 1;
for (i = index - 1; i >= 0; i--)
{
seekTime = seekTime + abs(requests[i] - start);
start = requests[i];
}
}
// if traversal is towards low value
else
{
for (i = index - 1; i >= 0; i--)
{
seekTime = seekTime + abs(requests[i] - start);
start = requests[i];
}
// last traversal for min maxSize
seekTime = seekTime + abs(requests[i + 1] - 0);
start = 0;
for (i = index; i < n; i++)
{
seekTime = seekTime + abs(requests[i] - start);
start = requests[i];
}
}
Output:
OS Laboratory Report 9