0% found this document useful (0 votes)
47 views1 page

Best Fit

Os lab

Uploaded by

Jayaram
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)
47 views1 page

Best Fit

Os lab

Uploaded by

Jayaram
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/ 1

6.

Write a C program to simulate the following contiguous memory


allocation techniques
B) Best Fit

#include <stdio.h>
#define MAX_BLOCKS 100
struct MemoryBlock {
int block_id;
int size;
int allocated;
};
void bestFit(struct MemoryBlock blocks[], int m, int processSize)
{
int bestIdx = -1;
for (int i = 0; i < m; i++)
{
if (blocks[i].allocated == 0 && blocks[i].size >= processSize)
{
if (bestIdx == -1 || blocks[i].size < blocks[bestIdx].size)
{
bestIdx = i;
}
}
}
if (bestIdx != -1)
{
blocks[bestIdx].allocated = 1;
printf("Process with size %d allocated to block %d\n", processSize, bestIdx + 1);
}
else
{
printf("Cannot allocate the process with size %d\n", processSize);
}
}
int main()
{
int m;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
struct MemoryBlock blocks[MAX_BLOCKS];
for (int i = 0; i < m; i++)
{
blocks[i].block_id = i + 1;
printf("Enter the size of memory block %d: ", i + 1);
scanf("%d", &blocks[i].size);
blocks[i].allocated = 0;
}
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
int processSize;
printf("Enter the size of process %d: ", i + 1);
scanf("%d", &processSize);
bestFit(blocks, m, processSize);
}
return 0;
}

You might also like