6.best Fit
6.best Fit
6.best Fit
BEST FIT
#include<stdio.h>
#define max 25
void main() {
int frag[max], b[max], f[max], i, j, nb, nf, temp, lowest;
static int bf[max], ff[max];
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 (i = 1; i <= nb; i++) {
printf("Block %d:", i); scanf("%d", &b[i]);
}
printf("Enter the size of the files:\n");
for (i = 1; i <= nf; i++) {
printf("File %d:", i); scanf("%d", &f[i]);
}
for (i = 1; i <= nf; i++) {
lowest = 10000;
for (j = 1; j <= nb; j++) {
if (bf[j] != 1 && b[j] >= f[i]) {
temp = b[j] - f[i];
if (temp < lowest) { ff[i] = j; lowest = temp; }
}
}
frag[i] = lowest;
bf[ff[i]] = 1;
}
printf("\nFile No\tFile Size\tBlock No\tBlock Size\tFragment");
for (i = 1; i <= nf && ff[i] != 0; 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]);
}
7.FIFO
#include<stdio.h>
#include<stdlib.h>
void FIFO(char [], char [], int, int);
int main() {
int ch, YN = 1, l, f;
char F[10], s[25];
printf("\nEnter the number of empty frames: ");
scanf("%d", &f);
printf("\nEnter the length of the string: ");
scanf("%d", &l);
printf("\nEnter the string: ");
scanf("%s", s);
printf("FIFO");
FIFO(s, F, l, f);
return 0;
}
void FIFO(char s[], char F[], int l, int f) {
int i, j = 0, k, flag = 0, cnt = 0;
printf("\n\tPAGE\tFRAMES\tFAULTS");
for (i = 0; i < l; i++) {
flag = 0;
for (k = 0; k < f; k++) {
if (F[k] == s[i]) {
flag = 1;
break;
}
}
printf("\n\t%c\t", s[i]);
if (flag == 0) {
F[j] = s[i];
j = (j + 1) % f;