-
Notifications
You must be signed in to change notification settings - Fork 0
/
best_fit.c
85 lines (75 loc) · 2.49 KB
/
best_fit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* C implementation of Best Fit algorithm in
memory management. Best fit allocates the
process to a partition which is the smallest
sufficient partition among all the available
partitions. First we input memory blocks and
processes with sizes. Then we initalize all
memory blocks as free. In step three, we start
by picking each process and find the minimum
block size that can be assigned to the current
process (i.e. find min(blockSize[1],blockSize[2],
...blockSize[n]) > processSize[current]. If found,
then assign it to the current proces. If not, then
leave that process and keep checking for further
processes. The time complexity is O(n^2) because
there are two loops to process the memory blocks
and processes.The outer loop is used to iterate
through the processes and the inner to iterate
through the memory blocks. Space complexity is
O(n) since it requires an array of length n to
store the block allocation for each process. */
#include <stdio.h>
#include <stdlib.h>
// Function to allocate memory to blocks as per Best Fit algorithm
void bestFit(int blockSize[], int m, int processSize[], int n)
{
// Stores block id of the block allocated to a process
int allocation[n];
// Initially no block is assigned to any process
for (int i = 0; i < n; i++)
allocation[i] = -1;
// pick each process and find suitable blocks
// according to its size ad assign to it
for (int i = 0; i < n; i++)
{
// Find the best fit block for current process
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 we could find a block for current process
if (bestIdx != -1)
{
// allocate block j to p[i] process
allocation[i] = bestIdx;
// Reduce available memory in this block.
blockSize[bestIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf("%d\t\t%d\t\t",i+1,processSize[i]);
if(allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not allocated\n");
}
}
// Driver Method
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 ;
}