[go: up one dir, main page]

0% found this document useful (0 votes)
10 views5 pages

Assignment 1

The document contains two implementations of page replacement algorithms in C: FIFO (First-In-First-Out) and LRU (Least Recently Used). Each algorithm processes a sequence of page references, maintains a set of frames, and counts the number of page faults. The output displays the state of frames after each page reference and the total number of page faults at the end.

Uploaded by

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

Assignment 1

The document contains two implementations of page replacement algorithms in C: FIFO (First-In-First-Out) and LRU (Least Recently Used). Each algorithm processes a sequence of page references, maintains a set of frames, and counts the number of page faults. The output displays the state of frames after each page reference and the total number of page faults at the end.

Uploaded by

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

Assignment-4

1)
#include <stdio.h>

void fifoPageReplacement(int pages[], int n, int capacity) {

int frames[capacity], front = 0, count = 0, page_faults = 0;

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

// Initialize frames as empty

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[front] = pages[i];

front = (front + 1) % capacity;

page_faults++; }

printf("Step %d: ", i + 1);

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

printf("%d ", frames[j]);

printf("\n"); }

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

int main() { int pages[] = {4, 2, 3, 1, 4, 5, 2, 3, 6, 4, 5, 7, 3, 2};

int n = sizeof(pages) / sizeof(pages[0]);

int capacity = 3;

printf("FIFO Page Replacement:\n");

fifoPageReplacement(pages, n, capacity);

return 0;

Output-
2) Least Recently Used (LRU) Page Replacement Algorithm
#include <stdio.h>

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

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

for (int i = 1; i < capacity; i++) {

if (time[i] < min) {

min = time[i];
pos = i;

return pos;

void lruPageReplacement(int pages[], int n, int capacity) {

int frames[capacity], time[capacity], page_faults = 0, counter = 0;

// Initialize frames with -1 to indicate empty slots

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

frames[i] = -1;

// Process each page reference

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

int found = 0;

// Check if the page is already in any frame

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

if (frames[j] == pages[i]) {

found = 1;

// Update the time of access

time[j] = counter++;

break;

// If page not found, we need to replace a page

if (!found) {

if (i < capacity) {

// If there is space in frames, load the page directly


frames[i] = pages[i];

time[i] = counter++;

} else {

// Find the Least Recently Used (LRU) page and replace it

int pos = findLRU(time, capacity);

frames[pos] = pages[i];

time[pos] = counter++;

page_faults++; // Increment page fault

// Print the current state of frames after each page reference

printf("Step %d: ", i + 1);

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

printf("%d ", frames[j]);

printf("\n");

// Print total page faults

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

int main() {

int pages[] = {0, 2, 3, 1, 7, 0, 2, 3, 6, 0, 2, 7, 4};

int n = sizeof(pages) / sizeof(pages[0]);

int capacity = 3;

printf("LRU Page Replacement:\n");

lruPageReplacement(pages, n, capacity);

return 0;

}
Output-

You might also like