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

Worst Fit

The first_fit function allocates processes to memory blocks using a first-fit algorithm. It takes block sizes, number of blocks, process sizes, and number of processes as inputs. It finds the first block that fits each process, allocates the process to that block, and reduces the remaining block size. It then prints the allocation results.
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)
27 views1 page

Worst Fit

The first_fit function allocates processes to memory blocks using a first-fit algorithm. It takes block sizes, number of blocks, process sizes, and number of processes as inputs. It finds the first block that fits each process, allocates the process to that block, and reduces the remaining block size. It then prints the allocation results.
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

#include <stdio.

h>

void first_fit(int *blockSize, int n, int *processSize, int m)


{
int allocation[4] = {-1};

for (int i = 0; i < n; i++)


{
// Find the best fit block for current process
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)
{
// allocate block j to p[i] process
allocation[i] = wstIdx;

// Reduce available memory in this block.


blockSize[wstIdx] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < n; i++)
{
printf(" %i\t\t\t", i + 1);
printf("%i\t\t\t\t", processSize[i]);
if (allocation[i] != -1)
printf("%d", allocation[i] + 1);
else
printf("Not Allocated");
printf("\n");
}
}

int main()
{
int m = 5; // number of blocks in the memory
int n = 4; // number of processes in the input queue
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};

first_fit(blockSize, n, processSize, m);


}

You might also like