0% found this document useful (0 votes)
50 views

Operating System Assignment

The document contains C++ code implementing three memory allocation algorithms: first fit, best fit, and worst fit. First fit allocates processes to the first block that fits, best fit allocates to the smallest fitting block, and worst fit allocates to the largest fitting block. The code takes block sizes and process sizes as input, allocates processes using the specified algorithm, and outputs the allocation results.

Uploaded by

Unknown
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)
50 views

Operating System Assignment

The document contains C++ code implementing three memory allocation algorithms: first fit, best fit, and worst fit. First fit allocates processes to the first block that fits, best fit allocates to the smallest fitting block, and worst fit allocates to the largest fitting block. The code takes block sizes and process sizes as input, allocates processes using the specified algorithm, and outputs the allocation results.

Uploaded by

Unknown
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/ 5

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 ;
}

<-------------------------->

You might also like