0% found this document useful (0 votes)
4 views3 pages

Os Program

Uploaded by

patilanuj49
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)
4 views3 pages

Os Program

Uploaded by

patilanuj49
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/ 3

LRU PROGRAM - Slip 3_1,8_1,12_1,18

#include <stdio.h>

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

int i, min = time[0], pos = 0;

for (i = 1; i < n; i++)

if (time[i] < min) pos = i, min = time[i];

return pos;

int main() {

int pages[] = {3, 5, 7, 2, 5, 1, 2, 3, 1, 3, 5, 3, 1, 6, 2}, frames[3], time[3];

int n = 15, no_of_frames = 3, count = 0, faults = 0, i, j, pos;

for (i = 0; i < no_of_frames; i++) frames[i] = -1;

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

int flag = 0;

for (j = 0; j < no_of_frames; j++)

if (frames[j] == pages[i]) flag = 1, time[j] = ++count;

if (!flag) {

pos = (frames[0] == -1) ? i % no_of_frames : findLRU(time, no_of_frames);

frames[pos] = pages[i], time[pos] = ++count, faults++;

for (j = 0; j < no_of_frames; j++) printf("%d ", frames[j] == -1 ? -1 : frames[j]);

printf("\n");

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

return 0;
}

Optimal Page Replacement - Slip 5_1,7_1,17_1,23_2

#include <stdio.h>

int find_farthest(int pages[], int n, int frame[], int m, int index) {

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

int j;

for (j = index; j < n; j++)

if (frame[i] == pages[j]) break;

if (j == n) return i;

return 0;

int main() {

int pages[] = {8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2};

int n = sizeof(pages) / sizeof(pages[0]), m, frame[10], faults = 0, filled = 0;

printf("Enter number of frames: ");

scanf("%d", &m);

for (int i = 0; i < m; i++) frame[i] = -1;


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

int hit = 0;

for (int j = 0; j < m; j++)

if (frame[j] == pages[i]) hit = 1;

if (!hit) {

if (filled < m) frame[filled++] = pages[i];

else frame[find_farthest(pages, n, frame, m, i + 1)] = pages[i];

faults++;

printf("Page %d -> ", pages[i]);

for (int j = 0; j < m; j++) printf("%d ", frame[j]);

printf("\n");

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

return 0;

You might also like