You are on page 1of 7

Course Name: Real Time System Lab Course code: CSP - 457

Experiment:2.2

Aim: Write a program to simulate Disk Scheduling Algorithms a) FCFS b) SCAN

Tools/Software Required: Dev C++


Description

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.

2.2.1 First Come First Serve (FCFS)

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

2.2.3 SCAN (Elevator) Algorithm

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

Name: AHMAD UID: 19BCS2850


MASSIH
Course Name: Real Time System Lab Course code: CSP - 457
and forth to access the disk. So, this algorithm works as an elevator and hence also known as
the elevator algorithm. As a result, the requests at the midrange are serviced more and those
arriving behind the disk arm will have to wait.

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.

3. Increment the total seek count with this distance.

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

Name: AHMAD UID: 19BCS2850


MASSIH
Course Name: Real Time System Lab Course code: CSP - 457
#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;

Name: AHMAD UID: 19BCS2850


MASSIH
Course Name: Real Time System Lab Course code: CSP - 457
FCFS(arr, head);
return 0;
}
Scan:
#include <bits/stdc++.h>
using namespace std;

int size = 8;
int disk_size = 200;

void SCAN(int arr[], int head, string direction)


{
int seek_count = 0;
int distance, cur_track;
vector<int> left, right;
vector<int> seek_sequence; if (direction == "left")
left.push_back(0);
else if (direction == "right")
right.push_back(disk_size - 1);

for (int i = 0; i < size; i++) {


if (arr[i] < head)

Name: AHMAD UID: 19BCS2850


MASSIH
Course Name: Real Time System Lab Course code: CSP - 457
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);

Name: AHMAD UID: 19BCS2850


MASSIH
Course Name: Real Time System Lab Course code: CSP - 457
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:

Name: AHMAD UID: 19BCS2850


MASSIH
Course Name: Real Time System Lab Course code: CSP - 457

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.

Name: AHMAD UID: 19BCS2850


MASSIH

You might also like