Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

OPERATING SYSTEMS

LAB ASSIGNMENT – 4

PRATHAM SHARMA
20BCE2463

Memory Management

(a) Consider a memory hole of size 1kb initially. When a sequence of memory request arrives as
following, illustrate the memory allocation by various approaches and calculate the total amount
memory wasted by external fragmentation and internal fragmentation in each approach.
i. First fit; ii. Best fit iii. Worst fit (Easy)

CODE:

#include <iostream>
using namespace std;
int main() {
int c,i,j,k,n,l,m[10],p[10],po[20],flag,z,y,temp,temp1;
cout<<"Enter no. of memory total partitions:\t";
cin>>n;
cout<<"\nEnter memory size for\n";
for(i=1;i<=n;i++) {
cout<<"\npartition "<<i<<" :\t";
cin>>m[i];
po[i]=i;
}
cout<<"\nEnter total no.of process:\t";
cin>>j;
cout<<"\nEnter memory size for\n";
for(i=1;i<=j;i++) {
cout<<"\nprocess "<<i<<" :\t";
cin>>p[i];
}
while(1) {
cout<<"\n\n1.First fit\n2.Best fit\n3.Worst fit\n4.Exit\nEnter choice:\t";
cin>>c;
switch(c) {
case 1:
for(i=1;i<=j;i++) {
flag=1;
for(k=1;k<=n;k++) {
if(p[i]<=m[k]) {
cout<<"\nProcess no. "<<i<<" whose memory size is "<<p[i]<<"KB
allocated at memory partition:\t"<<po[k];
m[k]=m[k]-p[i];
break;
}
else {
flag++;
}
}
if(flag>n) {
cout<<"\nProcess no."<<i<<" whose memory size is "<<p[i]<<"KB can't be
allocated";

}
}
break;
case 2:
for(y=1;y<=n;y++) {
for(z=y;z<=n;z++) {
if(m[y]>m[z]) {
temp=m[y];
m[y]=m[z];
m[z]=temp;
temp1=po[y];
po[y]=po[z];
po[z]=temp1;
}
}
}
for(i=1;i<=j;i++) {
flag=1;
for(k=1;k<=n;k++) {
if(p[i]<=m[k]) {
cout<<"\nProcess no."<<i<<" whose memory size is "<<p[i]<<"KB
allocated at memory partition:\t"<<po[k];
m[k]=m[k]-p[i];
break;
}
else {
flag++;
}
}
if(flag>n) {
cout<<"\nProcess no."<<i<<" whose memory size is "<<p[i]<<"KB can't be
allocated";
}
}
break;
case 3:
for(y=1;y<=n;y++) {
for(z=y;z<=n;z++) {
if(m[y]<m[z]) {
temp=m[y];
m[y]=m[z];
m[z]=temp;
temp1=po[y];
po[y]=po[z];
po[z]=temp1;
}
}
}
for(i=1;i<=j;i++) {
flag=1;
for(k=1;k<=n;k++) {
if(p[i]<=m[k]) {
cout<<"\nProcess no. "<<i<<" whose memory size is "<<p[i]<<"KB
allocated at memory partition:\t"<<po[k];
m[k]=m[k]-p[i];
break;
}
else {
flag++;
}
}
if(flag>n) {
cout<<"\nProcess no."<<i<<" whose memory size is "<<p[i]<<"KB can't be
allocated";

}
}
break;
default: exit(0);
}
cout<<"\n";
}
return 0;
}
Output:
(b) Write a program to implement the page replacement algorithms.
i. FIFO
CODE:
#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, pages[n], capacity;
cout<<"Enter the No. of pages: ";
cin>>n;
cout<<"Enter the Page Sequence: ";
for(int i=0;i<n;i++) {
cin>>pages[i];
}
cout<<"Enter capacity: ";
cin>>capacity;
cout<<"Page faults = "<<pageFaults(pages, n, capacity)<<"\n";
return 0;
}

Output:

ii. LRU
CODE:

#include<bits/stdc++.h>
using namespace std;
int pageFaults(int pages[], int n, int capacity) {
unordered_set<int> s;
unordered_map<int, 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[pages[i]] = i;
}
else {
if(s.find(pages[i]) == s.end()) {
int lru = INT_MAX, val;
for(auto it=s.begin(); it!=s.end(); it++) {
if(indexes[*it] < lru) {
lru = indexes[*it];
val = *it;
}
}
s.erase(val);
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
}
}
return page_faults;
}
int main() {
int n, pages[n], capacity;
cout<<"Enter number of pages: ";
cin>>n;
cout<<"Enter page sequence: ";
for(int i=0;i<n;i++) {
cin>>pages[i];
}
cout<<"Enter capacity: ";
cin>>capacity;
cout<<"Page faults: "<<pageFaults(pages, n, capacity)<<"\n";
return 0;}

Output:

iii. OPT (Medium)


CODE:
#include <bits/stdc++.h>
using namespace std;
bool search(int key, vector<int>& fr) {
for (int i = 0; i < fr.size(); i++)
if (fr[i] == key)
return true;
return false;
}
int predict(int pg[], vector<int>& fr, int pn, int index) {
int res = -1, farthest = index;
for (int i = 0; i < fr.size(); i++) {
int j;
for (j = index; j < pn; j++) {
if (fr[i] == pg[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}
if (j == pn)
return i;
}
return (res == -1) ? 0 : res;
}
void optimalPage(int pg[], int pn, int fn) {
vector<int> fr;
int hit = 0;
for (int i = 0; i < pn; i++) {
if (search(pg[i], fr)) {
hit++;
continue;
}
if (fr.size() < fn)
fr.push_back(pg[i]);
else {
int j = predict(pg, fr, pn, i + 1);
fr[j] = pg[i];
}
}
cout << "No. of hits = " << hit << endl;
cout << "No. of misses = " << pn - hit << endl;
}
int main() {
int n, pages[n], capacity;
cout<<"Enter number of pages: ";
cin>>n;
cout<<"Enter page sequence: ";
for(int i=0;i<n;i++) {
cin>>pages[i];
}
cout<<"Enter capacity: ";
cin>>capacity;
optimalPage(pages, n, capacity);
return 0;
}

Output:
(c) Write a program that implements the FIFO, LRU, and optimal pager replacement algorithms. First,
generate a random page-reference string where page numbers range from 0 to 9. Apply the random
page reference string to each algorithm, and record the number of page faults incurred by each
algorithm. Implement the replacement algorithms so that the number of page frames can vary from 1
to 7. Assume that demand paging is used. (High)

CODE:
#include <bits/stdc++.h>
using namespace std;
int FIFO(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 LRU(int pages[], int n, int capacity) {
unordered_set<int> s;
unordered_map<int, 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[pages[i]] = i;
}
else {
if(s.find(pages[i]) == s.end()) {
int lru = INT_MAX, val;
for(auto it=s.begin(); it!=s.end(); it++) {
if(indexes[*it] < lru) {
lru = indexes[*it];
val = *it;
}
}
s.erase(val);
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
}
}
return page_faults;
}
bool search(int key, vector<int>& fr) {
for (int i = 0; i < fr.size(); i++)
if (fr[i] == key)
return true;
return false;
}
int predict(int pg[], vector<int>& fr, int pn, int index) {
int res = -1, farthest = index;
for (int i = 0; i < fr.size(); i++) {
int j;
for (j = index; j < pn; j++) {
if (fr[i] == pg[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}
if (j == pn)
return i;
}
return (res == -1) ? 0 : res;
}
int optimalPage(int pg[], int pn, int fn) {
vector<int> fr;
int hit = 0;
for (int i = 0; i < pn; i++) {
if (search(pg[i], fr)) {
hit++;
continue;
}
if (fr.size() < fn)
fr.push_back(pg[i]);
else {
int j = predict(pg, fr, pn, i + 1);
fr[j] = pg[i];
}
}
int x = pn - hit;
return x;
}
int main() {
int n, x, pages[n], capacity;
cout<<"Enter number of pages: ";
cin>>n;
cout<<"Random page sequence: ";
srand(time(0));
for(int i=0;i<n;i++) {
pages[i] = rand()%10;
cout<<pages[i]<<" ";
}
cout<<"\nEnter page frame size: ";
cin>>capacity;
cout<<"FIFO page miss: "<<FIFO(pages, n, capacity)<<endl;
cout<<"LRY page miss: "<<LRU(pages, n, capacity)<<endl;
cout<<"Optimal page miss: "<<optimalPage(pages, n, capacity)<<endl;
return 0;
}

Output:
File system and Disk Management
(a) Implement the following Disk scheduling algorithms:
i. SSTF
CODE:
#include <bits/stdc++.h>
using namespace std;
void calculatedifference(int request[], int head, int diff[][2], int n) {
for(int i = 0; i < n; i++) {
diff[i][0] = abs(head - request[i]);
}
}
int findMIN(int diff[][2], int n) {
int index = -1;
int minimum = 1e9;
for(int i = 0; i < n; i++) {
if (!diff[i][1] && minimum > diff[i][0]) {
minimum = diff[i][0];
index = i;
}
}
return index;
}
void shortestSeekTimeFirst(int request[], int head, int n) {
if (n == 0) {
return;
}
int diff[n][2] = { { 0, 0 } };
int seekcount = 0;
int seeksequence[n + 1] = {0};
for(int i = 0; i < n; i++) {
seeksequence[i] = head;
calculatedifference(request, head, diff, n);
int index = findMIN(diff, n);
diff[index][1] = 1;
seekcount += diff[index][0];
head = request[index];
}
seeksequence[n] = head;
cout << "Total number of seek operations = "<< seekcount << endl;
cout << "Seek sequence is : " << "\n";
for(int i = 0; i <= n; i++) {
cout << seeksequence[i] << "\n";
}
}
int main() {
int n, proc[n], head;
cout<<"Enter number of seek requests: ";
cin>>n;
cout<<"Enter seek requests: ";
for(int i=0;i<n;i++) {
cin>>proc[i];
}
cout<<"Enter head position: ";
cin>>head;
shortestSeekTimeFirst(proc, head, n);
return 0;
}

Output:

ii. SCAN
CODE:
#include <bits/stdc++.h>
using namespace std;
void SCAN(int arr[], int head, string direction, int disk_size, int size) {
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;
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 << endl;
cout << "Seek Sequence is" << endl;
for (int i = 0; i < seek_sequence.size(); i++) {
cout << seek_sequence[i] << endl;
}
}
int main() {
int disk_size, size, arr[size], head;
cout<<"Enter disk size: ";
cin>>disk_size;
cout<<"Enter number of requests: ";
cin>>size;
cout<<"Enter requests: ";
for(int i=0;i<size;i++) {
cin>>arr[i];
}
cout<<"Enter the head position: ";
cin>>head;
string direction;
cout<<"Enter direction(left/right): ";
cin>>direction;
SCAN(arr, head, direction, disk_size, size);
return 0;
}
Output:

iii. C-SCAN
CODE:

#include <bits/stdc++.h>
using namespace std;
void CSCAN(int arr[], int head, int size, int disk_size) {
int seek_count = 0;
int distance, cur_track;
vector<int> left, right;
vector<int> seek_sequence;
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;
seek_count += (disk_size - 1);
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;
}
}
int main() {
int disk_size, size, arr[size], head;
cout<<"Enter disk size: ";
cin>>disk_size;
cout<<"Enter number of requests: ";
cin>>size;
cout<<"Enter requests: ";
for(int i=0;i<size;i++) {
cin>>arr[i];
}
cout<<"Enter the head position: ";
cin>>head;
CSCAN(arr, head, size, disk_size);
return 0;
}

Output:
iv. FCFS
CODE:
#include <bits/stdc++.h>
using namespace std;
void FCFS(int arr[], int head, int size) {
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 size, arr[size], head;
cout<<"Enter number of requests: ";
cin>>size;
cout<<"Enter requests: ";
for(int i=0;i<size;i++) {
cin>>arr[i];
}
cout<<"Enter the head position: ";
cin>>head;
FCFS(arr, head, size);
return 0;
}

Output:
(b) Consider a file of size 1 MB. The size of a disk block is 512Bytes. Assume any number of available
free blocks in the disk contiguously or non-contiguously. Implement the following algorithms to
perform file allocation. Determine the efficiency of each file allocation strategies.
i. Sequential
CODE:
#include <stdio.h>

void main()
{
int f[50], i, st, len, j, k, count = 0;

for (i = 0; i < 50; i++)


f[i] = 0;
printf("Files Allocated are : \n");
count = 0;
printf("Enter starting block and length of files: ");
scanf("%d%d", &st, &len);
for (k = st; k < (st + len); k++)
if (f[k] == 0)
count++;
if (len == count)
{
for (j = st; j < (st + len); j++)
if (f[j] == 0)
{
f[j] = 1;
printf("%d\t%d\n", j, f[j]);
}
if (j != (st + len - 1))
printf("The file is allocated to disk\n");
}
else
printf(" The file is not allocated \n");
}

Output:
ii. Indexed
CODE:
#include <stdio.h>

#include <stdlib.h>
void main()
{
int f[50], index[50], i, n, st, len, j, c, k, ind, count = 0;

for (i = 0; i < 50; i++)


f[i] = 0;
printf("Enter the index block: ");
scanf("%d", &ind);
if (f[ind] != 1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d", &n);
}
else
{
printf("%d index is already allocated \n", ind);
}
count = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &index[i]);
if (f[index[i]] == 0)
count++;
}
if (count == n)
{
for (j = 0; j < n; j++)
f[index[j]] = 1;
printf("Allocated\n");
printf("File Indexed\n");
for (k = 0; k < n; k++)
printf("%d ------- >%d : %d\n", ind, index[k], f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
}
}
Output:

iii. Linked
CODE:

#include <stdio.h>
int main()
{
int f[50], p, i, j, k, a, st, len, n;
for (i = 0; i < 50; i++)
f[i] = 0;
printf("Enter how many blocks that are already allocated");
scanf("%d", &p);
printf("\nEnter the blocks no.s that are already allocated");
for (i = 0; i < p; i++)
{
scanf("%d", &a);
f[a] = 1;
}
printf("Enter the starting index block & length");
scanf("%d%d", &st, &len);
k = len;
for (j = st; j < (k + st); j++)
{
if (f[j] == 0)
{
f[j] = 1;
printf("\n%d->%d", j, f[j]);
}
else
{
printf("\n %d->file is already allocated", j);
k++;
}
}
}

Output:

You might also like