0% found this document useful (0 votes)
2 views4 pages

OS Lab Assignment - 04

The document presents two disk scheduling algorithms: First Come First Serve (FCFS) and Shortest Seek Time First (SSTF). FCFS processes requests in the order they arrive, while SSTF selects the closest request to minimize seek time, though it may lead to starvation for distant requests. Both algorithms include C++ code implementations and calculate total and average seek times.

Uploaded by

Sandesh Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

OS Lab Assignment - 04

The document presents two disk scheduling algorithms: First Come First Serve (FCFS) and Shortest Seek Time First (SSTF). FCFS processes requests in the order they arrive, while SSTF selects the closest request to minimize seek time, though it may lead to starvation for distant requests. Both algorithms include C++ code implementations and calculate total and average seek times.

Uploaded by

Sandesh Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OS Lab Assignment - 04

Q1: Implementation of FCFS Disk Scheduling


The First Come First Serve (FCFS) disk scheduling algorithm processes disk requests
strictly in the order they arrive. It is simple and fair, but not optimal in terms of seek time
since it does not consider the distance between requests.

Code:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

void runFCFS(const vector<int>& req, int startHead) {


int current = startHead;
int seekSum = 0;

cout << "Seek Order: " << current;

for (int i = 0; i < [Link](); i++) {


int movement = abs(current - req[i]);
seekSum += movement;
current = req[i];

cout << " -> " << current;


}

cout << "\nTotal Seek Time: " << seekSum << endl;
cout << "Average Seek Time: "
<< (double)seekSum / [Link]() << endl;
}

int main() {
int n, headPos;
cout << "Number of requests: ";
cin >> n;

vector<int> req(n);
cout << "Enter request sequence:\n";
for (int i = 0; i < n; i++) {
cin >> req[i];
}

cout << "Initial head position: ";


cin >> headPos;

runFCFS(req, headPos);

return 0;
}

Output:

Q2: Implementation of SSTF Disk Scheduling


Shortest Seek Time First (SSTF) selects the request that is closest to the current head
position. This reduces total seek time compared to FCFS, but may cause starvation for
distant requests.

Code:
#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
using namespace std;
void runSSTF(vector<int> req, int startHead) {
int n = [Link]();
vector<bool> serviced(n, false);

int current = startHead;


int totalSeek = 0;

cout << "Seek Order: " << current;

for (int count = 0; count < n; count++) {


int nearestIndex = -1;
int shortestDist = INT_MAX;

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


if (!serviced[i]) {
int dist = abs(current - req[i]);
if (dist < shortestDist) {
shortestDist = dist;
nearestIndex = i;
}
}
}

serviced[nearestIndex] = true;
totalSeek += shortestDist;
current = req[nearestIndex];

cout << " -> " << current;


}

cout << "\nTotal Seek Time: " << totalSeek << endl;
cout << "Average Seek Time: "
<< (double)totalSeek / n << endl;
}

int main() {
int n, headPos;
cout << "Number of requests: ";
cin >> n;

vector<int> req(n);
cout << "Enter request sequence:\n";
for (int i = 0; i < n; i++) {
cin >> req[i];
}

cout << "Initial head position: ";


cin >> headPos;

runSSTF(req, headPos);

return 0;
}

Output:

You might also like