-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
112 lines (107 loc) · 3.63 KB
/
main.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int SizeOfPartition,SizeOfProcesses,Policy;
cout<<"Please enter the number of Partition !"<<endl;
cin>>SizeOfPartition;
int Partition[SizeOfPartition];
int temp[SizeOfPartition];
for(int i=1; i<=SizeOfPartition; i++)
{
cout<<"Enter size of Partition"<<i<<" :";
cin>>Partition[i];
}
int Allocator[SizeOfPartition];
for( int i = 1 ; i<=SizeOfPartition; i++)
{
Allocator[i]=0;
}
cout<<"please enter the number of processes !"<<endl;
cin>>SizeOfProcesses;
int Processes[SizeOfProcesses];
for(int i=1; i<=SizeOfProcesses; i++)
{
cout<<"Enter size of processes"<<i<<" :";
cin>>Processes[i];
}
cout<<"Select the policy you want to apply :"<<endl;
cout<<"1.First fit"<<endl<<"2.Worst fit"<<endl<<"3.Best fit"<<endl;
cin>>Policy;
if(Policy==1)
{
for( int i = 1 ; i <=SizeOfProcesses; i++)
{
for( int j = 1; j<=SizeOfPartition; j++)
{
if(Processes[i] <= Partition[j] )
{
cout<<"Partition "<<j<<" ("<<Partition[j]<<" KB"<<")"<<" = "<<"Process "<<i<<" ( "<<Processes[i]<<" KB"<<" )"<<",";
cout<<"Rest of Partition "<<j<<" = "<< Partition[j]-Processes[i]<<" KB "<<endl;
Allocator[j]=1;
Partition[j]-=Processes[i];
break;
}
}
}
for( int k=1 ; k<=SizeOfPartition; k++)
{
if(Allocator[k]==0)
cout<<"Partition "<<k<<"( "<<Partition[k]<<" KB"<<")"<<" = "<<"Empty"<<endl;
}
}
else if (Policy == 2 )
{
for( int i = 1 ; i <=SizeOfProcesses; i++)
{
int Max=-1;
for( int j = 1; j<=SizeOfPartition; j++)
{
if (Partition[j] >= Processes[i])
{
if(Max==-1)
Max=j;
else if (Partition[Max] < Partition[j])
Max=j;
}
}
cout<<"Partition "<<Max<<" ("<<Partition[Max]<<" KB"<<")"<<" = "<<"Process "<<i<<" ( "<<Processes[i]<<" KB"<<" )"<<",";
cout<<"Rest of Partition "<<Max<<" = "<< Partition[Max]-Processes[i]<<" KB "<<endl;
Allocator[Max]=1;
Partition[Max]-=Processes[i];
}
for(int k = 1 ; k<=SizeOfPartition; k++)
{
if ( Allocator[k]==0)
cout<<"Partition "<<k<<"( "<<Partition[k]<<" KB"<<")"<<" = "<<"Empty"<<endl;
}
}
else if (Policy==3)
{
for( int i = 1 ; i <=SizeOfProcesses; i++)
{
int BestIndex=-1;
for( int j = 1 ; j <=SizeOfPartition; j++)
{
if (Partition[j] >= Processes[i])
{
if(BestIndex==-1)
BestIndex=j;
else if (Partition[BestIndex] > Partition[j])
BestIndex=j;
}
}
cout<<"Partition "<<BestIndex<<" ("<<Partition[BestIndex]<<" KB"<<")"<<" = "<<"Process "<<i<<" ( "<<Processes[i]<<" KB"<<" )"<<",";
cout<<"Rest of Partition "<<BestIndex<<" = "<< Partition[BestIndex]-Processes[i]<<" KB "<<endl;
Allocator[BestIndex]=1;
Partition[BestIndex]-=Processes[i];
}
for(int k = 1 ; k<=SizeOfPartition; k++)
{
if ( Allocator[k]==0)
cout<<"Partition "<<k<<"( "<<Partition[k]<<" KB"<<")"<<" = "<<"Empty"<<endl;
}
}
return 0;
}