Experiment:2.2: Write A Program To Simulate Disk Scheduling Algorithms A) FCFS B) SCAN
Experiment:2.2: Write A Program To Simulate Disk Scheduling Algorithms A) FCFS B) SCAN
Experiment:2.2: Write A Program To Simulate Disk Scheduling Algorithms A) FCFS B) SCAN
Experiment:2.2
One of the responsibilities of the operating system is to use the hardware efficiently. For the
disk drives, meeting this responsibility entails having fast access time and large disk
bandwidth. Both the access time and the bandwidth can be improved by managing the order
in which disk I/O requests are serviced which is called as disk scheduling. The simplest form of
disk scheduling is, of course, the first-come, first-served (FCFS) algorithm. This algorithm is
intrinsically fair, but it generally does not provide the fastest service. In the SCAN algorithm, the
disk arm starts at one end, and moves towards the other end, servicing requests as it reaches
each cylinder, until it gets to the other end of the disk. At the other end, the direction of head
movement is reversed, and servicing continues. The head continuously scans back and forth
across the disk. C-SCAN is a variant of SCAN designed to provide a more uniform wait time. Like
SCAN, C-SCAN moves the head from one end of the disk to the other, servicing requests along
the way. When the head reaches the other end, however, it immediately returns to the
beginning of the disk without servicing any requests on the return trip.
FCFS is the simplest disk scheduling algorithm as the name suggests, this algorithm entertains
requests in the order they arrive in the disk queue. The algorithm looks very fair and there is no
starvation (all requests are serviced sequentially) but generally, it does not provide the fastest
service
In SCAN disk scheduling algorithm, head starts from one end of the disk and moves towards
the other end, servicing requests in between one by one and reach the other end. Then the
direction of the head is reversed and the process continues as head continuously scan back
Pseudo code/Algorithms/Flowchart/Steps:
1. Let Request array represents an array storing indexes of tracks that have been requested
in ascending order of their time of arrival. ‘head’ is the position of disk head.
2. Let us one by one take the tracks in default order and calculate the absolute distance of
the track from the head.
4. Currently serviced track position now becomes the new head position.
5. Go to step 2 until all tracks in request array have not been serviced.
Step 1: Let Request array represents an array storing indexes of tracks that have been
requested in ascending order of their time of arrival. ‘head’ is the position of disk head.
Step 2: Let direction represents whether the head is moving towards left or right.
Step 3: In the direction in which head is moving service all tracks one by one.
Step 4: Calculate the absolute distance of the track from the head.
Step 5: Increment the total seek count with this distance.
Step 6: Currently serviced track position now becomes the new head position.
Step 7: Go to step 3 until we reach at one of the ends of the disk.
Step 8: If we reach at the end of the disk reverse the direction and go to step 2 u
Implementation:
1. FCFS
#include <bits/stdc++.h>
using namespace std;
int size = 8;
void FCFS(int arr[], int head)
{
int seek_count = 0;
int distance, cur_track;
for (int i = 0; i < size; i++) {
cur_track = arr[i];
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track; }
cout << "Total number of seek operations = "
<< seek_count << endl;
cout << "Seek Sequence is" << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << endl; }}
int main()
{
// request array
int arr[size] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int head = 50;
FCFS(arr, head);
return 0;
}
Scan:
#include <bits/stdc++.h>
using namespace std;
int size = 8;
int disk_size = 200;
left.push_back(arr[i]);
if (arr[i] > head)
right.push_back(arr[i]);
}
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end());
int run = 2;
while (run--) {
if (direction == "left") {
for (int i = left.size() - 1; i >= 0; i--) {
cur_track = left[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
direction = "right"; }
else if (direction == "right") {
for (int i = 0; i < right.size(); i++) {
cur_track = right[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track; }
direction = "left"; }}
cout << "Total number of seek operations = "
<< seek_count << endl;
cout << "Seek Sequence is" << endl;
for (int i = 0; i < seek_sequence.size(); i++) {
cout << seek_sequence[i] << endl; }}
int main()
{
int arr[size] = { 176, 79, 34, 60,92, 11, 41, 114 };
int head = 50;
string direction = "left";
SCAN(arr, head, direction);
return 0;}
Output:
LEARNING OUTCOMES: -
The students will be able to
i. To select a disk request from the queue of I/O requests and decide the schedule
when this request will be processed.
ii. To Increase the throughput: the average number of requests satisfied per time unit.
iii. To use the hardware efficiently using fast access time and large disk bandwidth that
depends on the relative positions of the read-write head and the requested data.