Practical5-7 Os
Practical5-7 Os
int main()
intpageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
int temp[frames];
temp[m] = -1;}
{ s = 0;
{ if(incomingStream[m] == temp[n])
s++;
pageFaults--; }}
temp[m] = incomingStream[m]; }
else if(s == 0)
cout<< "\n";
{if(temp[n] != -1)
else
return 0;}
OUTPUT:
CODE:
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
intnop,nof,page[20],i,count=0;
cout<<"\n\tEnter the No. of Pages:";
cin>>nop; //Store the no of Pages
cout<<"\n\t Enter the Reference String:";
for(i=0;i<nop;i++)
{
cout<<"\t";
cin>>page[i]; //Store the pages }
for(i=0;i<nof;i++)
{frame[i]=-1; //Store the frames
fcount[i]=0; //Track when the page is last used }
i=0;
while(i<nop)
{
int j=0,flag=0;
while(j<nof)
{
if(page[i]==frame[j]){
flag=1;
fcount[j]=i+1; }
j++; }
j=0;
cout<<"\n\t**************************************\n";
cout<<"\t"<<page[i]<<"-->";
while(k<nof-1)
{
if(fcount[min]>fcount[k+1])
min=k+1;
k++; }
frame[min]=page[i];
fcount[min]=i+1;
count++;
while(j<nof)
{
cout<<"\t|"<<frame[j]<<"|";
j++; } }
i++; }
cout<<"\n\t**************************************\n";
cout<<"\n\tPage Fault is:"<<count;
getch();
return 0;}
OUTPUT
int main()
{
intnop,nof,page[20],i,count=0;
cout<<"\n\tEnter the No. of Pages:";
cin>>nop; //Store the no of pages
cout<<"\n\t Enter the Reference String:";
for(i=0;i<nop;i++)
{cout<<"\t";
cin>>page[i]; //Array for Storing Reference String }
for(i=0;i<nof;i++)
{frame[i]=-1; //Frame Array
fcount[i]=0; // Track the next Availability of frames }
i=0;
while(i<nop)
{
int j=0,flag=0;
while(j<nof)
{
if(page[i]==frame[j]){ // Checking Whether the Page is Already in frame or not
flag=1; }
j++; }
j=0;
cout<<"\n\t**************************************\n";
cout<<"\t"<<page[i]<<"-->";
if(flag==0)
{
if(i>=nof)
{
while(k<nof)
{
intdist=0,j1=i+1;
while(j1<nop)
{
if(frame[k]!=page[j1])
dist++;
else {
break; }
j1++; }
fcount[k]=dist;
k+ }
k=0;
while(k<nof-1)
{
if(fcount[max]<fcount[k+1]) //Finding out the maxximum distance
max=k+1;
k++; }
frame[max]=page[i]; }
else {
frame[i%nof]=page[i]; }
count++;
while(j<nof){
cout<<"\t|"<<frame[j]<<"|";
j++; } }
i+ }
cout<<"\n\t**************************************\n";
cout<<"\n\tPage Fault is:"<<count;
getch();
return 0;
SOURCE CODE:
#include<iostream>
using namespace std;
classbankers_alg
{intp,r,seq[20],al[20][20],rn[20][20],avl[20],ed;
intcom_pr();
public:
voidgetdata();
voidsafe_seq();
};
voidbankers_alg::getdata()
{cout<<"Enter number of processes : ";
cin>>p;
cout<<"Enter number of resources : ";
cin>>r;
cout<<"Enter number of instances available of each resource : ";
for(inti=0;i<r;i++)
cin>>avl[i];
for(inti=0;i<p;i++)
{cout<<"Enter no. of instances of resources allocated to process p["<<i<<"] : ";
for(int y=0;y<r;y++)
cin>>al[i][y]; }
for(inti=0;i<p;i++)
{cout<<"Enter max no. of instances of resources process p["<<i<<"] needs : ";
for(int y=0;y<r;y++)
cin>>rn[i][y];
}}
intbankers_alg::com_pr()
{int flag=0,fl=0;
for(inti=0;i<p;i++)
if(fl!=1)
{for(int j=0;j<r;j++)
{if(avl[j]-rn[i][j]+al[i][j]<0)
{
flag=1;
break; }}
if(flag==0)
returni;
flag=0; }
fl=0; }
return -1;}
voidbankers_alg::safe_seq()
{inttemp,flag=0;
ed=0;
for(inti=0;i<p;i++)
{temp=com_pr();
if(temp!=-1)
{for(int y=0;y<r;y++)
avl[y]+=al[temp][y];
seq[ed++]=temp; }
else
{cout<<"\nSystem is in unsafe state ";
flag=1;
break; }}
if(flag!=1)
{cout<<"Safe sequence is : ";
for(inti=0;i<ed;i++)
cout<<"p["<<seq[i]<<"] "; }}
int main()
{
bankers_alg banker;
return 0;
}
OUTPUT:
a) First fit
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
int main() {
intblock_size[] = {300, 50, 200, 350, 70};
intprocess_size[] = {200, 47, 212, 426, 10};
inttotal_blocks = sizeof(block_size) / sizeof(block_size[0]);
inttotal_process = sizeof(process_size) / sizeof(process_size[0]);
b)Best fit
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
if (bestIdx != -1)
{ allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];}}
int main()
{ intblockSize[] = {100, 500, 200, 300, 600};
intprocessSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
return 0 ;
}
OUTPUT
c) Worst fit
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
voidworstFit(intblockSize[], int m, intprocessSize[], int n)
{ int allocation[n];
memset(allocation, -1, sizeof(allocation));
for (inti=0; i<n; i++)
{intwstIdx = -1;
for (int j=0; j<m; j++)
{ if (blockSize[j] >= processSize[i])
{if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] <blockSize[j])
int main()
{ intblockSize[] = {100, 500, 200, 300, 600};
intprocessSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
worstFit(blockSize, m, processSize, n);
return 0 ; }
OUTPUT