7
University of Sialkot
Name: Muhammad Zaigham_113
Subject: OS LAB
Section: Yellow
Submitted To: Mam Faryal
First Fit Algorithm
#include <bits/stdc++.h>
using namespace std;
void firstFit ( int blockSize [ ], int m, int processSize [ ], int n )
{
int allocation [n];
memset ( allocation, -1, sizeof ( allocation ) ); // Initially no block is assigned to any
process
for ( int i = 0; i < n; i++ )
{
for ( int j = 0; j < m; j++ )
{
if ( blockSize [j] >= processSize [i] )
{
allocation [i] = j; // allocate block j to p [i] process
blockSize [j] - = processSize [i]; // Reduce available memory in this block.
break;
}
}
}
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 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] );
firstFit ( blockSize, m, processSize, n );
return 0;
}
Best Fit Algorithm
#include <bits/stdc++.h>
using namespace std;
void bestFit ( int blockSize [ ], int m, int processSize [ ], int n )
{
int allocation [n];
memset ( allocation, -1, sizeof ( allocation ) ); // Initially no block is assigned to any pro-
cess
for ( int i=0; i<n; i++ )
{
int bestIdx = -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 ) // If we could find a block for current process
{
allocation [i] = bestIdx; // allocate block j to p [i] process
blockSize [bestIdx] -= processSize [i]; // Reduce available memory in this block.
}
}
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 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] );
bestFit ( blockSize, m, processSize, n );
return 0 ;
}
Worst Fit Algorithm
#include <bits/stdc++.h>
using namespace std;
void worstFit ( int blockSize [ ], int m, int processSize [ ], int n )
{
int allocation [n];
memset ( allocation, -1, sizeof ( allocation ) ); // Initially no block is assigned to any process
for ( int i=0; i<n; i++ )
{
int wstIdx = -1;
for ( int j=0; j<m; j++ )
{
if ( blockSize [j] >= processSize [i] )
{
if ( wstIdx == -1 )
wstIdx = j;
else if ( blockSize [wstIdx] < blockSize [j] )
wstIdx = j;
}
}
// If we could find a block for current process
if ( wstIdx != -1 )
{
allocation [i] = wstIdx; // allocate block j to p [i] process
blockSize [wstIdx] -= processSize [i]; // Reduce available memory in this block.
}
}
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 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] );
worstFit ( blockSize, m, processSize, n );
return 0 ;
}
<-------------------------->