Repository navigation
Expand file tree
/
Copy pathWorst_Fit.cpp
More file actions
114 lines (97 loc) · 2.9 KB
/
Copy pathWorst_Fit.cpp
File metadata and controls
114 lines (97 loc) · 2.9 KB
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
113
114
/* Shabbir Mahmood ID: 14015439 Part-3 Even Operating System Lab Worst Fit Algorithm Date: 05-04-2017*/
/*
5
400
200
300
500
100
5
100
900
300
400
200
*/
#include<bits/stdc++.h>
using namespace std;
struct Block
{
int block_id;
int block_size;
int internal_fragmentation;
int block_flag = 0;
};
struct File
{
int file_id;
int file_size;
int file_flag = 0;
};
bool compare(Block lhs, Block rhs) { return lhs.block_size > rhs.block_size; }
int main()
{
Block block[50];
File file[50];
int no_of_block, no_of_file, sum_file=0, sum_block=0;
cout<<"Enter the no. of Blocks: "; // Block
cin>>no_of_block;
cout<<"Enter Sizes of Each Block:"<<endl;
for(int i=1; i<=no_of_block; i++)
{
cout<<"Block "<<i<<" : ";
cin>>block[i].block_size;
block[i].block_id = i;
}
cout<<endl;
cout<<"Enter the no. of Files : "; // File
cin>>no_of_file;
cout<<"Enter Sizes of Each Files:"<<endl;
for(int i=1; i<=no_of_file; i++)
{
cout<<"File "<<i<<" : ";
cin>>file[i].file_size;
file[i].file_id = i;
}
sort(block+1, block+(no_of_block+1), compare);
cout<<endl;
cout<<" FileNO | FileSize | BlockNo | BlockSize | InternalFragmentation"<<endl;
cout<<" -------------------------------------------------------------------------"<<endl;
for(int i=1; i<=no_of_file; i++)
{
for(int j=1; j<=no_of_block; j++)
{
if(block[j].block_flag == 0 && file[i].file_size <= block[j].block_size)
{
block[j].block_flag = 1;
file[i].file_flag = 1;
block[j].internal_fragmentation = block[j].block_size - file[i].file_size;
printf(" %3d | %3d | %3d | %3d | %3d \n",file[i].file_id,file[i].file_size,block[j].block_id,block[j].block_size,block[j].internal_fragmentation);
break;
}
}
if(file[i].file_flag == 0)
{
printf(" %3d | %3d | XXX | XXX | XXX \n",file[i].file_id,file[i].file_size);
}
}
cout<<" -------------------------------------------------------------------------"<<endl;
for(int j=1; j<=no_of_block; j++)
{
if(block[j].block_flag == 0)
{
sum_block += block[j].block_size;
}
}
cout<<"Total Free Space is : "<<sum_block<<endl;
for(int i=1; i<=no_of_file; i++)
{
if(file[i].file_flag == 0 && file[i].file_size <= sum_block)
{
cout<<"External Fragmentation has Occurred for File: "<<file[i].file_id<<endl;
sum_file += file[i].file_size;
}
}
cout<<"Total External Fragmentation is : "<<sum_file<<endl;
return 0;
}