6.best Fit

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

6.

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;

for (k = 0; k < f; k++) {


printf(" %c", F[k]);
}
printf("\tPage-fault%d", cnt++);
} else {
for (k = 0; k < f; k++) {
printf(" %c", F[k]);
}
printf("\tNo page-fault");
}
}
}
8.TWO LEVEL DIRECTORY
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct {
char dname[10], fname[10][10];
int fcnt;
} dir;
void main() {
int i, ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1) {
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\
nEnter your choice -- ");
scanf("%d", &ch);
switch(ch) {
case 1:
printf("\n Enter the name of the file -- ");
scanf("%s", dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2:
printf("\n Enter the name of the file -- ");
scanf("%s", f);
for(i = 0; i < dir.fcnt; i++) {
if(strcmp(f, dir.fname[i]) == 0) {
printf("File %s is deleted ", f);
strcpy(dir.fname[i], dir.fname[dir.fcnt - 1]);
break;
}
}
if(i == dir.fcnt)
printf("File %s not found", f);
else
dir.fcnt--;
break;
case 3:
printf("\n Enter the name of the file -- ");
scanf("%s", f);
for(i = 0; i < dir.fcnt; i++) {
if(strcmp(f, dir.fname[i]) == 0) {
printf("File %s is found ", f);
break;
}
}
if(i == dir.fcnt)
printf("File %s not found", f);
break;
case 4:
if(dir.fcnt == 0)
printf("\n Directory Empty");
else {
printf("\n The Files are -- ");
for(i = 0; i < dir.fcnt; i++)
printf("\t%s", dir.fname[i]);
}
break;
default:
exit(0);
}
}
}
9.LINKED FILE ALLOCATION
#include <stdio.h>
#include <stdlib.h>
int main() {
int f[50] = {0}, p, i, st, len, j, c, k, a;
printf("Enter how many blocks are already allocated: ");
scanf("%d", &p);
printf("Enter blocks already allocated: ");
for (i = 0; i < p; i++) {
scanf("%d", &a);
f[a] = 1;
}
do {
printf("Enter index of starting block and length: ");
scanf("%d %d", &st, &len);
k = len;
if (f[st] == 0) {
for (j = st; j < (st + k); j++) {
if (f[j] == 0) {
f[j] = 1;
printf("%d ------>%d\n", j, f[j]);
} else {
printf("%d Block is already allocated\n", j);
k++;
}
}
} else {
printf("%d starting block is already allocated\n", st);
}
printf("Do you want to enter more file? (Yes - 1 / No - 0): ");
scanf("%d", &c);
} while (c == 1);
return 0;
}

10.SCAN DISK SCHEDULING


#include<stdio.h>
int main() {
int a[20], b[20], n, i, j, temp, p, s, m, x, t=0;
printf("Enter head pointer position:");
scanf("%d", &a[0]);
s = a[0];
printf("\nEnter previous head position:");
scanf("%d", &p);
printf("\nEnter max track limit:");
scanf("%d", &m);
printf("\nEnter number of processes:");
scanf("%d", &n);
printf("\nEnter processes in request order");
for(i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
a[n+1] = m;
a[n+2] = 0;
for(i = 0; i <= n+1; i++) {
for(j = 0; j <= n+1-i; j++) {
if(a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(i = 1; i <= n+1; i++) {
if(s == a[i]) {
x = i;
break;
}
}
j = 0;
if(s < p) {
for(i = x; i >= 1; i--) {
t += (a[i] - a[i-1]);
b[j++] = a[i];
}
t += (a[n+1] - a[x]);
b[j++] = a[0];
for(i = n+1; i > x; i--) {
t += (a[i] - a[i-1]);
b[j++] = a[i];
}
} else {
for(i = x; i <= n+1; i++) {
t += (a[i] - a[i-1]);
b[j++] = a[i];
}
t += (a[x-1] - a[0]);
for(i = 1; i < x; i++) {
t += (a[i+1] - a[i]);
b[j++] = a[i];
}
}
printf("\nProcessing order:");
for(i = 0; i <= n+1; i++)
printf("%d->", b[i]);
printf("\n\nTotal Head Movement:%d", t);
return 0;
}

You might also like