QUESTION DESCRIPTION
Write a program to implement First fit algorithm.The First Fit Memory Allocation Algorithm allocates the first free partition available in the memory that is sufficient enough to hold the process within the system.
Write a program to implement First fit algorithm.The First Fit Memory Allocation Algorithm allocates the first free partition available in the memory that is sufficient enough to hold the process within the system.
TEST CASE 1
INPUT
INPUT
3 9
20 30 20
11 10 7 3 4 2 5 6 4
OUTPUTFile Number Block Number File Size Block Size Fragment
0 0 11 20 9
1 1 10 30 20
2 2 7 20 13
3 0 3 20 13
4 0 4 20 13
5 0 2 20 13
6 0 5 20 13
7 0 6 20 13
8 0 4 20 13
TEST CASE 2
INPUT
INPUT
4 3
50 32 10
12 23 12
OUTPUTFile Number Block Number File Size Block Size Fragment
0 0 23 50 27
1 1 12 32 20
2 2 0 10 10
Code :
#include<stdio.h>
int main()
{
static int block_arr[10], file_arr[10];
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp;
scanf("%d", &number_of_blocks);
scanf("%d", &number_of_files);
for(m = 0; m < number_of_blocks; m++)
{
scanf("%d", &blocks[m]);
}
for(m = 0; m < number_of_files; m++)
{
scanf("%d", &files[m]);
}
for(m = 0; m < number_of_files; m++)
{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
file_arr[m] = n;
break;
}
}
}
fragments[m] = temp;
block_arr[file_arr[m]] = 1;
}
printf("\nFile Number Block Number File Size Block Size Fragment");
for(m = 0; m < number_of_files; m++)
{
printf("\n%d %d %d %d %d", m, file_arr[m], files[m], blocks[file_arr[m]], fragments[m]);
}
printf("\n");
return 0;
}