LRU Program in C: Algorithm and Implementation

What is the reason for using the LRU page replacement algorithm?

LRU (Least Recently Used) is a page replacement algorithm that is commonly used in computer operating systems and cache memory management.

The purpose of the LRU algorithm is to replace the least recently used page in memory to optimize and improve system performance and minimize page fault issues.


How does the LRU algorithm work?

The purpose of the LRU page replacement algorithm is to improve system performance, Which is based on the idea that the pages that have not been used for the longest time are the least likely to be used in the near future.

The algorithm keeps a log of pages used. So that when a new page needs to be loaded in memory but memory does not have enough space to load it, The LRU algorithm identifies the pages least recently used and removes them from memory to accommodate new page requests.

The LRU algorithm uses a queue or a list to track page usage order. Recently used pages will be moved to the front of the queue, which indicates the most recently used pages. The page at the end of the queue indicates least recently used.


LRU algorithm program in c: Implementing LRU Page Replacement Algorithm

#include <stdio.h>
#include <stdlib.h>

// Function to find the index of the least recently used page
int findLRUIndex(int pages[], int n, int used[], int m) {
    int i, min = used[0], index = 0;
    for (i = 1; i < n; i++) {
        if (used[i] < min) {
            min = used[i];
            index = i;
        }
    }
    return index;
}

// Function to simulate LRU page replacement algorithm
void lruPageReplacement(int pages[], int n, int m) {
    int frames[m];      // Memory frames
    int used[n];        // Array to store the usage count of each page
    int i, j, k, found, count = 0;

    for (i = 0; i < m; i++)
        frames[i] = -1; // Initialize memory frames with -1 (indicating an empty frame)

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

        // Check if the page is already in memory
        for (j = 0; j < m; j++) {
            if (frames[j] == pages[i]) {
                found = 1;
                used[j] = count++; // Update usage count for the page
                break;
            }
        }

        // If the page is not in memory, perform a page replacement
        if (!found) {
            int index = findLRUIndex(pages, m, used, m);
            frames[index] = pages[i];
            used[index] = count++; // Update usage count for the newly loaded page
        }

        // Print the current state of memory frames
        printf("Memory frames after request %d: ", pages[i]);
        for (k = 0; k < m; k++) {
            if (frames[k] == -1)
                printf("Empty ");
            else
                printf("%d ", frames[k]);
        }
        printf("\n");
    }
}

int main() {
    int n, m, i;

    printf("Enter the number of pages: ");
    scanf("%d", &n);

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

    printf("Enter the number of memory frames: ");
    scanf("%d", &m);

    printf("\nLRU Page Replacement Simulation:\n");
    lruPageReplacement(pages, n, m);

    return 0;
}

LRU program in c with output:

Enter the number of pages: 12
Enter the page reference string: 2 3 1 4 2 5 3 4 6 7 4 3
Enter the number of memory frames: 3

LRU Page Replacement Simulation:
Memory frames after request 2: 2 Empty Empty 
Memory frames after request 3: 2 3 Empty 
Memory frames after request 1: 2 3 1 
Memory frames after request 4: 4 3 1 
Memory frames after request 2: 4 2 1 
Memory frames after request 5: 4 2 5 
Memory frames after request 3: 3 2 5 
Memory frames after request 4: 3 4 5 
Memory frames after request 6: 6 4 5 
Memory frames after request 7: 6 7 5 
Memory frames after request 4: 6 7 4 
Memory frames after request 3: 3 7 4 

Understand Code - LRU program in C:

1. Header Files:

#include <stdio.h>
#include <stdlib.h>
These are the standard header files required for input and output operations and memory allocation functions.

2. findLRUIndex() function :

int findLRUIndex(int pages[], int n, int used[], int m)
The findLRUIndex() function is responsible for finding the index of the least recently used page in the memory frames.
Function takes four arguments, described below.
  • int pages[] : An array pages[] represents a page reference string.
  • int n : Number of pages in the page reference string.
  • int used[] : used[] array keeps track of the usage count of each page in the memory frames.
  • int m: The number of memory frames.

3. lruPageReplacement() function:

void lruPageReplacement(int pages[], int n, int m)
The lruPageReplacement() function is responsible for simulating the LRU page replacement algorithm.
Function takes four arguments, described below.
  • int pages[] : An array representing the page reference string.
  • int n: The number of pages in the page reference string.
  • int m: The number of memory frames.
Inside the function, it initializes two arrays:
frames: An array to represent the memory frames, initially filled with -1 to indicate empty frames.
used: An array to store the usage count of each page in the memory frames.

It then iterates through the page reference string and checks if the requested page is already present in the memory frames. If it is found, the usage count for that page is updated. If the page is not found in the memory frames, the algorithm identifies the least recently used page using the findLRUIndex function, replaces it with the new page, and updates the usage count for the newly loaded page.

After each iteration, the function prints the current state of the memory frames.

4. main() Function:

The main() function is the entry point of the program.

It first asks the user to input the number of pages in the page reference string (n), the page reference string itself (pages), and the number of memory frames (m).

Then, it calls the lruPageReplacement() function to simulate the LRU page replacement algorithm with the given input.

Post a Comment

0 Comments