0% found this document useful (0 votes)
68 views6 pages

8 - Disk - Scheduling - Algo (FCFS, SCAN, C-SCAN)

The document describes and provides code examples for three disk scheduling algorithms: FCFS, SCAN, and C-SCAN. FCFS services requests in the order they arrive, while SCAN and C-SCAN attempt to reduce seek time by servicing requests in a clockwise or counterclockwise direction around the disk. For each algorithm, the code provided calculates the total number of seek operations and outputs the seek sequence.

Uploaded by

Brijesh Kuvadiya
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)
68 views6 pages

8 - Disk - Scheduling - Algo (FCFS, SCAN, C-SCAN)

The document describes and provides code examples for three disk scheduling algorithms: FCFS, SCAN, and C-SCAN. FCFS services requests in the order they arrive, while SCAN and C-SCAN attempt to reduce seek time by servicing requests in a clockwise or counterclockwise direction around the disk. For each algorithm, the code provided calculates the total number of seek operations and outputs the seek sequence.

Uploaded by

Brijesh Kuvadiya
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
You are on page 1/ 6

Disk_Scheduling_Algo (FCFS, SCAN, C-SCAN) 17/04/20, 7:16 PM

FCFS
In [ ]: #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()
{

int arr[size] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int head = 50;

FCFS(arr, head);

return 0;
}

http://localhost:8888/nbconvert/html/Jupyter%20Notebooks/OS-…ing_Algo%20(FCFS%2C%20SCAN%2C%20C-SCAN).ipynb?download=false Page 1 of 6
Disk_Scheduling_Algo (FCFS, SCAN, C-SCAN) 17/04/20, 7:16 PM

OUTPUT:

Total number of seek operations = 510

Seek Sequence is 176, 79, 34, 60, 92, 11, 41, 114

SCAN
In [ ]: #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)
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;

http://localhost:8888/nbconvert/html/Jupyter%20Notebooks/OS-…ing_Algo%20(FCFS%2C%20SCAN%2C%20C-SCAN).ipynb?download=false Page 2 of 6
Disk_Scheduling_Algo (FCFS, SCAN, C-SCAN) 17/04/20, 7:16 PM

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 << e
ndl;

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: Total number of seek operations = 226

Seek Sequence is 41, 34, 11, 0, 60, 79, 92, 114, 176

C-SCAN

include <bits/stdc++.h>
http://localhost:8888/nbconvert/html/Jupyter%20Notebooks/OS-…ing_Algo%20(FCFS%2C%20SCAN%2C%20C-SCAN).ipynb?download=false Page 3 of 6
Disk_Scheduling_Algo (FCFS, SCAN, C-SCAN) 17/04/20, 7:16 PM

include <bits/stdc++.h>
using namespace std;

int size = 8; int disk_size = 200;

void CSCAN(int arr[], int head) { int seek_count = 0; int distance, cur_track; vector left, right; vector
seek_sequence;

http://localhost:8888/nbconvert/html/Jupyter%20Notebooks/OS-…ing_Algo%20(FCFS%2C%20SCAN%2C%20C-SCAN).ipynb?download=false Page 4 of 6
Disk_Scheduling_Algo (FCFS, SCAN, C-SCAN) 17/04/20, 7:16 PM

left.push_back(0);
right.push_back(disk_size - 1);

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


if (arr[i] < head)
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());

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;
}

head = 0;

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


cur_track = left[i];

seek_sequence.push_back(cur_track);

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 < seek_sequence.size(); i++) {


cout << seek_sequence[i] << endl;
}

http://localhost:8888/nbconvert/html/Jupyter%20Notebooks/OS-…ing_Algo%20(FCFS%2C%20SCAN%2C%20C-SCAN).ipynb?download=false Page 5 of 6
Disk_Scheduling_Algo (FCFS, SCAN, C-SCAN) 17/04/20, 7:16 PM

int main() {

int arr[size] = { 176, 79, 34, 60,


92, 11, 41, 114 };
int head = 50;

cout << "Initial position of head: " << head << endl;
CSCAN(arr, head);

return 0;

Output:

Initial position of head: 50

Total number of seek operations = 190

Seek Sequence is 60, 79, 92, 114, 176, 199, 0, 11, 34, 41

http://localhost:8888/nbconvert/html/Jupyter%20Notebooks/OS-…ing_Algo%20(FCFS%2C%20SCAN%2C%20C-SCAN).ipynb?download=false Page 6 of 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy