Skip to content

Commit 46de36f

Browse files
added
1 parent 954544c commit 46de36f

File tree

7 files changed

+471
-4
lines changed

7 files changed

+471
-4
lines changed

CLOOK.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// C++ implementation of the approach
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
int size = 8;
5+
int disk_size = 200;
6+
7+
// Function to perform C-LOOK on the request
8+
// array starting from the given head
9+
void CLOOK(int arr[], int head)
10+
{
11+
int seek_count = 0;
12+
int distance, cur_track;
13+
vector<int> left, right;
14+
vector<int> seek_sequence;
15+
16+
// Tracks on the left of the
17+
// head will be serviced when
18+
// once the head comes back
19+
// to the beggining (left end)
20+
for (int i = 0; i < size; i++) {
21+
if (arr[i] < head)
22+
left.push_back(arr[i]);
23+
if (arr[i] > head)
24+
right.push_back(arr[i]);
25+
}
26+
27+
// Sorting left and right vectors
28+
std::sort(left.begin(), left.end());
29+
std::sort(right.begin(), right.end());
30+
31+
// First service the requests
32+
// on the right side of the
33+
// head
34+
for (int i = 0; i < right.size(); i++) {
35+
cur_track = right[i];
36+
37+
// Appending current track to seek sequence
38+
seek_sequence.push_back(cur_track);
39+
40+
// Calculate absolute distance
41+
distance = abs(cur_track - head);
42+
43+
// Increase the total count
44+
seek_count += distance;
45+
46+
// Accessed track is now new head
47+
head = cur_track;
48+
}
49+
50+
// Once reached the right end
51+
// jump to the last track that
52+
// is needed to be serviced in
53+
// left direction
54+
seek_count += abs(head - left[0]);
55+
head = left[0];
56+
57+
// Now service the requests again
58+
// which are left
59+
for (int i = 0; i < left.size(); i++) {
60+
cur_track = left[i];
61+
62+
// Appending current track to seek sequence
63+
seek_sequence.push_back(cur_track);
64+
65+
// Calculate absolute distance
66+
distance = abs(cur_track - head);
67+
68+
// Increase the total count
69+
seek_count += distance;
70+
71+
// Accessed track is now the new head
72+
head = cur_track;
73+
}
74+
75+
cout << "Total number of seek operations = "
76+
<< seek_count << endl;
77+
78+
cout << "Seek Sequence is" << endl;
79+
80+
for (int i = 0; i < seek_sequence.size(); i++) {
81+
cout << seek_sequence[i] << endl;
82+
}
83+
}
84+
85+
// Driver code
86+
int main()
87+
{
88+
// Request array
89+
int arr[size] = { 176, 79, 34, 60,
90+
92, 11, 41, 114 };
91+
int head = 50;
92+
93+
cout << "Initial position of head: " << head << endl;
94+
95+
CLOOK(arr, head);
96+
97+
return 0;
98+
}

FIFO.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// C++ implementation of FIFO page replacement
2+
// in Operating Systems.
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
// Function to find page faults using FIFO
7+
int pageFaults(int pages[], int n, int capacity)
8+
{
9+
// To represent set of current pages. We use
10+
// an unordered_set so that we quickly check
11+
// if a page is present in set or not
12+
unordered_set<int> s;
13+
14+
// To store the pages in FIFO manner
15+
queue<int> indexes;
16+
17+
// Start from initial page
18+
int page_faults = 0;
19+
for (int i=0; i<n; i++)
20+
{
21+
// Check if the set can hold more pages
22+
if (s.size() < capacity)
23+
{
24+
// Insert it into set if not present
25+
// already which represents page fault
26+
if (s.find(pages[i])==s.end())
27+
{
28+
s.insert(pages[i]);
29+
30+
// increment page fault
31+
page_faults++;
32+
33+
// Push the current page into the queue
34+
indexes.push(pages[i]);
35+
}
36+
}
37+
38+
// If the set is full then need to perform FIFO
39+
// i.e. remove the first page of the queue from
40+
// set and queue both and insert the current page
41+
else
42+
{
43+
// Check if current page is not already
44+
// present in the set
45+
if (s.find(pages[i]) == s.end())
46+
{
47+
//Pop the first page from the queue
48+
int val = indexes.front();
49+
50+
indexes.pop();
51+
52+
// Remove the indexes page
53+
s.erase(val);
54+
55+
// insert the current page
56+
s.insert(pages[i]);
57+
58+
// push the current page into
59+
// the queue
60+
indexes.push(pages[i]);
61+
62+
// Increment page faults
63+
page_faults++;
64+
}
65+
}
66+
}
67+
68+
return page_faults;
69+
}
70+
71+
// Driver code
72+
int main()
73+
{
74+
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4,
75+
2, 3, 0, 3, 2};
76+
int n = sizeof(pages)/sizeof(pages[0]);
77+
int capacity = 4;
78+
cout << "Total Faults are "<<pageFaults(pages, n, capacity);
79+
return 0;
80+
}

LOOK.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
// C++ program to demonstrate
6+
// SCAN Disk Scheduling algorithm
7+
8+
int size = 8;
9+
int disk_size = 200;
10+
11+
void LOOK(int arr[], int head, string direction)
12+
{
13+
int seek_count = 0;
14+
int distance, cur_track;
15+
vector<int> left, right;
16+
vector<int> seek_sequence;
17+
18+
// appending values which are
19+
// currently at left and right
20+
// direction from the head.
21+
for (int i = 0; i < size; i++) {
22+
if (arr[i] < head)
23+
left.push_back(arr[i]);
24+
if (arr[i] > head)
25+
right.push_back(arr[i]);
26+
}
27+
28+
// sorting left and right vectors
29+
// for servicing tracks in the
30+
// correct sequence.
31+
std::sort(left.begin(), left.end());
32+
std::sort(right.begin(), right.end());
33+
34+
// run the while loop two times.
35+
// one by one scanning right
36+
// and left side of the head
37+
int run = 2;
38+
while (run--) {
39+
if (direction == "left") {
40+
for (int i = left.size() - 1; i >= 0; i--) {
41+
cur_track = left[i];
42+
43+
// appending current track to seek sequence
44+
seek_sequence.push_back(cur_track);
45+
46+
// calculate absolute distance
47+
distance = abs(cur_track - head);
48+
49+
// increase the total count
50+
seek_count += distance;
51+
52+
// accessed track is now the new head
53+
head = cur_track;
54+
}
55+
// reversing the direction
56+
direction = "right";
57+
}
58+
else if (direction == "right") {
59+
for (int i = 0; i < right.size(); i++) {
60+
cur_track = right[i];
61+
// appending current track to seek sequence
62+
seek_sequence.push_back(cur_track);
63+
64+
// calculate absolute distance
65+
distance = abs(cur_track - head);
66+
67+
// increase the total count
68+
seek_count += distance;
69+
70+
// accessed track is now new head
71+
head = cur_track;
72+
}
73+
// reversing the direction
74+
direction = "left";
75+
}
76+
}
77+
78+
cout << "Total number of seek operations = "
79+
<< seek_count << endl;
80+
81+
cout << "Seek Sequence is" << endl;
82+
83+
for (int i = 0; i < seek_sequence.size(); i++) {
84+
cout << seek_sequence[i] << endl;
85+
}
86+
}
87+
88+
// Driver code
89+
int main()
90+
{
91+
92+
// request array
93+
int arr[size] = { 176, 79, 34, 60,
94+
92, 11, 41, 114 };
95+
int head = 50;
96+
string direction = "right";
97+
98+
cout << "Initial position of head: "
99+
<< head << endl;
100+
101+
LOOK(arr, head, direction);
102+
103+
return 0;
104+
}

LRU.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//C++ implementation of above algorithm
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
// Function to find page faults using indexes
6+
int pageFaults(int pages[], int n, int capacity)
7+
{
8+
// To represent set of current pages. We use
9+
// an unordered_set so that we quickly check
10+
// if a page is present in set or not
11+
unordered_set<int> s;
12+
13+
// To store least recently used indexes
14+
// of pages.
15+
unordered_map<int, int> indexes;
16+
17+
// Start from initial page
18+
int page_faults = 0;
19+
for (int i=0; i<n; i++)
20+
{
21+
// Check if the set can hold more pages
22+
if (s.size() < capacity)
23+
{
24+
// Insert it into set if not present
25+
// already which represents page fault
26+
if (s.find(pages[i])==s.end())
27+
{
28+
s.insert(pages[i]);
29+
30+
// increment page fault
31+
page_faults++;
32+
}
33+
34+
// Store the recently used index of
35+
// each page
36+
indexes[pages[i]] = i;
37+
}
38+
39+
// If the set is full then need to perform lru
40+
// i.e. remove the least recently used page
41+
// and insert the current page
42+
else
43+
{
44+
// Check if current page is not already
45+
// present in the set
46+
if (s.find(pages[i]) == s.end())
47+
{
48+
// Find the least recently used pages
49+
// that is present in the set
50+
int lru = INT_MAX, val;
51+
for (auto it=s.begin(); it!=s.end(); it++)
52+
{
53+
if (indexes[*it] < lru)
54+
{
55+
lru = indexes[*it];
56+
val = *it;
57+
// cout<<indexes[*it]<<" index";
58+
}
59+
}
60+
61+
// Remove the indexes page
62+
s.erase(val);
63+
64+
// insert the current page
65+
s.insert(pages[i]);
66+
67+
// Increment page faults
68+
page_faults++;
69+
}
70+
71+
// Update the current page index
72+
indexes[pages[i]] = i;
73+
}
74+
}
75+
76+
return page_faults;
77+
}
78+
79+
// Driver code
80+
int main()
81+
{
82+
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
83+
int n = sizeof(pages)/sizeof(pages[0]);
84+
int capacity = 4;
85+
cout << pageFaults(pages, n, capacity);
86+
return 0;
87+
}

0 commit comments

Comments
 (0)