9. Simulate MVT and MFT.
MVT (Multiprogramming with a Variable number of Tasks)
Allocates memory dynamically to each process as per its requirement.
Memory is not divided into fixed-sized partitions (unlike MFT).
If enough memory is available, the process is loaded; else, it waits.
#include <stdio.h>
void main() {
int total, used = 0, req;
printf(“enter total memory”);
scanf("%d", &total);
while (1) {
printf(“enter requested memory”);
scanf("%d", &req);
if (req==0) break;
if (req <= total - used)
printf("OK %d %d\n", used += req, total - used);
else
printf("NO %d %d\n", used, total - used);
Output:
enter total memory 20
enter requested memory15
OK 15 20
enter requested memory25
NO 15 5
enter requested memory0
MFT (Multiprogramming with Fixed number of Tasks)
Memory is divided into fixed-size partitions.
Each process gets exactly one partition.
If the process size is smaller than partition size, internal fragmentation occurs.
#include<stdio.h>
void main() {
int mem, part, n, size, i = 0;
printf(“enter total memory and partition size”);
scanf("%d%d", &mem, &part); // total memory & partition size
n = mem / part;
while (i < n) {
printf(“enter requested size”);
scanf("%d", &size);
if (size == 0) break;
if (size <= part)
{
printf("P%d Allocated. IF: %d\n", i + 1, part - size);
i++;
} else
printf("Too big!\n");
} }
Output:
enter total memory and partition size 50 5
enter requested size 10
Too big!
enter requested size3
P1 Allocated. IF: 2
enter requested size5
P2 Allocated. IF: 0
enter requested size 0
10. Implementation of the following Memory Allocation
Methods for fixed partition
a)First Fit b) Worst Fit c) Best Fit
a. First fit code
#include <stdio.h>
void main() {
int b[5] = {100, 500, 200, 300, 600};
int p[4] = {212, 417, 112, 426};
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
if (b[j] >= p[i]) {
printf("Process %d -> Block %d\n", p[i], b[j]);
b[j] -= p[i];
break;
}
}
if (j == 5)
printf("Process %d -> Not Allocated\n", p[i]);
} }
Output:
Process 212 -> Block 500
Process 417 -> Block 600
Process 112 -> Block 288
Process 426 -> Not Allocated
b. Best fit
#include <stdio.h>
void main() {
int blocks[5] = {100, 500, 200, 300, 600};
int processes[4] = {212, 417, 112, 426};
int i, j, best;
for (i = 0; i < 4; i++) {
best = -1;
for (j = 0; j < 5; j++) {
if (blocks[j] >= processes[i]) {
if (best == -1 || blocks[j] < blocks[best]) {
best = j;
} } }
if (best != -1) {
printf("Process %d -> Block %d\n", processes[i], blocks[best]);
blocks[best] -= processes[i];
} else {
printf("Process %d -> Not Allocated\n", processes[i]);
} }}
Output:
Process 212 -> Block 300
Process 417 -> Block 500
Process 112 -> Block 200
Process 426 -> Block 600
C. Worst Fit
#include <stdio.h>
int main() {
int b[5] = {100, 500, 200, 388, 600};
int p[4] = {212, 417, 112, 426};
int i, j, worst;
for (i = 0; i < 4; i++) {
worst = -1;
for (j = 0; j < 5; j++) {
if (b[j] >= p[i]) {
if (worst == -1 || b[j] > b[worst])
worst = j;
}
}
if (worst != -1) {
printf("Process %d -> Block %d\n", p[i], b[worst]);
b[worst] -= p[i];
} else {
printf("Process %d -> Not Allocated\n", p[i]);
} }}
Output:
Process 212 -> Block 600
Process 417 -> Block 500
Process 112 -> Block 388
Process 426 -> Not Allocated
[Link] all page replacement algorithms.
a) FIFO b) LRU c) Optimal (LFU).
a. FIFO
#include <stdio.h>
int main() {
int pages[] = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};
int n = 12, frames[3] = {-1, -1, -1}, i, j = 0, hit = 0;
for (i = 0; i < n; i++) {
int found = 0;
for (int k = 0; k < 3; k++)
if (frames[k] == pages[i])
found = 1;
if (found)
hit++;
else {
frames[j] = pages[i];
j = (j + 1) % 3;
}
}
printf("Page Hits: %d\n", hit);
printf("Page Faults: %d\n", n - hit);
return 0;
}
Output:
Page Hits: 3
Page Faults: 9
b. LRU
#include <stdio.h>
int main() {
int pages[] = {1,2,3,2,4,1,5}, n = 7, f[3] = {-1,-1,-1}, t[3] = {0}, i, j, k,
hit = 0, time = 0;
for (i = 0; i < n; i++)
{
int found = 0;
for (j = 0; j < 3; j++)
if (f[j] == pages[i]) { hit++; t[j] = ++time; found = 1; break;
}
if (!found) {
int lru = 0;
for (j = 1; j < 3; j++) if (t[j] < t[lru]) lru = j;
f[lru] = pages[i]; t[lru] = ++time;
}
}
printf("Hits: %d\nFaults: %d\n", hit, n - hit);
return 0;
} Output:
Hits: 1
Faults: 6
c. Optimal
#include <stdio.h>
void main() {
int pages[] = {7,0,1,2,0,3,0,4}, n = 8;
int frames[3] = {-1,-1,-1}, i, j, k, hit = 0, fault = 0;
for(i = 0; i < n; i++) {
int found = 0;
for(j = 0; j < 3; j++)
if(frames[j] == pages[i]) { hit++; found = 1; break; }
if(found) continue;
int farthest = -1, index = -1;
for(j = 0; j < 3; j++) {
for(k = i + 1; k < n; k++)
if(frames[j] == pages[k]) break;
if(k > farthest) { farthest = k; index = j; }
}
if(index == -1) // empty frame
for(j = 0; j < 3; j++)
if(frames[j] == -1) { index = j; break; }
frames[index] = pages[i];
fault++;
}
printf("Hits: %d\nFaults: %d\n", hit, fault); }
output: Hits:2 Faults:6
12. Simulate all File allocation strategies a) Sequenced b)
Indexed c) Linked.
[Link] Allocation
#include <stdio.h>
#include <stdbool.h>
int main() {
bool disk[100] = {0};
int start, len, i;
printf("Start block and length: ");
scanf("%d %d", &start, &len);
for (i = start; i < start + len; i++) {
if (disk[i]) {
printf("Block %d already allocated.\n", i);
return 1;
}
}
for (i = start; i < start + len; i++)
disk[i] = 1;
printf("Allocated from %d to %d\n", start, start + len - 1);
return 0;
}
Output:
Start block and length: 5 4
Allocated from 5 to 8
[Link] Allocation
#include <stdio.h>
int main() {
int n, start, next[100], visited[100] = {0};
printf("Enter number of blocks: ");
scanf("%d", &n);
printf("Enter starting block: ");
scanf("%d", &start);
printf("Enter next block for each (use -1 for end):\n");
for (int i = 0; i < n; i++)
scanf("%d", &next[i]);
printf("Allocated blocks: ");
while (start != -1 && !visited[start]) {
printf("%d -> ", start);
visited[start] = 1;
start = next[start];
}
printf("NULL\n");
return 0; }
output:
Enter number of blocks: 5
Enter starting block: 0
Enter next block for each (use -1 for end):
1 2 3 4 -1
Allocated blocks: 0 -> 1 -> 2 -> 3 -> 4 -> NULL
[Link] Allocation
#include <stdio.h>
int main() {
int indexBlock, n, blocks[100];
printf("Enter index block and number of blocks: ");
scanf("%d %d", &indexBlock, &n);
printf("Enter %d block numbers: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &blocks[i]);
printf("Index Block: %d\nBlocks Allocated: ", indexBlock);
for (int i = 0; i < n; i++)
printf("%d ", blocks[i]);
printf("\n");
return 0;
}
Output:
Enter index block and number of blocks: 5 3
Enter 3 block numbers: 11 12 13
Index Block: 5
Blocks Allocated: 11 12 13