0% found this document useful (0 votes)
15 views15 pages

Memorymanagement Osexp89 2

The document outlines various memory management techniques in C programming, including MVT (Multi-Variable Task), MFT (Multi-File Task), and dynamic partitioning algorithms like First Fit, Best Fit, and Worst Fit. It also covers page replacement policies such as FIFO (First In First Out) and LRU (Least Recently Used) for handling page faults. Each section includes sample code and explanations for implementing these concepts.

Uploaded by

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

Memorymanagement Osexp89 2

The document outlines various memory management techniques in C programming, including MVT (Multi-Variable Task), MFT (Multi-File Task), and dynamic partitioning algorithms like First Fit, Best Fit, and Worst Fit. It also covers page replacement policies such as FIFO (First In First Out) and LRU (Least Recently Used) for handling page faults. Each section includes sample code and explanations for implementing these concepts.

Uploaded by

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

MEMORY MANAGEMENT

EXPERIMENT NO - 8(A)

AIM - A) Write a program to demonstrate the concept of MVT and MFT memory
management technique .

PROGRAM-(MVT)
#include<stdio.h>
void main() {
int ms, bs, nob, ef, n, mp[10], tif = 0;
int i, p = 0;
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d", &ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob = ms / bs;
ef = ms - nob * bs;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);

for (i = 0; i < n; i++) {


printf("Enter memory required for process %d (in Bytes)-- ", i + 1);
scanf("%d", &mp[i]);
}
printf("\nNo. of Blocks available in memory-- %d", nob);
printf("\n\nPROCESS\tMEMORY REQUIRED\tALLOCATED\tINTERNAL
FRAGMENTATION");
for (i = 0; i < n && p < nob; i++)
printf("\n %d\t\t%d", i + 1, mp[i]);
if (mp[i] > bs) {
printf("\t\tNO\t\t---");
} else {
printf("\t\tYES\t\t%d", bs - mp[i]);
tif += bs - mp[i];
p++;
}
}
if (i < n) {
printf("\nMemory is Full, Remaining Processes cannot be accommodated");
}
printf("\n\nTotal Internal Fragmentation is %d", tif);
printf("\nTotal External Fragmentation is %d", ef);
}

OUTPUT :
PROGRAM-(MFT)
#include<stdio.h>
void main() {
int ms, mp[10], i, temp, n = 0;
char ch = 'y';

printf("\nEnter the total memory available (in Bytes) -- ");


scanf("%d", &ms);

temp = ms;

for(i = 0; ch == 'y'; i++, n++) {


printf("\nEnter memory required for process %d (in Bytes) -- ", i + 1);
scanf("%d", &mp[i]);

if(mp[i] <= temp) {

printf("\nMemory is allocated for Process %d", i + 1);


temp = temp - mp[i];

} else {
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue (y/n) -- ");
scanf(" %c", &ch); // The space before %c handles the newline character
from previous input
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\tMEMORY ALLOCATED");

for(i = 0; i < n; i++) {


printf("\n \t%d\t\t%d", i + 1, mp[i]);
}
printf("\n\nTotal Memory Allocated is %d", ms - temp);
printf("\nTotal External Fragmentation is %d", temp);
}
OUTPUT :
MEMORY MANAGEMENT
EXPERIMENT NO - 8B

AIM -. B) Write a program to demonstrate the concept of dynamic partitioning


placement algorithms i.e First Fit ,Best Fit,Worst Fit etc .

PROGRAM (FIRST-FIT)
#include<stdio.h>
#define MAX 25
void main() {
int frag[MAX], b[MAX], f[MAX], bf[MAX], ff[MAX];
int i, j, nb, nf, temp;
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 (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 <= nb; i++) {


bf[i] = 0;
}
for (i = 1; i <= nf; i++) {
for (j = 1; j <= nb; j++) {
if (bf[j] != 1) {
temp = b[j] - f[i];
if (temp >= 0) {
ff[i] = j;
bf[j] = 1;
frag[i] = temp;
break; } }
}
if (j > nb) {
ff[i] = -1;
frag[i] = f[i];
} }
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragment");
for (i = 1; i <= nf; i++) {
if (ff[i] != -1) {
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]);
} else {
printf("\n%d\t\t%d\t\tNot Allocated\t\t-\t\t-\t\t", i, f[i]);
}}
printf("\n");
}

OUTPUT :

PROGRAM (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("\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) {
temp = b[j] - f[i];
if(temp >= 0 && temp < lowest) {
ff[i] = j;
lowest = temp;
}
}
}

frag[i] = lowest;
bf[ff[i]] = 1;
}
printf("\nFile No\tFile Size\tBlock No\tBlock Size\tFragmentation");
for(i = 1; i <= nf; i++) {
if(ff[i] != 0) {
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]);
} else {
printf("\n%d\t\t%d\t\tNot Allocated\t\t-\t\t-", i, f[i]);
}
}

printf("\n");
}

OUTPUT :

PROGRAM (WORST-FIT)
#include<stdio.h>
#define MAX 25

void main() {
int frag[MAX], b[MAX], f[MAX], i, j, nb, nf, temp, highest;
static int bf[MAX], ff[MAX];

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(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++) {


highest = -1;
for(j = 1; j <= nb; j++) {
if(bf[j] != 1) {
temp = b[j] - f[i];
if(temp >= 0 && temp > highest) {
ff[i] = j;
highest = temp;
}
}
}

if(highest != -1) {
frag[i] = highest;
bf[ff[i]] = 1;
}
}

printf("\nFile No\tFile Size\tBlock No\tBlock Size\tFragmentation");


for(i = 1; i <= nf; i++) {
if(ff[i] != 0) {
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]);
} else {
printf("\n%d\t\t%d\t\tNot Allocated\t\t-\t\t-", i, f[i]);
}
}

printf("\n");
}

OUTPUT :

MEMORY MANAGEMENT : VIRTUAL MEMORY


EXPERIMENT NO - 09
AIM - Write a C program to demonstrate the concept of page replacement
policies for handling page faults eg: FIFO,LRU .
PROGRAM (FIRST IN FIRST OUT)
#include<stdio.h>
int fr[3];
void display();
void main() {
int i, j, page[12] = {2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2};
int flag1 = 0, flag2 = 0, pf = 0, frsize = 3, top = 0;

for(i = 0; i < 3; i++) {


fr[i] = -1;
}
for(j = 0; j < 12; j++) {
flag1 = 0;
flag2 = 0;
for(i = 0; i < 3; i++) {
if(fr[i] == page[j]) {
flag1 = 1;
flag2 = 1;
break;
}}
if(flag1 == 0) {
for(i = 0; i < frsize; i++) {
if(fr[i] == -1) {
fr[i] = page[j];
flag2 = 1;
break;
}}
}
if(flag2 == 0) {
fr[top] = page[j];
top++;
pf++;
if(top >= frsize)
top = 0;
}
display(); }
printf("Number of page faults: %d ", pf + frsize);
}
void display() {
int i;
printf("\n");
for(i = 0; i < 3; i++) {
printf("%d\t", fr[i]);
}
}

OUTPUT :

PROGRAM-(LEAST RECENTLY USED)


#include<stdio.h>
int fr[3];

void display();

void main() {
int p[12] = {2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2};
int i, j, fs[3], index, k, flag1 = 0, flag2 = 0, pf = 0, frsize = 3;

for(i = 0; i < 3; i++) {


fr[i] = -1;
}

for(j = 0; j < 12; j++) {


flag1 = 0;
flag2 = 0;

for(i = 0; i < 3; i++) {


if(fr[i] == p[j]) {
flag1 = 1;
flag2 = 1;
break;
}
}

if(flag1 == 0) {
for(i = 0; i < 3; i++) {
if(fr[i] == -1) {
fr[i] = p[j];
flag2 = 1;
break;
}
}
}
if(flag2 == 0) {
for(i = 0; i < 3; i++) {
fs[i] = 0;
}

for(k = j + 1; k < 12; k++) {


for(i = 0; i < 3; i++) {
if(fr[i] == p[k]) {
fs[i] = 1;
}
}
}

for(i = 0; i < 3; i++) {


if(fs[i] == 0) {
index = i;
break;
}
}

fr[index] = p[j];
pf++;
}

display();
}

printf("\nNumber of page faults: %d", pf + frsize);


}

void display() {
int i;
printf("\n");
for(i = 0; i < 3; i++) {
printf("%d\t", fr[i]);
}
}

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy