Question & Answer: Maintain a page table as an array. Use page number as an index to the array. The array should include an R…..

Write a program to simulate the Least-Recently-Used (LRU) Page Replacement Algorithm that appears on page 366 of the textbook. In particular, the simulator should accept the following input from standard input (stdin):
Number of Virtual Pages
Number of Page Frames Available (not used by page table)
Number of References
Page Referenced   Read or Write (0 or 1, respectively)   duration
Page Referenced   Read or Write (0 or 1, respectively)   duration
             :                  :
For example:
          0 0 2
          1 1 1
          3 0 1
is valid input. There are 5 virtual pages (numbered 0, 1, ... 4), 3 page frames (numbered 0, 1, 2) in memory, 3 references listed: page 0 is read from for 2 clock ticks, page 1 is written to for 1 clock tick, and page 3 is read from for 1 clock tick.
Maintain a page table as an array. Use page number as an index to the array. The array should include an R-bit, an M-bit, and a Present/Absent bit, as well as the frame currently mapped.
R-bit = 1 if page has been referenced recently (read/write)
M-bit = 1 if page has been modified (needs to be written to disk)
Corresponding to the previous example, the page table could look like this way:
       1 0 1 0
       1 1 1 1
       0 0 0 0
       1 0 1 2
       0 0 0 0
The first row indicates the virtual page 0 is mapped to the physical page 0. The page has been referenced and is present in the physical memory.
Assume that the R-bit is reset to 0 after a set of 6 pages have been referenced. This is to simulate a reset after each clock interrupt.
The output from the simulator should indicate whenever a fault occurs, and which page has been evicted (if one is evicted). Always select the smallest numbered page in a class to be evicted. In addition, the output should include the total number of faults that occurred. 
The possible output would look something like:
                    : : 
            Fault: no page evicted, page 0 brought in to memory.
            Fault: page 2 evicted, page 3 brought in to memory.
                    : :
            Total number of faults is 23.

Expert Answer


// A C program to show implementation of LRU cache
#include <stdio.h>
#include <stdlib.h>

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Maintain a page table as an array. Use page number as an index to the array. The array should include an R…..
Order Essay

// A Queue Node (Queue is implemented using Doubly Linked List)
typedef struct QNode
struct QNode *prev, *next;
unsigned pageNumber; // the page number stored in this QNode
} QNode;

// A Queue (A FIFO collection of Queue Nodes)
typedef struct Queue
unsigned count; // Number of filled frames
unsigned numberOfFrames; // total number of frames
QNode *front, *rear;
} Queue;

// A hash (Collection of pointers to Queue Nodes)
typedef struct Hash
int capacity; // how many pages can be there
QNode* *array; // an array of queue nodes
} Hash;

// A utility function to create a new Queue Node. The queue Node
// will store the given ‘pageNumber’
QNode* newQNode( unsigned pageNumber )
// Allocate memory and assign ‘pageNumber’
QNode* temp = (QNode *)malloc( sizeof( QNode ) );
temp->pageNumber = pageNumber;

// Initialize prev and next as NULL
temp->prev = temp->next = NULL;

return temp;

// A utility function to create an empty Queue.
// The queue can have at most ‘numberOfFrames’ nodes
Queue* createQueue( int numberOfFrames )
Queue* queue = (Queue *)malloc( sizeof( Queue ) );

// The queue is empty
queue->count = 0;
queue->front = queue->rear = NULL;

// Number of frames that can be stored in memory
queue->numberOfFrames = numberOfFrames;

return queue;

// A utility function to create an empty Hash of given capacity
Hash* createHash( int capacity )
// Allocate memory for hash
Hash* hash = (Hash *) malloc( sizeof( Hash ) );
hash->capacity = capacity;

// Create an array of pointers for refering queue nodes
hash->array = (QNode **) malloc( hash->capacity * sizeof( QNode* ) );

// Initialize all hash entries as empty
int i;
for( i = 0; i < hash->capacity; ++i )
hash->array[i] = NULL;

return hash;

// A function to check if there is slot available in memory
int AreAllFramesFull( Queue* queue )
return queue->count == queue->numberOfFrames;

// A utility function to check if queue is empty
int isQueueEmpty( Queue* queue )
return queue->rear == NULL;

// A utility function to delete a frame from queue
void deQueue( Queue* queue )
if( isQueueEmpty( queue ) )

// If this is the only node in list, then change front
if (queue->front == queue->rear)
queue->front = NULL;

// Change rear and remove the previous rear
QNode* temp = queue->rear;
queue->rear = queue->rear->prev;

if (queue->rear)
queue->rear->next = NULL;

free( temp );

// decrement the number of full frames by 1

// A function to add a page with given ‘pageNumber’ to both queue
// and hash
void Enqueue( Queue* queue, Hash* hash, unsigned pageNumber )
// If all frames are full, remove the page at the rear
if ( AreAllFramesFull ( queue ) )
// remove page from hash
hash->array[ queue->rear->pageNumber ] = NULL;
deQueue( queue );

// Create a new node with given page number,
// And add the new node to the front of queue
QNode* temp = newQNode( pageNumber );
temp->next = queue->front;

// If queue is empty, change both front and rear pointers
if ( isQueueEmpty( queue ) )
queue->rear = queue->front = temp;
else // Else change the front
queue->front->prev = temp;
queue->front = temp;

// Add page entry to hash also
hash->array[ pageNumber ] = temp;

// increment number of full frames

// This function is called when a page with given ‘pageNumber’ is referenced
// from cache (or memory). There are two cases:
// 1. Frame is not there in memory, we bring it in memory and add to the front
// of queue
// 2. Frame is there in memory, we move the frame to front of queue
void ReferencePage( Queue* queue, Hash* hash, unsigned pageNumber )
QNode* reqPage = hash->array[ pageNumber ];

// the page is not in cache, bring it
if ( reqPage == NULL )
Enqueue( queue, hash, pageNumber );

// page is there and not at front, change pointer
else if (reqPage != queue->front)
// Unlink rquested page from its current location
// in queue.
reqPage->prev->next = reqPage->next;
if (reqPage->next)
reqPage->next->prev = reqPage->prev;

// If the requested page is rear, then change rear
// as this node will be moved to front
if (reqPage == queue->rear)
queue->rear = reqPage->prev;
queue->rear->next = NULL;

// Put the requested page before current front
reqPage->next = queue->front;
reqPage->prev = NULL;

// Change prev of current front
reqPage->next->prev = reqPage;

// Change front to the requested page
queue->front = reqPage;

// Driver program to test above functions
int main()
// Let cache can hold 4 pages
Queue* q = createQueue( 4 );

// Let 10 different pages can be requested (pages to be
// referenced are numbered from 0 to 9
Hash* hash = createHash( 10 );

// Let us refer pages 1, 2, 3, 1, 4, 5
ReferencePage( q, hash, 1);
ReferencePage( q, hash, 2);
ReferencePage( q, hash, 3);
ReferencePage( q, hash, 1);
ReferencePage( q, hash, 4);
ReferencePage( q, hash, 5);

// Let us print cache frames after the above referenced pages
printf (“%d “, q->front->pageNumber);
printf (“%d “, q->front->next->pageNumber);
printf (“%d “, q->front->next->next->pageNumber);
printf (“%d “, q->front->next->next->next->pageNumber);

return 0;

Grand Paper Writers
Order NOW For A 10% Discount!
Pages (550 words)
Approximate price: -

Our Advantages

Plagiarism Free Papers

All papers are written by the best professional writers to ensure 100% originality. We always provide plagiarism reports whenever we deliver completed papers.

Free Revisions

All papers by Grand Paper Writers are completed and submitted on time. This timely delivery of papers gives you time to go through the paper before the official deadline.


As an additional service, we will provide a title page that precedes the contents of your paper. Here, you will provide your personal details.


We also ensure that we provide an extra page for the references or bibliographies following referencing rules.

Originality & Security

At, we guarantee students for the provision of security and original work. All your personal information is handled with confidentiality and is not shared with third parties. Additionally, we ensure that we provide original content with accompanying plagiarism reports to show originality.

24/7 Customer Support

Our customer support team is always available 24/7 to provide instant responses to any queries raised by students.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services has the best professional essay writers for quality services.


Flexible Pricing

Here, a, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.


Admission Help & Client-Writer Contact provides an interactive portal where students can communicate directly to their writers.


Paper Submission

Here, a, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.


Customer Feedback appreciates feedback from our clients to help us improve the delivery of essay writing services. As such, we are constantly changing our policies to ensure maximum customer and writer satisfaction.