-
Notifications
You must be signed in to change notification settings - Fork 22
/
_16c.BestFit.c
69 lines (54 loc) · 1.64 KB
/
_16c.BestFit.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
#include <stdio.h>
void BestFit(int blockSize[], int blocks, int processSize[], int processes)
{
int allocation[processes];
for(int i = 0; i < processes; i++){
allocation[i] = -1;
}
for (int i=0; i < processes; i++)
{
int indexPlaced = -1;
for (int j=0; j < blocks; j++)
{
if (blockSize[j] >= processSize[i])
{
if (indexPlaced == -1)
indexPlaced = j;
else if (blockSize[j] < blockSize[indexPlaced])
indexPlaced = j;
}
}
if (indexPlaced != -1)
{
allocation[i] = indexPlaced;
blockSize[indexPlaced] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < processes; i++)
{
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main()
{
int blockSize[50],processSize[50],blocks,processes;
printf("Enter no of blocks:");
scanf("%d",&blocks);
printf("Enter no. of processes:");
scanf("%d",&processes);
printf("Enter the blocks:");
for(int i=0;i<blocks;i++){
scanf("%d",&blockSize[i]);
}
printf("Enter the processes:");
for(int i=0;i<processes;i++){
scanf("%d",&processSize[i]);
}
BestFit(blockSize, blocks, processSize, processes);
return 0 ;
}