-
Notifications
You must be signed in to change notification settings - Fork 5
/
best fit
53 lines (53 loc) · 1.07 KB
/
best fit
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
#include<iostream.h>
#include<conio.h>
void main()
{
int mem[10],processes[10],n1,n2,flags[10],allocation[10];
int i,j,smallest;
clrscr();
for(i=0;i < 10;i++)
{
flags[i]=0;
allocation[i]=-1;
}
cout<<"\n Enter no.of memory partitions:";
cin>>n1;
cout<<"\n Enter size of each partiton:";
for(i=0;i < n1;i++)
cin>>mem[i];
cout<<"\n enter no.of processes:";
cin>>n2;
cout<<"\n Enter size of each process:";
for(i=0;i < n2;i++)
cin>>processes[i];
//allocation as per best fit
for(i=0;i < n2;i++)
{
smallest=-1;
for(j=0;j < n1;j++)
if(flags[j]==0 && mem[j] >= processes[i])
{
smallest=j;
break;
}
for(;j < n1;j++)
if(flags[j]==0 && mem[j] >= processes[i] && mem[j] < mem[smallest])
smallest=j;
if(smallest!=-1)
{
allocation[smallest]=i;
flags[smallest]=1;
}
}
//display allocation details
cout<<"\n partition size process no. size";
for(i=0;i < n1;i++)
{
cout<<"\n"<<i<<" "<<mem[i]<<" ";
if(flags[i]==1)
cout<<allocation[i]<<" "<<processes[allocation[i]];
else
cout<<"-----";
}
getch();
}