-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbestfit.c
59 lines (52 loc) · 1.45 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
59
#include <stdio.h>
void main()
{
int blocknum, processnum;
printf("Enter the number of blocks: ");
scanf("%d", &blocknum);
printf("Enter the number of process: ");
scanf("%d", &processnum);
int blockarr[blocknum], processarr[processnum], sortarr[blocknum];
for (int i = 0; i < blocknum; i++)
{
printf("Enter the size of the memory blocks: ");
scanf("%d", &blockarr[i]);
sortarr[i] = blockarr[i];
}
for (int i = 0; i < processnum; i++)
{
printf("Enter the process: ");
scanf("%d", &processarr[i]);
}
for (int i = 0; i < blocknum; i++)
{
for (int j = 0; j < blocknum - i - 1; j++)
{
if (sortarr[j] > sortarr[j + 1])
{
int temp = sortarr[j];
sortarr[j] = sortarr[j + 1];
sortarr[j + 1] = temp;
}
}
}
for (int i = 0; i < processnum; i++)
{
int allocflag;
for (int j = 0; j < blocknum; j++)
{
allocflag = 0;
if (processarr[i] <= sortarr[j])
{
printf("process %d in block of size %d\n", i + 1, sortarr[j]);
sortarr[j] = 0;
allocflag = 1;
break;
}
}
if (allocflag == 0)
{
printf("process %d cannot be allocated\n", i + 1);
}
}
}