0% found this document useful (0 votes)
17 views11 pages

Final Os

Uploaded by

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

Final Os

Uploaded by

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

NAME:M,CHAITANYA KARTHIK

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.

Page Replacement Algorithm (FIFO Example)


#include <stdio.h>
void FIFO(int pages[], int n, int capacity) {
int frames[capacity];
int frameIndex = 0, pageFaults = 0;
for (int i = 0; i < capacity; i++)
frames[i] = -1;
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < capacity; j++) {
if (frames[j] == pages[i]) {
found = 1;
break;
}
}
if (!found) {
frames[frameIndex] = pages[i];
frameIndex = (frameIndex + 1) % capacity;
pageFaults++;
}
printf("\nFrames: ");
for (int j = 0; j < capacity; j++) {
if (frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
}
printf("\n\nTotal Page Faults: %d\n", pageFaults);
}
int main() {
int n, capacity;
printf("Enter the number of pages: ");
scanf("%d", &n);
int pages[n];
printf("Enter the reference string (page numbers):\n");
for (int i = 0; i < n; i++) {
printf("Page %d: ", i + 1);
scanf("%d", &pages[i]);
}
printf("\nEnter the number of frames: ");
scanf("%d", &capacity);
FIFO(pages, n, capacity);
return 0;
}
output:

LRU
CODE:
#include <stdio.h>

int findLRU(int time[], int n) {


int i, minimum = time[0], pos = 0;
for (i = 1; i < n; ++i) {
if (time[i] < minimum) {
minimum = time[i];
pos = i;
}
}
return pos;
}

void LRU(int pages[], int n, int frames[], int capacity) {


int time[capacity], pageFaults = 0, counter = 0;
for (int i = 0; i < capacity; i++)
frames[i] = -1;

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


int flag1 = 0, flag2 = 0;

for (int j = 0; j < capacity; j++) {


if (frames[j] == pages[i]) {
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}

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("- ");
}
}

printf("\n\nTotal Page Faults: %d\n", pageFaults);


}

int main() {
int n, capacity;

printf("Enter the number of pages: ");


scanf("%d", &n);
int pages[n];
printf("Enter the reference string (page numbers):\n");
for (int i = 0; i < n; i++) {
printf("Page %d: ", i + 1);
scanf("%d", &pages[i]);
}

printf("\nEnter the number of frames: ");


scanf("%d", &capacity);
int frames[capacity];

LRU(pages, n, frames, 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;
}

void Optimal(int pages[], int n, int frames[], int capacity) {


int pageFaults = 0;

for (int i = 0; i < capacity; i++)


frames[i] = -1;

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


int flag = 0;
for (int j = 0; j < capacity; j++) {
if (frames[j] == pages[i]) {
flag = 1;
break;
}
}

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("- ");
}
}

printf("\n\nTotal Page Faults: %d\n", pageFaults);


}

int main() {
int n, capacity;

printf("Enter the number of pages: ");


scanf("%d", &n);
int pages[n];
printf("Enter the reference string (page numbers):\n");
for (int i = 0; i < n; i++) {
printf("Page %d: ", i + 1);
scanf("%d", &pages[i]);
}

printf("\nEnter the number of frames: ");


scanf("%d", &capacity);
int frames[capacity];

Optimal(pages, n, frames, capacity);

return 0;
}

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