0% found this document useful (0 votes)
16 views14 pages

Practical5-7 Os

The document contains code for implementing three memory allocation algorithms - first fit, best fit, and worst fit. For first fit, it allocates processes to the first block that fits. For best fit, it allocates to the block with the smallest remaining space that fits. For worst fit, it allocates to the block with the largest remaining space that fits. The code takes block sizes and process sizes as input, performs allocation, and outputs the allocation details.

Uploaded by

Gaurav Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

Practical5-7 Os

The document contains code for implementing three memory allocation algorithms - first fit, best fit, and worst fit. For first fit, it allocates processes to the first block that fits. For best fit, it allocates to the block with the smallest remaining space that fits. For worst fit, it allocates to the block with the largest remaining space that fits. The code takes block sizes and process sizes as input, performs allocation, and outputs the allocation details.

Uploaded by

Gaurav Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PRACTICAL-5

AIM: Write a program for page replacement policy using


a) FIFO
CODE:
#include <iostream>

using namespace std;

int main()

{ intincomingStream[] = {7, 0, 1, 2, 0, 3, 0, 4, 2};

intpageFaults = 0;

int frames = 3;

int m, n, s, pages;

pages = sizeof(incomingStream)/sizeof(incomingStream[0]);

printf("Incoming \t Frame 1 \t Frame 2 \t Frame 3");

int temp[frames];

for(m = 0; m < frames; m++)

temp[m] = -1;}

for(m = 0; m < pages; m++)

{ s = 0;

for(n = 0; n < frames; n++)

{ if(incomingStream[m] == temp[n])

s++;

pageFaults--; }}

02415003120 IT 1 Diwakar Jha


pageFaults++;

if((pageFaults<= frames) && (s == 0))

temp[m] = incomingStream[m]; }

else if(s == 0)

temp[(pageFaults - 1) % frames] = incomingStream[m]; }

cout<< "\n";

cout<<incomingStream[m] << "\t\t\t";

for(n = 0; n < frames; n++)

{if(temp[n] != -1)

cout<< temp[n] << "\t\t\t";

else

cout << "- \t\t\t"; }}

cout<< "\n\nTotal Page Faults:\t" <<pageFaults;

cout<< "\nTotal Hits :\t" << pages - pageFaults;

return 0;}

OUTPUT:

02415003120 IT 1 Diwakar Jha


b)LRU

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 }

cout<<"\n\t Enter the No of frames:-";


cin>>nof;
int frame[nof],fcount[nof];

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]<<"-->";

02415003120 IT 1 Diwakar Jha


if(flag==0)
{
int min=0,k=0;

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

02415003120 IT 1 Diwakar Jha


C) OPTIMAL
#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]; //Array for Storing Reference String }

cout<<"\n\t Enter the No of frames:-";


cin>>nof;
int frame[nof],fcount[nof];

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)
{

02415003120 IT 1 Diwakar Jha


int max=0,k=0;

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;

02415003120 IT 1 Diwakar Jha


OUTPUT

02415003120 IT 1 Diwakar Jha


PRACTICAL-6
AIM :WAP to implement banker’s algorithm for deadlock avoidance.

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++)

02415003120 IT 1 Diwakar Jha


{for(int j=0;j<ed;j++)
if(i==seq[j])
{fl=1;
break; }

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;

02415003120 IT 1 Diwakar Jha


banker.getdata();
banker.safe_seq();

return 0;
}

OUTPUT:

02415003120 IT 1 Diwakar Jha


PRACTICAL-7
AIM: WAP to implement first fit, best fit and worst fit algorithm for
memory management.

a) First fit
SOURCE CODE:

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

voidFirst_Fit(intblock_size[], inttotal_blocks, intprocess_size[], inttotal_process) {


int allocation[total_process];
memset(allocation, -1, sizeof(allocation));
for (inti = 0; i<total_process; i++) {
for (int j = 0; j <total_blocks; j++) {
if (block_size[j] >= process_size[i]) {
allocation[i] = j;
block_size[j] -= process_size[i];
break; } } }

cout<< "\nProcess No.\tProcess Size\tBlock no.\n";


for (inti = 0; i<total_process; i++) {
cout<< " " << i+1 << "\t\t" <<process_size[i] << "\t\t";
if (allocation[i] != -1)
cout<< allocation[i] + 1;
else
cout<< "Not Allocated";
cout<<endl; }}

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]);

First_Fit(block_size, total_blocks, process_size, total_process);


return 0 ;
}

02415003120 IT 1 Diwakar Jha


OUTPUT

b)Best fit
SOURCE CODE:

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

voidbestFit(intblockSize[], int m, intprocessSize[], int n)


{ int allocation[n];
memset(allocation, -1, sizeof(allocation));
for (inti=0; i<n; i++)
{ intbestIdx = -1;
for (int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{ if (bestIdx == -1)
bestIdx = j;
else if (blockSize[bestIdx] >blockSize[j])
bestIdx = j; } }

if (bestIdx != -1)
{ allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];}}

cout<< "\nProcess No.\tProcess Size\tBlock no.\n";


for (inti = 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";

02415003120 IT 1 Diwakar Jha


cout<<endl; }}

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]);

bestFit(blockSize, m, processSize, n);

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])

02415003120 IT 1 Diwakar Jha


wstIdx = j; }}
if (wstIdx != -1)
{ allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i]; } }

cout<< "\nProcess No.\tProcess Size\tBlock no.\n";

for (inti = 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()
{ 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

02415003120 IT 1 Diwakar Jha

You might also like