Final Os
Final Os
REG NO : 22BCE0894
1. Memory Allocation Strategies
a. First Fit
CODE :
#include <stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int m, n;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
int blockSize[m];
printf("Enter the sizes of memory blocks:\n");
for (int i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}
printf("\nEnter the number of processes: ");
scanf("%d", &n);
int processSize[n];
printf("Enter the sizes of processes:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
}
firstFit(blockSize, m, processSize, n);
return 0;
}
OUTPUT:
b. Best Fit
CODE:
#include <stdio.h>
void bestFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++) {
int bestIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIdx == -1 || blockSize[bestIdx] > blockSize[j])
bestIdx = j;
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int m, n;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
int blockSize[m];
printf("Enter the sizes of memory blocks:\n");
for (int i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}
printf("\nEnter the number of processes: ");
scanf("%d", &n);
int processSize[n];
printf("Enter the sizes of processes:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
}
bestFit(blockSize, m, processSize, n);
return 0;
}
OUTPUT:
c. Worst Fit
CODE:
#include <stdio.h>
void worstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++) {
int worstIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1 || blockSize[worstIdx] <
blockSize[j])
worstIdx = j;
}
}
if (worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int m, n;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
int blockSize[m];
printf("Enter the sizes of memory blocks:\n");
for (int i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}
printf("\nEnter the number of processes: ");
scanf("%d", &n);
int processSize[n];
printf("Enter the sizes of processes:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
}
worstFit(blockSize, m, processSize, n);
return 0;
}
OUTPUT:
2.
LRU
CODE:
#include <stdio.h>
if (!flag1) {
for (int j = 0; j < capacity; j++) {
if (frames[j] == -1) {
counter++;
pageFaults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if (!flag2) {
int pos = findLRU(time, capacity);
counter++;
pageFaults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\nFrames: ");
for (int j = 0; j < capacity; j++) {
if (frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
}
int main() {
int n, capacity;
return 0;
}
OPTIMAL PAGE :
CODE:
#include <stdio.h>
int findOptimal(int pages[], int frames[], int n, int index, int capacity) {
int farthest = index, pos = -1;
for (int i = 0; i < capacity; i++) {
int j;
for (j = index; j < n; j++) {
if (frames[i] == pages[j]) {
if (j > farthest) {
farthest = j;
pos = i;
}
break;
}
}
if (j == n)
return i;
}
return (pos == -1) ? 0 : pos;
}
if (!flag) {
if (i < capacity) {
frames[i] = pages[i];
pageFaults++;
} else {
int pos = findOptimal(pages, frames, n, i + 1, capacity);
frames[pos] = pages[i];
pageFaults++;
}
}
printf("\nFrames: ");
for (int j = 0; j < capacity; j++) {
if (frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
}
int main() {
int n, capacity;
return 0;
}