-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBestFit.c
58 lines (58 loc) · 1.29 KB
/
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
#include<stdio.h>
void main()
{
int bno, pno, i, j;
printf("Enter no. of blocks: ");
scanf("%d", &bno);
int bsize[bno];
printf("\nEnter size of each block: ");
for(i = 0; i < bno; i++){
scanf("%d", &bsize[i]);
}
printf("\nEnter no. of processes: ");
scanf("%d", &pno);
int psize[pno];
printf("\nEnter size of each process: ");
for(i = 0; i < pno; i++)
scanf("%d", &psize[i]);
int flags[bno],allocation[pno];
for(i = 0; i < bno; i++)
{
flags[i] = 0;
}
for(i=0;i<pno;i++){
allocation[i]=-1;
}
for (int i=0; i<pno; i++)
{
int bestIdx = -1;
for (int j=0; j<bno; j++)
{
if (bsize[j] >= psize[i])
{
if (bestIdx == -1)
bestIdx = j;
else if (bsize[bestIdx] > bsize[j])
bestIdx = j;
}
}
if (bestIdx != -1)
{
allocation[i] = bestIdx;
}
}
int diff=0;
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i = 0; i < bno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, bsize[i]);
if(flags[i] == 1){
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
diff=diff+(bsize[i]-psize[allocation[i]]);
}
else
printf("Not allocated");
}
printf("\nSize of memory wasted: ");
printf("%d",diff);
}