0% found this document useful (0 votes)
9 views8 pages

Os 8

The document contains implementations of three memory allocation algorithms: First Fit, Best Fit, and Worst Fit. Each algorithm includes code for allocating memory blocks to processes based on their sizes and displays the allocation results. The document provides a structured approach to memory management in operating systems.

Uploaded by

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

Os 8

The document contains implementations of three memory allocation algorithms: First Fit, Best Fit, and Worst Fit. Each algorithm includes code for allocating memory blocks to processes based on their sizes and displays the allocation results. The document provides a structured approach to memory management in operating systems.

Uploaded by

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

Experiment No: 8

First Fit Algorithm

#include<stdio.h>
void main()
{
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;

for(i = 0; i < 10; i++)


{
flags[i] = 0;
allocation[i] = -1;
}

printf("Enter no. of blocks: ");


scanf("%d", &bno);

printf("\nEnter size of each block: ");


for(i = 0; i < bno; i++)
scanf("%d", &bsize[i]);

printf("\nEnter no. of processes: ");


scanf("%d", &pno);

printf("\nEnter size of each process: ");


for(i = 0; i < pno; i++)
scanf("%d", &psize[i]);
for(i = 0; i < pno; i++) //allocation as per first fit
for(j = 0; j < bno; j++)
if(flags[j] == 0 && bsize[j] >= psize[i])
{
allocation[j] = i;
flags[j] = 1;
break;
}

//display allocation details


printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i = 0; i < bno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, bsize[i]);
if(flags[i] == 1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
else
printf("Not allocated");
}
}
Output:
Best Fit Algorithm
#include <stdio.h>
void main()
{
// declaration and initialization of variables
int fragment[20],b[20],p[20],i,j,nb,np,tem,low=9999;
static int barray[20],parray[20];

printf("Memory Management Scheme - Best Fit");

//input number of processes

printf("Enter the number of processes:");

scanf("%d",&np);

//input number of blocks

printf("\nEnter the number of blocks:");

scanf("%d",&nb);

//input number of blocks

printf("\nEnter the size of the blocks:-\n");


for(i=1;i<=nb;i++)
{

printf("Block no.%d:",i);

scanf("%d",&b[i]);
}

//input the size of process

printf("\nEnter the size of the processes :-\n");


for(i=1;i<=np;i++)
{

printf("Process no.%d:",i);

scanf("%d",&p[i]);
}

for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{

if(barray[j]!=1)
{
tem=b[j]-p[i];
if(tem>=0)
if(low>tem)
{
parray[i]=j;
low=tem;
}
}
}

fragment[i]=low;
barray[parray[i]]=1;
low=10000;
}

//Print the result

printf("\nProcess_number \tProcess_size\tBlock_number \tBlock_size\tFragment");


for(i=1;i<=np && parray[i]!=0;i++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}

Output:

Worst Fit Algorithm

#include &lt;stdio.h&gt;

int main()

// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k;

n = 5; // Number of processes

m = 3; // Number of resources

int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix

{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2

{ 2, 1, 1 }, // P3

{ 0, 0, 2 } }; // P4

int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix

{ 3, 2, 2 }, // P1

{ 9, 0, 2 }, // P2

{ 2, 2, 2 }, // P3

{ 4, 3, 3 } }; // P4

int avail[3] = { 3, 3, 2 }; // Available Resources

int f[n], ans[n], ind = 0;

for (k = 0; k &lt; n; k++) {

f[k] = 0;

int need[n][m];

for (i = 0; i &lt; n; i++) {

for (j = 0; j &lt; m; j++)

need[i][j] = max[i][j] - alloc[i][j];

int y = 0;

for (k = 0; k &lt; 5; k++) {

for (i = 0; i &lt; n; i++) {

if (f[i] == 0) {
int flag = 0;

for (j = 0; j &lt; m; j++) {

if (need[i][j] &gt; avail[j]){

flag = 1;

break;

if (flag == 0) {

ans[ind++] = i;

for (y = 0; y &lt; m; y++)

avail[y] += alloc[i][y];

f[i] = 1;

Operating System

printf(&quot;Following is the SAFE Sequence\n&quot;);

for (i = 0; i &lt; n - 1; i++)

printf(&quot; P%d -&gt;&quot;, ans[i]);

printf(&quot; P%d&quot;, ans[n - 1]);


return (0);

Output

You might also like