forked from iedcbootcampcec/letshack-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbestfit.c
70 lines (65 loc) · 1.62 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
60
61
62
63
64
65
66
67
68
69
70
#include<stdio.h>
int find_min(int a[],int n)
{
int min=0,i;
for(i=0;i<n;i++)
{
if(a[i]<a[min])
{
min=i;
}
}
return min;
}
void main()
{
int process_size[10],block_size[10],temp[10]={0},alloc[10]={0},temp2[10]={0};
int i,j,bno,pno,count=0,k,flag=0,l;
printf("enter the number of blocks:\n");
scanf("%d",&bno);
printf("enter the number of processes:\n");
scanf("%d",&pno);
for(i=0;i<bno;i++)
{
printf("\nenter block%d size:",i);
scanf("%d",&block_size[i]);
}
for(i=0;i<pno;i++)
{
printf("\nenter process%d size:",i);
scanf("%d",&process_size[i]);
}
for(i=0;i<pno;i++)
{
count=0;
flag=0;
for(j=0;j<bno;j++)
{
if(process_size[i]<=block_size[j])
{
temp[count]=block_size[j];
temp2[count]=j;
count++;
flag++;
}
}
if(flag==0)
{
alloc[i]=-1;
}
else
{
k= find_min(temp,count);
alloc[i]=temp2[k];
l=temp2[k];
block_size[l]=block_size[l]-process_size[i];
}
}
printf("block no:-1 indicates no block has been allocated!\n");
printf("Process\t\t\tProcess size\t\t\tAllocated block no:\n");
for(i=0;i<pno;i++)
{
printf("%d\t\t\t%d\t\t\t%d",i,process_size[i],alloc[i]);
printf("\n");
}
}