Professional Documents
Culture Documents
OSLExp 8
OSLExp 8
a. First Fit
Code:-
#include <stdio.h>
typedef struct {
int start_address;
int end_address;
int size;
int allocated;
} Partition;
Partition memory[MAX_PARTITIONS];
int num_partitions = 0;
int main() {
int size;
initializeMemory();
return 0;
}
Output:-
b. Best Fit:-
Code:-
#include <stdio.h>
typedef struct {
int start_address;
int end_address;
int size;
int allocated;
} Partition;
Partition memory[MAX_PARTITIONS];
int num_partitions = 0;
int main() {
int size;
initializeMemory();
return 0;
}
Output:-
Worst Fit:-
Code:-
#include <stdio.h>
typedef struct {
int start_address;
int end_address;
int size;
int allocated;
} Partition;
Partition memory[MAX_PARTITIONS];
int num_partitions = 0;
if (worst_fit_index != -1) {
memory[worst_fit_index].allocated = 1;
if (memory[worst_fit_index].size > size) {
memory[num_partitions].start_address = memory[worst_fit_index].start_address + size;
memory[num_partitions].end_address = memory[worst_fit_index].end_address;
memory[num_partitions].size = memory[worst_fit_index].size - size;
memory[num_partitions].allocated = 0;
num_partitions++;
}
memory[worst_fit_index].size = size;
printf("Memory allocated using Worst Fit.\n");
} else {
printf("Memory allocation failed using Worst Fit.\n");
}
}
int main() {
int size;
initializeMemory();
return 0;
}
Output:-
Conclusion:-In conclusion, each allocation strategy has its trade-offs in terms of fragmentation and
computational efficiency. The choice of strategy depends on the specific requirements of the
system, such as the expected size distribution of processes and the tolerance for fragmentation.