You are on page 1of 18

Source code:

i) LRU

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

int pageFaults(int n, int c, int pages[])


{
// Initialise count to 0
int count = 0;

// To store elements in memory of size c


vector<int> v;
int i;
for (i = 0; i <= n - 1; i++) {
// check if element present or not
auto it = find(v.begin(), v.end(), pages[i]);

if (it == v.end()) {

// If memory is full
if (v.size() == c) {

// Remove the first element


// As it is least recently used
v.erase(v.begin());
}
// Add the recent element into memory
v.push_back(pages[i]);
// Increment the count
count++;
}
else {
// If element is present
// Remove the element
v.erase(it);
v.push_back(pages[i]);
}
}

return count;
}
int main()
{

int n,c;
cout<<"Enter the number of pages: ";
cin>>n;
cout<<endl;
cout<<"Enter the capacity: ";
cin>>c;

int pages[n];

cout<<"\nEnter the pages: ";


for(int i=0;i<n;i++){
cin>>pages[i];
}

cout << "\nPage Faults = " << pageFaults(n, c, pages);


return 0;
}

Output:
Source code:
ii) FIFO:

#include <bits/stdc++.h>
using namespace std;
int pageFaults(int pages[], int n, int capacity)
{
unordered_set<int> s;
queue<int> indexes;
int page_faults = 0;
for (int i = 0; i < n; i++)
{
if (s.size() < capacity)
{
if (s.find(pages[i]) == s.end())
{
s.insert(pages[i]);
page_faults++;
indexes.push(pages[i]);
}
}
else
{
if (s.find(pages[i]) == s.end())
{
int val = indexes.front();
indexes.pop();
s.erase(val);
s.insert(pages[i]);
indexes.push(pages[i]);
page_faults++;
}
}
}
return page_faults;
}
int main()
{
int n;
cout << "Enter number of pages : ";
cin >> n;
int pages[n], capacity;
cout << "Enter pages : ";
for (int i = 0; i < n; i++)
{
cin >> pages[i];
}
cout << "Enter capacity : ";
cin >> capacity;
cout << "Page faults : " << pageFaults(pages, n, capacity);
return 0;
}

Output:
Source code:
iii) Optimal Page Replacement:

#include <bits/stdc++.h>
using namespace std;
bool find(int key, vector<int> &frames)
{
for (int i = 0; i < frames.size(); i++)
if (frames[i] == key)
return true;
return false;
}
int predict(int pages[], vector<int> &frames, int n, int idx)
{
int res = -1, end = idx;
for (int i = 0; i < frames.size(); i++)
{
int j;
for (j = idx; j < n; j++)
{
if (frames[i] == pages[j])
{
if (j > end)
{
end = j;
res = i;
}
break;
}
}
if (j == n) return i;
}
return (res == -1) ? 0 : res;
}
int optimalPage(int pages[], int n, int capacity)
{
vector<int> frames;
int hit = 0;
for (int i = 0; i < n; i++)
{
if (find(pages[i], frames))
{
hit++;
continue;
}
if (frames.size() < capacity)
frames.push_back(pages[i]);
else
{
int j = predict(pages, frames, n, i + 1);
frames[j] = pages[i];
}
}
return n-hit;
}
int main()
{
// int pages[] = {4, 7, 6, 1, 7, 6, 1, 2, 7, 2};
int n;
cout << "Enter number of pages : ";
cin >> n;
int pages[n], capacity;
cout << "Enter pages : ";
for (int i = 0; i < n; i++)
{
cin >> pages[i];
}
cout << "Enter capacity : ";
cin >> capacity;
cout << "Page faults : " << optimalPage(pages, n, capacity);
}

Output:
Source code:
i)First Fit:

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

void firstFit(int memSize[], int m,


int processSize[], int n)
{
int allocation[n];
memset(allocation, -1, sizeof(allocation));

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


{
for (int j = 0; j < m; j++)
{
if (memSize[j] >= processSize[i])
{
allocation[i] = j;
memSize[j] -= processSize[i];

break;
}
}
}
cout << "\t\tFirst Fit" << endl;
cout << "\nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i + 1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}

int main()
{
int memSize[] = {300, 50, 200, 350, 70};
int processSize[] = {200, 47, 212, 426, 10};
int m = sizeof(memSize) / sizeof(memSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);

firstFit(memSize, m, processSize, n);

return 0;
}

Output:
Source code:
ii)Best Fit:

#include <bits/stdc++.h>
using namespace std;
void bestFit(int memSize[], int m, int processSize[], int n)
{
int allocation[n];
memset(allocation, -1, sizeof(allocation));
for (int i = 0; i < n; i++)
{
int bestIdx = -1;
for (int j = 0; j < m; j++)
{
if (memSize[j] >= processSize[i])
{
if (bestIdx == -1)
{
bestIdx = j;
}
else if (memSize[bestIdx] > memSize[j])
{
bestIdx = j;
}
}
}

if (bestIdx != -1)
{
allocation[i] = bestIdx;
memSize[bestIdx] -= processSize[i];
}
}
cout << "\tBest Fit" << endl;
cout << "\nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i + 1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
{
cout << allocation[i] + 1;
}
else
{
cout << "Not Allocated";
}

cout << endl;


}
}

int main()
{
int memSize[] = {10, 20, 7, 10, 40};
int processSize[] = {6, 10, 20, 30, 20};
int m = sizeof(memSize) / sizeof(memSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);

bestFit(memSize, m, processSize, n);


return 0;
}

Output:
Source code:
iii) Worst Fit:

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

void worstFit(int memSize[], int m, int processSize[], int n)


{
int allocation[n];
memset(allocation, -1, sizeof(allocation));
for (int i = 0; i < n; i++)
{
int idx = -1;
for (int j = 0; j < m; j++)
{
if (memSize[j] >= processSize[i])
{
if (idx == -1)
idx = j;
else if (memSize[idx] < memSize[j])
idx = j;
}
}
if (idx != -1)
{
allocation[i] = idx;

memSize[idx] -= processSize[i];
}
}
cout << "\t\tWorst Fit" << endl;
cout << "\nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i + 1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}

int main()
{
int memSize[] = {100, 200, 300, 400, 500};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(memSize) / sizeof(memSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
worstFit(memSize, m, processSize, n);
return 0;
}

Output:
Source code:

#include <iostream>
using namespace std;

int main()
{
// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4

int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix


{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4

int avail[3] = { 3, 3, 2 }; // Available Resources

int f[n], ans[n], ind = 0;


for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}

if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}

int flag = 1;

// To check if sequence is safe or not


for(int i = 0;i<n;i++)
{
if(f[i]==0)
{
flag = 0;
cout << "The given sequence is not safe";
break;
}
}

if(flag==1)
{
cout << "Following is the SAFE Sequence" << endl;
for (i = 0; i < n - 1; i++)
cout << " P" << ans[i] << " ->";
cout << " P" << ans[n - 1] <<endl;
}
return (0);
}

Output:
Source code:

#include <stdio.h>
int main() {

// P0, P1, P2, P3, P4 are the processes

int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources

// Allocation Matrix
int alloc[5][3] = { { 0, 1, 0 }, // P0
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4

// MAX Matrix
int max[5][3] = { { 7, 5, 3 }, // P0
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4

// Available Resources
int avail[3] = { 3, 3, 2 };

// Printing details
printf("\nProcess\t\tAllocation\t\t\tMax\t\n");
printf("\t\t\tA\tB\tC\t\tA\tB\tC\n");
printf("-------------------------------------");
for(int i=0; i<n; i++) {
printf("\n P%d\t", i);
printf("\t%d\t%d\t%d\t",
alloc[i][0],
alloc[i][1],
alloc[i][2]
);

printf("\t%d\t%d\t%d",
max[i][0],
max[i][1],
max[i][2]
);
}

printf("\nAvailable: A = %d | B = %d | C = %d",
avail[0],
avail[1],
avail[2]
);

int f[n], ans[n], ind = 0;


for (k = 0; k < n; k++) {
f[k] = 0;
}

// Creating need matrix


int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}

// Banker's Algorithm logic


int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {

int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}

if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;

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


if(f[i]==0) {
flag=0;
printf("\nThe following system is not safe.");
break;
}
}

if(flag == 1) {
printf("\n\nFollowing is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}

return (0);
}

Output:

You might also like