Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

DIGITAL Assignment – 4

OPERATING SYSTEMS LAB

BCSE303P

NAME : Boggarapu Bala Mohan Sathvik


REG NO : 21BCE0270
SLOT : L9+L10+B2
FACULTY NAME : Vijayasherly V

Signature of the Student


Question no – 01:
Write a C program to study the allocation of memory by applying the
following memory allocation strategies.
• FIRST FIT
• BEST FIT
• WORST FIT
Code:
#include <stdio.h>
#define MAX_BLOCKS 100
struct Block {
int id;
int size;
int allocated;
};
void bestFit(struct Block blocks[], int numBlocks, int processSize) {
int bestFitIdx = -1;
int minDiff = __INT_MAX__;
for (int i = 0; i < numBlocks; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= processSize) {
int diff = blocks[i].size - processSize;
if (diff < minDiff) {
bestFitIdx = i;
minDiff = diff;
}
}
}
if (bestFitIdx != -1) {
blocks[bestFitIdx].allocated = 1;
printf("Process of size %d is allocated to block %d using the best fit
strategy.\n", processSize, blocks[bestFitIdx].id);
} else {
printf("No block is available to allocate process of size %d using the best
fit strategy.\n", processSize);
}
}
void worstFit(struct Block blocks[], int numBlocks, int processSize) {
int worstFitIdx = -1;
int maxDiff = -1;
for (int i = 0; i < numBlocks; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= processSize) {
int diff = blocks[i].size - processSize;
if (diff > maxDiff) {
worstFitIdx = i;
maxDiff = diff;
}
}
}
if (worstFitIdx != -1) {
blocks[worstFitIdx].allocated = 1;
printf("Process of size %d is allocated to block %d using the worst fit
strategy.\n", processSize, blocks[worstFitIdx].id);
} else {
printf("No block is available to allocate process of size %d using the worst
fit strategy.\n", processSize);
}
}
void firstFit(struct Block blocks[], int numBlocks, int processSize) {
for (int i = 0; i < numBlocks; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= processSize) {
blocks[i].allocated = 1;
printf("Process of size %d is allocated to block %d using the first fit
strategy.\n", processSize, blocks[i].id);
return;
}
}
printf("No block is available to allocate process of size %d using the first fit
strategy.\n", processSize);
}

int main() {
int numBlocks, numProcesses;
printf("Enter the number of memory blocks: ");
scanf("%d", &numBlocks);
struct Block blocks[MAX_BLOCKS];
for (int i = 0; i < numBlocks; i++) {
blocks[i].id = i + 1;
printf("Enter the size of block %d: ", blocks[i].id);
scanf("%d", &blocks[i].size);
blocks[i].allocated = 0;
}
printf("Enter the number of processes: ");
scanf("%d", &numProcesses);
int processSize;
int strategy;
printf("Choose the memory allocation strategy:\n");
printf("1. Best Fit\n");
printf("2. Worst Fit\n");
printf("3. First Fit\n");
printf("Enter your choice: ");
scanf("%d", &strategy);
if(!(strategy<=3 && strategy>=1))
{
printf("You have entered an invalid choice,please execute the code once
again\n");
}
for (int i = 0; i < numProcesses; i++) {
printf("Enter the size of process %d: ", i + 1);
scanf("%d", &processSize);
switch (strategy) {
case 1:
bestFit(blocks, numBlocks, processSize);
break;
case 2:
worstFit(blocks, numBlocks, processSize);
break;
case 3:
firstFit(blocks, numBlocks, processSize);
break;
default:
printf("Invalid choice. Please try again.\n");
i--;
}
}
return 0;
}
Output:

FIRST FIT:

BEST FIT:
WORST FIT:
Question no – 02:
Write a C program to study the working of the following page
replacement algorithms and output the page fault rate for arbitrary inputs.
• FIFO
• LRU
• Optimal
Code:
#include <stdio.h>
#define MAX_FRAMES 10
#define MAX_PAGES 100
int frames[MAX_FRAMES];
int pages[MAX_PAGES];
int frameCount = 0;
int pageCount = 0;
int fifoPageFaults() {
int i, j, k, fault = 0;
int flag = 0;
for (i = 0; i < frameCount; ++i) {
frames[i] = -1;
}
j = 0;
printf("Page Replacement Order (FIFO):\n");
for (i = 0; i < pageCount; ++i) {
flag = 0;
for (k = 0; k < frameCount; ++k) {
if (frames[k] == pages[i]) {
flag = 1;
break;
}
}
if (flag == 0) {
printf("%d ", pages[i]);
frames[j] = pages[i];
j = (j + 1) % frameCount;
fault++;
}
}
printf("\n");
return fault;
}
int lruPageFaults() {
int i, j, k, m, fault = 0;
int flag = 0;
int least[10];
int unusedCount = 0;
int leastIndex = 0;
for (i = 0; i < frameCount; ++i) {
frames[i] = -1;
}
j = 0;
printf("Page Replacement Order (LRU):\n");
for (i = 0; i < pageCount; ++i) {
flag = 0;
for (k = 0; k < frameCount; ++k) {
if (frames[k] == pages[i]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (unusedCount < frameCount) {
frames[j] = pages[i];
j = (j + 1) % frameCount;
fault++;
unusedCount++;
} else {
for (k = 0; k < frameCount; ++k) {
least[k] = 0;
for (m = i - 1; m >= 0; --m) {
if (pages[m] == frames[k]) {
least[k] = 1;
break;
}
}
}
for (k = 0; k < frameCount; ++k) {
if (least[k] == 0) {
leastIndex = k;
break;
}
}
frames[leastIndex] = pages[i];
fault++;
}
printf("%d ", pages[i]);
}
}
printf("\n");
return fault;
}
int optimalPageFaults() {
int i, j, k, m, fault = 0;
int flag = 0;
int future[10];
int index = 0;
int max = 0;
for (i = 0; i < frameCount; ++i) {
frames[i] = -1;
}
j = 0;
printf("Page Replacement Order (Optimal):\n");
for (i = 0; i < pageCount; ++i) {
flag = 0;
for (k = 0; k < frameCount; ++k) {
if (frames[k] == pages[i]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (j < frameCount) {
frames[j++] = pages[i];
fault++;
} else {
for (k = 0; k < frameCount; ++k) {
future[k] = 0;
for (m = i + 1; m < pageCount; ++m) {
if (pages[m] == frames[k]) {
future[k] = m - i;
break;
}
}
if (future[k] == 0) {
index = k;
break;
}
}
frames[index] = pages[i];
fault++;
}
printf("%d ", pages[i]);
}
}
printf("\n");
return fault;
}
int main() {
int algorithmChoice;
int i;
printf("Enter the number of frames: ");
scanf("%d", &frameCount);
printf("Enter the number of pages: ");
scanf("%d", &pageCount);
printf("Enter the page reference string:\n");
for (i = 0; i < pageCount; ++i) {
scanf("%d", &pages[i]);
}
printf("\nChoose the page replacement algorithm:\n");
printf("1. FIFO\n");
printf("2. LRU\n");
printf("3. Optimal\n");
printf("Enter your choice: ");
scanf("%d", &algorithmChoice);
int pagefaults;
float hitratio,missratio;
switch (algorithmChoice) {
case 1:
pagefaults = fifoPageFaults();
hitratio = (pageCount - pagefaults)/(float)pageCount;
missratio = 1-hitratio;
printf("\nPage Faults (FIFO): %d\n",pagefaults);
printf("Hit percentage: %f\n",hitratio*100);
printf("Miss percentage: %f\n",missratio*100);
break;
case 2:
pagefaults = lruPageFaults();
hitratio = (pageCount - pagefaults)/(float)pageCount;
missratio = 1-hitratio;
printf("\nPage Faults (LRU): %d\n", pagefaults);
printf("Hit percentage: %f\n",hitratio*100);
printf("Miss percentage: %f\n",missratio*100);
break;
case 3:
pagefaults = optimalPageFaults();
hitratio = (pageCount - pagefaults)/(float)pageCount;
missratio = 1-hitratio;
printf("\nPage Faults (Optimal): %d\n", pagefaults);
printf("Hit percentage: %f\n",hitratio*100);
printf("Miss percentage: %f\n",missratio*100);
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
return 0;
}
Output:
FIFO:
LRU:

Optimal:

You might also like