0% found this document useful (0 votes)
8 views9 pages

Os Lab Da4

operating systems lab assigments
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Os Lab Da4

operating systems lab assigments
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB ASSESMENT-4

Memory management
techniques

Name: ch jaswanth

Reg no: 22BCE3546


Code:
#include <stdio.h>
#define MAX 25

void first_fit(int b[], int f[], int nb, int nf) {


int frag[MAX], bf[MAX] = {0}, ff[MAX];

for (int i = 1; i <= nf; i++) {


int highest = -1; // Reset highest for each file
for (int j = 1; j <= nb; j++) {
if (bf[j] != 1) { // if bf[j] is not allocated
int temp = b[j] - f[i];
if (temp >= 0) {
ff[i] = j; // Store the block number
break; // Exit the loop for first fit
}
}
}
if (ff[i] != 0) { // If a block is found
frag[i] = b[ff[i]] - f[i]; // Calculate fragmentation
bf[ff[i]] = 1; // Mark block as allocated
} else {
frag[i] = -1; // Indicate failure to allocate
}
}

printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragment");
for (int i = 1; i <= nf; i++) {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]],
frag[i]);
}
}

int main() {
int b[MAX], f[MAX];
int nb, nf;

printf("\n\tMemory Management Scheme - First Fit");


printf("\nEnter the number of blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");
scanf("%d", &nf);

printf("\nEnter the size of the blocks:\n");


for (int i = 1; i <= nb; i++) {
printf("Block %d: ", i);
scanf("%d", &b[i]);
}

printf("Enter the size of the files:\n");


for (int i = 1; i <= nf; i++) {
printf("File %d: ", i);
scanf("%d", &f[i]);
}

first_fit(b, f, nb, nf);

return 0;
}
Output:

Best fit

Code:
#include <stdio.h>
#define MAX 25

void best_fit(int b[], int f[], int nb, int nf) {


int frag[MAX], bf[MAX] = {0}, ff[MAX];

for (int i = 1; i <= nf; i++) {


int best_index = -1;
int min_fragmentation = 99999; // Large number
for (int j = 1; j <= nb; j++) {
if (bf[j] != 1) { // if bf[j] is not allocated
int temp = b[j] - f[i];
if (temp >= 0 && temp < min_fragmentation) {
best_index = j;
min_fragmentation = temp; // Find minimum fragmentation
}
}
}
if (best_index != -1) { // If a block is found
ff[i] = best_index; // Store the block number
frag[i] = b[best_index] - f[i]; // Calculate fragmentation
bf[best_index] = 1; // Mark block as allocated
} else {
frag[i] = -1; // Indicate failure to allocate
}
}

printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragment");
for (int i = 1; i <= nf; i++) {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]],
frag[i]);
}
}

int main() {
int b[MAX], f[MAX];
int nb, nf;

printf("\n\tMemory Management Scheme - Best Fit");


printf("\nEnter the number of blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");
scanf("%d", &nf);

printf("\nEnter the size of the blocks:\n");


for (int i = 1; i <= nb; i++) {
printf("Block %d: ", i);
scanf("%d", &b[i]);
}

printf("Enter the size of the files:\n");


for (int i = 1; i <= nf; i++) {
printf("File %d: ", i);
scanf("%d", &f[i]);
}

best_fit(b, f, nb, nf);

return 0;
}
Output :
Worst fit

Code:
#include <stdio.h>
#define MAX 25

void worst_fit(int b[], int f[], int nb, int nf) {


int frag[MAX], bf[MAX] = {0}, ff[MAX];

for (int i = 1; i <= nf; i++) {


int worst_index = -1;
int max_fragmentation = -1; // Small number
for (int j = 1; j <= nb; j++) {
if (bf[j] != 1) { // if bf[j] is not allocated
int temp = b[j] - f[i];
if (temp >= 0 && temp > max_fragmentation) {
worst_index = j;
max_fragmentation = temp; // Find maximum fragmentation
}
}
}
if (worst_index != -1) { // If a block is found
ff[i] = worst_index; // Store the block number
frag[i] = b[worst_index] - f[i]; // Calculate fragmentation
bf[worst_index] = 1; // Mark block as allocated
} else {
frag[i] = -1; // Indicate failure to allocate
}
}

printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragment");
for (int i = 1; i <= nf; i++) {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]],
frag[i]);
}
}

int main() {
int b[MAX], f[MAX];
int nb, nf;

printf("\n\tMemory Management Scheme - Worst Fit");


printf("\nEnter the number of blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");
scanf("%d", &nf);

printf("\nEnter the size of the blocks:\n");


for (int i = 1; i <= nb; i++) {
printf("Block %d: ", i);
scanf("%d", &b[i]);
}

printf("Enter the size of the files:\n");


for (int i = 1; i <= nf; i++) {
printf("File %d: ", i);
scanf("%d", &f[i]);
}

worst_fit(b, f, nb, nf);


return 0;

Output:

You might also like