2021UCA1930 (Ospractical)
2021UCA1930 (Ospractical)
2021UCA1930 (Ospractical)
-Shreyansh Agarwal
-2021UCA1930
-------------------------------------
Question 1: Write a program for system calls, fork(),
wait(), exit(), signal(), getpid(), getppid()
//getpid() Call:
// C++ Code to demonstrate getpid()
#include <iostream>
#include <unistd.h>
using namespace std;
// Driver Code
int main()
{
int pid = fork();
if (pid == 0)
cout << "\nCurrent process id of Process : "
<< getpid() << endl;
return 0;
}
Output:
//getppid() Call:
// C++ Code to demonstrate getppid()
#include <iostream>
#include <unistd.h>
using namespace std;
// Driver Code
int main()
{
int pid;
pid = fork();
if (pid == 0)
{
cout << "\nParent Process id : "
<< getpid() << endl;
cout << "\nChild Process with parent id : "
<< getppid() << endl;
}
return 0;
}
Output:
//Fork() Call:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t p;
printf("before fork\n");
p = fork();
if (p == 0) {
printf("I am child having id %d\n", getpid());
printf("My parent's id is %d\n", getppid());
}
else {
printf("My child's id is %d\n", p);
printf("I am parent having id %d\n", getpid());
}
printf("Common\n");
}
Output:
//Wait () Call:
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<sys/wait.h>
int main()
{
pid_t p;
printf("before fork\n");
p = fork();
if (p == 0) //child
{
printf("I am child having id %d\n", getpid());
printf("My parent's id is %d\n", getppid());
}
else//parent
{
wait(NULL);
printf("My child's id is %d\n", p);
printf("I am parent having id %d\n", getpid());
}
printf("Common\n");
Output:
//Signal () Call:
#include<stdio.h>
#include<signal.h>
int main()
{
signal(SIGINT, handle_sigint);
while (1) ;
return 0;
}
Output:
//Exit () Call:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE * pFile;
pFile = fopen ("myfile.txt", "r");
if (pFile == NULL)
{
printf ("Error opening file");
exit (1);
}
else
{
/* file operations here */
}
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1, full = 0, empty = 3, x = 0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while (1)
{
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n)
{
case 1:
if ((mutex == 1) && (empty != 0))
producer();
else
printf("Buffer is full!!");
break;
case 2:
if ((mutex == 1) && (full != 0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return (++s);
}
void producer()
{
mutex = wait(mutex);
full = signal(full);
empty = wait(empty);
x++;
printf("\nProducer produces the item %d", x);
mutex = signal(mutex);
}
void consumer()
{
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);
printf("\nConsumer consumes item %d", x);
x--;
mutex = signal(mutex);
}
//Unbounded Buffer
#include <stdio.h>
#include <stdlib.h>
#include <climits>
int mutex = 1, full = 0, empty = INT_MAX, x = 0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while (1)
{
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n)
{
case 1:
if ((mutex == 1) && (empty != 0))
producer();
else
printf("Buffer is full!!");
break;
case 2:
if ((mutex == 1) && (full != 0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return (++s);
}
void producer()
{
mutex = wait(mutex);
full = signal(full);
empty = wait(empty);
x++;
printf("\nProducer produces the item %d", x);
mutex = signal(mutex);
}
void consumer()
{
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);
printf("\nConsumer consumes item %d", x);
x--;
mutex = signal(mutex);
}
Output:
Bounded Buffer:
Unbounded Buffer:
//Reader.cpp:
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
using namespace std;
int main()
{
//ftok function is used to generate unique key
key_t my_key = ftok("shmfile",65);
//shmget returns an ide in shmid
int shmid = shmget(my_key,1024,0666|IPC_CREAT);
//shmat to join to shared memory
char *str = (char*) shmat(shmid,(void*)0,0);
printf("Data read from memory: %s\n",str);
shmdt(str);
shmctl(shmid,IPC_RMID,NULL); // destroy the shared memory
}
//SJF Algo
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, n) for (int i = 0; i < n; i++)
void DDA(float x1, float y1, float x2, float y2, int i, int
buffer = 0)
{
float dx = x2 - x1;
float dy = y2 - y1;
// Finding slope
float m = dy / dx;
float steps;
if (abs(m) <= 1)
{
steps = abs(dx);
}
else
{
steps = abs(dy);
}
float Delx = dx / steps;
float Dely = dy / steps;
float x = x1, y = y1;
for (int k = 1; k <= steps; k++)
{
x = x + Delx;
y = y + Dely;
}
}
void makeRect( int x, int y, int i, int t) //t is thickness
i,e, time
{
{
DDA(x, y, t * 10 + x, y, i);
DDA(x + t * 10, y, t * 10 + x, y + 50, i);
DDA(x + t * 10, y + 50, x, 50 + y, i);
DDA(x, y + 50, x, y, i);
}
}
void intToString(int num, char str[100]) {
if (num == 0) {
str[0] = '0';
str[1] = '\0';
return;
}
int num_of_digits = 0, temp = num;
while (temp) {
++num_of_digits;
temp /= 10;
}
str[num_of_digits] = '\0';
temp = num;
int i = num_of_digits - 1;
while (temp) {
str[i] = '0' + (temp % 10);
temp /= 10;
--i;
}
}
int main()
{
cout << "enter number of processes\n";
int n;
cin >> n;
int burstTimes[n];
cout << "Enter the burst times \n";
f(i, n) {
cout << "Process " << (i + 1) << "th :";
cin >> burstTimes[i];
}
sort(burstTimes, burstTimes + n);
int x = 100, y = 100;
f(i, n) {
makeRect(x, y, (i % 8) + 1, burstTimes[i]);
char c[3];
c[0] = 'P';
c[1] = '1' + i;
x += burstTimes[i] * 10;
}
int waitingTimes[n];
int turnaround = 0;
float avgWait = 0;
x = 100;
f(i, n) {
cout << "For process " << (i + 1) << "\n";
// cout<<"Turnaround time = ";
turnaround += burstTimes[i];
// cout<<turnaround<<endl;
cout << "waiting time : ";
waitingTimes[i] = (turnaround - burstTimes[i]);
cout << waitingTimes[i];
avgWait += waitingTimes[i];
cout << endl << endl;
//gantt chart
makeRect(x, y, (i % 8) + 3, burstTimes[i]);
char c[100];
intToString(waitingTimes[i], c);
x += burstTimes[i] * 10;
}
avgWait = avgWait / n;
cout << "Avg wait time: " << avgWait << endl;
return 0;
if (temp[i] > 0)
{
comp = false;
if (temp[i] > quantum)
{
t += quantum;
temp[i] -= quantum;
}
else
{
t = t + temp[i];
wt[i] = t - a[i];
temp[i] = 0;
}
}
}
if (comp)
{
break;
}
}
float turn = 0;
cout << "Waiting times are\n";
float wait = 0;
for (int i = 0; i < n; i++)
{
cout << wt[i] << " ";
wait += float(wt[i]);
turn += (float(wt[i]) + float(a[i]));
}
cout << endl;
cout << "Turnaround times are\n";
for (int i = 0; i < n; i++)
{
cout << wt[i] + a[i] << " ";
}
cout << endl;
cout << "Average waiting time is " << wait / n << " s" <<
endl;
cout << "Average turnaround time is " << turn / n << " s"
<< endl;
return 0;
}
struct process
{
char name;
int AT,BT,WT,TAT,RT,CT;
}Q1[10],Q2[10],Q3[10];/*Three queues*/
int n;
void sortByArrival()
{
struct process temp;
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(Q1[i].AT>Q1[j].AT)
{
temp=Q1[i];
Q1[i]=Q1[j];
Q1[j]=temp;
}
}
}
}
int main()
{
int i,j,k=0,r=0,time=0,tq1=5,tq2=8,flag=0;
char c;
printf("Enter no of processes:");
scanf("%d",&n);
for(i=0,c='A';i<n;i++,c++)
{
Q1[i].name=c;
printf("\nEnter the arrival time and burst time of
process %c: ",Q1[i].name);
scanf("%d%d",&Q1[i].AT,&Q1[i].BT);
Q1[i].RT=Q1[i].BT;/*save burst time in remaining
time for each process*/
}
sortByArrival();
time=Q1[0].AT;
printf("Process in first queue following RR with qt=5");
printf("\nProcess\t\tRT\t\tWT\t\tTAT\t\t");
for(i=0;i<n;i++)
{
if(Q1[i].RT<=tq1)
{
time+=Q1[i].RT;/*from arrival time of first process to
completion of this process*/
Q1[i].RT=0;
Q1[i].WT=time-Q1[i].AT-Q1[i].BT;/*amount of time
process has been waiting in the first queue*/
Q1[i].TAT=time-Q1[i].AT;/*amount of time to execute
the process*/
printf("\n%c\t\t%d\t\t%d\t\t
%d",Q1[i].name,Q1[i].BT,Q1[i].WT,Q1[i].TAT);
}
else/*process moves to queue 2 with qt=8*/
{
Q2[k].WT=time;
time+=tq1;
Q1[i].RT-=tq1;
Q2[k].BT=Q1[i].RT;
Q2[k].RT=Q2[k].BT;
Q2[k].name=Q1[i].name;
k=k+1;
flag=1;
}
}
if(flag==1)
{printf("\nProcess in second queue following RR with qt=8");
printf("\nProcess\t\tRT\t\tWT\t\tTAT\t\t");
}for(i=0;i<k;i++)
{
if(Q2[i].RT<=tq2)
{
time+=Q2[i].RT;/*from arrival time of first process
+BT of this process*/
Q2[i].RT=0;
Q2[i].WT=time-tq1-Q2[i].BT;/*amount of time process
has been waiting in the ready queue*/
Q2[i].TAT=time-Q2[i].AT;/*amount of time to execute
the process*/
printf("\n%c\t\t%d\t\t%d\t\t
%d",Q2[i].name,Q2[i].BT,Q2[i].WT,Q2[i].TAT);
}
else/*process moves to queue 3 with FCFS*/
{
Q3[r].AT=time;
time+=tq2;
Q2[i].RT-=tq2;
Q3[r].BT=Q2[i].RT;
Q3[r].RT=Q3[r].BT;
Q3[r].name=Q2[i].name;
r=r+1;
flag=2;
}
}
{if(flag==2)
printf("\nProcess in third queue following FCFS ");
}
for(i=0;i<r;i++)
{
if(i==0)
Q3[i].CT=Q3[i].BT+time-tq1-tq2;
else
Q3[i].CT=Q3[i-1].CT+Q3[i].BT;
for(i=0;i<r;i++)
{
Q3[i].TAT=Q3[i].CT;
Q3[i].WT=Q3[i].TAT-Q3[i].BT;
printf("\n%c\t\t%d\t\t%d\t\t%d\t\
t",Q3[i].name,Q3[i].BT,Q3[i].WT,Q3[i].TAT);
}
Output:
FCFS:
SJF:
Round Robin:
Output:
Question 5: Deadlock Detection and Deadlock
Avoidance Algorithm.
// total safe-sequences
int total = 0;
return flag;
}
safe.push_back(i);
// find safe sequence by taking process i
safe_sequence(marked, allocated, max, need,
available, safe);
safe.pop_back();
total++;
for (int i = 0; i < P; i++) {
// Driver Code
int main()
{
cout << "\nThere are total " << total << " safe-
sequences" << endl;
return 0;
}
Output:
//Deadlock Detection
#include <bits/stdc++.h>
using namespace std;
int arrmax[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input()
{
int i, j;
cout << "Enter the no of Processes\t";
cin >> n;
cout << "Enter the no of resource instances\t";
cin >> r;
cout << "Enter the Max Matrix\n";
for (i = 0; i < n; i++)
{
for (j = 0; j < r; j++)
{
cin >> arrmax[i][j];
}
}
cout << "Enter the Allocation Matrix\n";
for (i = 0; i < n; i++)
{
for (j = 0; j < r; j++)
{
cin >> alloc[i][j];
}
}
cout << "Enter the available Resources\n";
for (j = 0; j < r; j++)
{
cin >> avail[j];
}
}
void show()
{
int i, j;
cout << "Process\t Allocation\t Max\t Available\t";
for (i = 0; i < n; i++)
{
cout << "\nP" << i + 1 << "\t ";
for (j = 0; j < r; j++)
{
cout << alloc[i][j] << " ";
}
cout << "\t\t";
for (j = 0; j < r; j++)
{
cout << arrmax[i][j] << " ";
}
cout << "\t ";
if (i == 0)
{
for (j = 0; j < r; j++)
cout << avail[j] << " ";
}
}
}
void cal()
{
int finish[100], temp, need[100][100], flag = 1, k, c1 =
0;
int dead[100];
int safe[100];
int i, j;
for (i = 0; i < n; i++)
{
finish[i] = 0;
}
//find need matrix
for (i = 0; i < n; i++)
{
for (j = 0; j < r; j++)
{
need[i][j] = arrmax[i][j] - alloc[i][j];
}
}
while (flag)
{
flag = 0;
for (i = 0; i < n; i++)
{
int c = 0;
for (j = 0; j < r; j++)
{
if ((finish[i] == 0) && (need[i][j] <=
avail[j]))
{
c++;
if (c == r)
{
for (k = 0; k < r; k++)
{
avail[k] += alloc[i][j];
finish[i] = 1;
flag = 1;
}
//cout<<"\nP%d",i;
if (finish[i] == 1)
{
i = n;
}
}
}
}
}
}
j = 0;
flag = 0;
for (i = 0; i < n; i++)
{
if (finish[i] == 0)
{
dead[j] = i;
j++;
flag = 1;
}
}
if (flag == 1)
{
cout << "\n\nSystem is in Deadlock and the Deadlock
process are\n";
for (i = 0; i < n; i++)
{
cout << "P" << dead[i] << "\t";
}
}
else
{
cout << "\nNo Deadlock Occur";
}
}
int main()
{
int i, j;
cout << "**** Deadlock Detection Algorithm ****\n";
input();
show();
cal();
return 0;
}
Output:
// Driver Method
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
return 0 ;
}
Output:
//Worst Fit:
// C++ implementation of worst - Fit algorithm
#include<bits/stdc++.h>
using namespace std;
int n)
{
// Stores block id of the block allocated to a
// process
int allocation[n];
// Driver code
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
return 0 ;
}
Output:
//First Fit:
// C++ implementation of First - Fit algorithm
#include<bits/stdc++.h>
using namespace std;
break;
}
}
}
// Driver code
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
return 0 ;
}
Output:
#define FRAMES_NUMBER 3
// If Not Found
if (!isFound)
{
// 2) Find A Free Frame
bool hasFreeFrame = false;
for (int i = 0; i < FRAMES_NUMBER; i++)
if (frames[i] == -1)
{
hasFreeFrame = true;
frames[i] = pages[pageIndex];
paeFaults++;
// Printing
cout << pages[pageIndex] << "\t\t";
for (int f = 0; f < FRAMES_NUMBER; f++)
cout << frames[f] << "\t";
cout << endl;
break;
}
frames[victim] = pages[pageIndex];
paeFaults++;
// Printing
cout << pages[pageIndex] << "\t\t";
for (int f = 0; f < FRAMES_NUMBER; f++)
cout << frames[f] << "\t";
cout << endl;
}
}
}
return paeFaults;
}
getchar();
return 0;
}
Output:
//MRU:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int no_of_frames, no_of_pages;
printf("Enter the no of frames:\n");
scanf("%d", &no_of_frames);
printf("Enter the no of pages:\n");
scanf("%d", &no_of_pages);
printf("Enter the pageString\n");
int *pageString;
pageString = (int *)calloc(no_of_pages, sizeof(int));
for (int i = 0; i < no_of_pages; i++)
{
scanf("%d", &pageString[i]);
}
int *frames;
frames = (int *)calloc(no_of_frames, sizeof(int));
for (int i = 0; i < no_of_frames; i++)
{
frames[i] = -1;
}
int index = 0;
int no_of_page_faults = 0;
int no_of_page_hits = 0;
int idx;
int count = 0;
for (int i = 0; i < no_of_pages; i++)
{
if (count < no_of_frames)
{
idx = find(no_of_frames, frames, pageString[i]);
if (idx != -1)
{
no_of_page_hits++;
printf("Page Hit : Succesfully found Page %d
at %d Frame\n", pageString[i], idx + 1);
}
else
{
frames[count] = pageString[i];
printf("Page Miss : Storing %d Page no in %d
Frame:\n", pageString[i], count + 1);
count++;
no_of_page_faults++;
}
}
else
{
idx = find(no_of_frames, frames, pageString[i]);
if (idx != -1)
{
no_of_page_hits++;
printf("Page Hit : Succesfully found Page %d
at %d Frame\n", pageString[i], idx + 1);
}
else
{
index = find_MRU(pageString, i, no_of_pages,
no_of_frames, frames);
printf("Page Miss : Replacing %d Frame Page
with %d Page no:\n", index + 1, pageString[i]);
no_of_page_faults++;
frames[index] = pageString[i];
}
}
}
printf("The total number of page faults are : %d\n",
no_of_page_faults);
printf("The total number of page hits are : %d\n",
no_of_page_hits);
return 0;
}
Output:
//Second Chance:
// CPP program to find largest in an array
// without conditional/bitwise/ternary/ operators
// and without library functions.
#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;
{
int i;
if(arr[i] == x)
{
// Mark that the page deserves a second
chance
second_chance[i] = true;
string str[100];
string word = "";
for (auto x : reference_string)
{
if (x == ' ')
{
str[l]=word;
word = "";
l++;
}
else
{
word = word + x;
}
}
str[l] = word;
l++;
// l=the length of array
for(i = 0; i < l; i++)
{
x = stoi(str[i]);
// Driver code
int main()
{
string reference_string = "";
int frames = 0;
// Test 1:
reference_string = "0 4 1 4 2 4 3 4 2 4 0 4 1 4 2 4
3 4";
frames = 3;
// Output is 9
printHitsAndFaults(reference_string,frames);
// Test 2:
reference_string = "2 5 10 1 2 2 6 9 1 2 10 2 6 1 2
1 6 9 5 1";
frames = 4;
// Output is 11
printHitsAndFaults(reference_string,frames);
return 0;
}
Output:
//LFU:
// C++ program for LFU cache implementation
#include <bits/stdc++.h>
using namespace std;
if (n == v.size()) {
m.erase(v[0].first);
cout << "Cache block " << v[0].first
<< " removed.\n";
v[0] = v[--n];
heapify(v, m, 0, n);
}
v[n++] = make_pair(value, 1);
m.insert(make_pair(value, n - 1));
int i = n - 1;
Output:
//LRU Algo
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,i,j,k,hit=0;
cout<<"Enter number of frames\n";
cin>>n;
cout<<"Enter number of processes\n";
cin>>m;
vector<int> p(m);
vector<int> hi(m);
cout<<"Enter processes\n";
for(i=0;i<m;i++){
cin>>p[i];
}
vector<vector<int>> a(n);
for(i=0;i<n;i++){
a[i]=vector<int>(m,-1);
}
map <int, int> mp;
for(i=0;i<m;i++){
vector<pair<int,int>> c;
for(auto q: mp){
c.push_back({q.second,q.first});
}
sort(c.begin(),c.end());
bool hasrun=false;
for(j=0;j<n;j++){
if(a[j][i]==p[i]){
hit++;
hi[i]=1;
mp[p[i]]=1;
hasrun=true;
break;
}
if(a[j][i]==-1){
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
hasrun=true;
break;
}
}
if(j==n||hasrun==false){
for(j=0;j<n;j++){
if(a[j][i]==c[c.size()-1].second){
mp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
break;
}
}
}
for(auto q:mp){
if(q.first!=p[i]){
mp[q.first]++;
}
}
}
cout<<"Process ";
for(i=0;i<m;i++){
cout<<p[i]<<" ";
}
cout<<'\n';
for(i=0;i<n;i++){
cout<<"Frame "<<i<<" ";
for(j=0;j<m;j++){
if(a[i][j]==-1)
cout<<"E ";
else
cout<<a[i][j]<<" ";
}
cout<<'\n';
}
for(i=0;i<m;i++){
if(hi[i]==0)
cout<<" ";
else
cout<<hi[i]<<" ";
}
cout<<"\n";
cout<<"Hit "<<hit<<'\n'<<"Page Fault "<<m-hit<<'\n';
return 0;
}
//FIFO Algo
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,i,j,k,hit=0;
cout<<"Enter number of frames\n";
cin>>n;
cout<<"Enter number of processes\n";
cin>>m;
vector<int> p(m);
vector<int> hi(m);
cout<<"Enter processes\n";
for(i=0;i<m;i++){
cin>>p[i];
}
vector<vector<int>> a(n);
for(i=0;i<n;i++){
a[i]=vector<int>(m,-1);
}
map <int, int> mp;
for(i=0;i<m;i++){
vector<pair<int,int>> c;
for(auto q: mp){
c.push_back({q.second,q.first});
}
sort(c.begin(),c.end());
bool hasrun=false;
for(j=0;j<n;j++){
if(a[j][i]==p[i]){
hit++;
hi[i]=1;
mp[p[i]]++;
hasrun=true;
break;
}
if(a[j][i]==-1){
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
hasrun=true;
break;
}
}
if(j==n||hasrun==false){
for(j=0;j<n;j++){
if(a[j][i]==c[c.size()-1].second){
mp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
break;
}
}
}
for(auto q:mp){
if(q.first!=p[i]){
mp[q.first]++;
}
}
}
cout<<"Process ";
for(i=0;i<m;i++){
cout<<p[i]<<" ";
}
cout<<'\n';
for(i=0;i<n;i++){
cout<<"Frame "<<i<<" ";
for(j=0;j<m;j++){
if(a[i][j]==-1)
cout<<"E ";
else
cout<<a[i][j]<<" ";
}
cout<<'\n';
}
for(i=0;i<m;i++){
if(hi[i]==0)
cout<<" ";
else
cout<<hi[i]<<" ";
}
cout<<"\n";
cout<<"Hit "<<hit<<'\n'<<"Page Fault "<<m-hit<<'\n';
return 0;
}
{
int i;
if(arr[i] == x)
{
second_chance[i] = true;
return true;
}
}
return false;
if(!second_chance[pointer])
{
arr[pointer] = x;
second_chance[pointer] = false;
pointer = 0;
pf = 0;
int arr[frames];
string str[100];
string word = "";
for (auto x : reference_string)
{
if (x == ' ')
{
str[l]=word;
word = "";
l++;
}
else
{
word = word + x;
}
}
str[l] = word;
l++;
if(!findAndUpdate(x,arr,second_chance,frames))
{
pointer = replaceAndUpdate(x,arr,
second_chance,frames,pointer);
pf++;
}
}
cout << "Total page faults were " << pf << "\n";
}
// Driver code
int main()
{
string reference_string = "";
int frames = 0;
// Test 1:
reference_string = "0 4 1 4 2 4 3 4 2 4 0 4 1 4 2 4 3 4";
frames = 3;
// Output is 9
printHitsAndFaults(reference_string,frames);
// Test 2:
reference_string = "2 5 10 1 2 2 6 9 1 2 10 2 6 1 2 1 6 9 5
1";
frames = 4;
// Output is 12
printHitsAndFaults(reference_string,frames);
return 0;
}
Output:
LRU:
FIFO:
int size = 8;
void FCFS(int arr[], int head)
{
int seek_count = 0;
int distance, cur_track;
seek_count += distance;
head = cur_track;
}
// Driver code
int main()
{
// request array
int arr[size] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int head = 50;
FCFS(arr, head);
return 0;
}
int diff[n][2] = { { 0, 0 } };
int seekcount = 0;
seekcount += diff[index][0];
head = request[index];
}
seeksequence[n] = head;
// Driver code
int main()
{
int n = 8;
int proc[n] = { 176, 79, 34, 60, 92, 11, 41, 114 };
return 0;
}
int size = 8;
int disk_size = 200;
if (direction == "left")
left.push_back(0);
else if (direction == "right")
right.push_back(disk_size - 1);
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);
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);
seek_count += distance;
head = cur_track;
}
direction = "left";
}
}
// Driver code
int main()
{
// request array
int arr[size] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
string direction = "left";
return 0;
}
int size = 8;
int disk_size = 200;
left.push_back(0);
right.push_back(disk_size - 1);
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end());
seek_sequence.push_back(cur_track);
seek_count += distance;
head = cur_track;
}
head = 0;
seek_count += (disk_size - 1);
seek_sequence.push_back(cur_track);
seek_count += distance;
head = cur_track;
}
// Driver code
int main()
{
// request array
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;
}
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);
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);
seek_count += distance;
head = cur_track;
}
direction = "left";
}
}
// Driver code
int main()
{
// request array
int arr[size] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
string direction = "right";
return 0;
}
//C-LOOK Algo
#include <bits/stdc++.h>
using namespace std;
int size = 8;
int disk_size = 200;
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end());
seek_sequence.push_back(cur_track);
seek_count += distance;
head = cur_track;
}
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
// Driver code
int main()
{
// Request array
int arr[size] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
cout << "Initial position of head: " << head << endl;
CLOOK(arr, head);
return 0;
}
Output:
FCFS:
SSTF:
SCAN:
C-SCAN:
LOOK:
C-LOOK: