-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIRST,BEST,WORST FIT
150 lines (121 loc) · 4.85 KB
/
FIRST,BEST,WORST 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
struct memory_block {
int start_address;
int end_address;
int size;
int allocated;
};
// Function to allocate memory using First Fit technique
void allocate_first_fit(struct memory_block memory_blocks[], int n, int process_size, int process_no) {
int i;
for (i = 0; i < n; ++i) {
if (memory_blocks[i].allocated == 0 && memory_blocks[i].size >= process_size) {
// Allocate memory
memory_blocks[i].allocated = 1;
memory_blocks[i].size = process_size;
printf("| %-12d| %-14d| %-9d|\n", process_no, process_size, i+1);
return;
}
}
// No suitable memory block available for allocation
printf("| %-12d| %-14d| %-9s|\n", process_no, process_size, "Not found");
}
// Function to allocate memory using Best Fit technique
void allocate_best_fit(struct memory_block memory_blocks[], int n, int process_size, int process_no) {
int i, best_index = -1, best_size = 2147483647;
for (i = 0; i < n; ++i) {
if (memory_blocks[i].allocated == 0 && memory_blocks[i].size >= process_size && memory_blocks[i].size < best_size) {
// Choose the block with the smallest remaining size after allocation
best_index = i;
best_size = memory_blocks[i].size;
}
}
if (best_index == -1) {
// No suitable memory block available for allocation
printf("| %-12d| %-14d| %-9s|\n", process_no, process_size, "Not found");
return;
}
// Allocate memory
memory_blocks[best_index].allocated = 1;
memory_blocks[best_index].size = process_size;
printf("| %-12d| %-14d| %-9d|\n", process_no, process_size, best_index+1);
}
// Function to allocate memory using Worst Fit technique
void allocate_worst_fit(struct memory_block memory_blocks[], int n, int process_size, int process_no) {
int i, worst_index = -1, worst_size = -1;
for (i = 0; i < n; ++i) {
if (memory_blocks[i].allocated == 0 && memory_blocks[i].size >= process_size && memory_blocks[i].size > worst_size) {
// Choose the block with the largest remaining size after allocation
worst_index = i;
worst_size = memory_blocks[i].size;
}
}
if (worst_index == -1) {
// No suitable memory block available for allocation
printf("| %-12d| %-14d| %-9s|\n", process_no, process_size, "Not found");
return;
}
// Allocate memory
memory_blocks[worst_index].allocated = 1;
memory_blocks[worst_index].size = process_size;
printf("| %-12d| %-14d| %-9d|\n", process_no, process_size, worst_index+1);
}
int main() {
int i, n_blocks, n_processes;
struct memory_block *memory_blocks;
int *process_sizes;
// Take input from user
printf("Enter the number of memory blocks: ");
scanf("%d", &n_blocks);
memory_blocks = (struct memory_block *) malloc(n_blocks * sizeof(struct memory_block));
// Initialize memory blocks
for (i = 0; i < n_blocks; ++i) {
printf("Enter the size of memory block %d: ", i + 1);
scanf("%d", &memory_blocks[i].size);
memory_blocks[i].start_address = i * memory_blocks[i].size;
memory_blocks[i].end_address = (i + 1) * memory_blocks[i].size - 1;
memory_blocks[i].allocated = 0;
}
printf("Enter the number of processes: ");
scanf("%d", &n_processes);
process_sizes = (int *) malloc(n_processes * sizeof(int));
printf("Enter the size of each process:\n");
for (i = 0; i < n_processes; ++i) {
printf("Process %d: ", i + 1);
scanf("%d", &process_sizes[i]);
}
// Print table header
printf("\nFirst Fit\n");
printf("| Process No. | Process Size | Block No. |\n");
printf("|-------------|---------------|-----------|\n");
// Allocate memory using First Fit technique
for (i = 0; i < n_processes; ++i) {
allocate_first_fit(memory_blocks, n_blocks, process_sizes[i], i+1);
}
// Print table header
printf("\nBest Fit\n");
printf("| Process No. | Process Size | Block No. |\n");
printf("|-------------|---------------|-----------|\n");
// Deallocate memory
for (i = 0; i < n_blocks; ++i) {
memory_blocks[i].allocated = 0;
}
// Allocate memory using Best Fit technique
for (i = 0; i < n_processes; ++i) {
allocate_best_fit(memory_blocks, n_blocks, process_sizes[i], i+1);
}
// Print table header
printf("\nWorst Fit\n");
printf("| Process No. | Process Size | Block No. |\n");
printf("|-------------|---------------|-----------|\n");
// Deallocate memory
for (i = 0; i < n_blocks; ++i) {
memory_blocks[i].allocated = 0;
}
// Allocate memory using Worst Fit technique
for (i = 0; i < n_processes; ++i) {
allocate_worst_fit(memory_blocks, n_blocks, process_sizes[i], i+1);
}
return 0;
}