A first-out replacement algorithm, and a last-most-used algorithm, simulates the process.
/* Page replacement algorithm, which is a queue operation.
*/
# Include <stdio. h>
# Include <conio. h>
# Include <time. h>
# Include <stdlib. h>
// Number of pages available in memory
# Define memery_page_num 3
// Data structure of the FIFO Page Replacement Algorithm
Struct restore oqueue
{
Int number; // number of data
Int data [memery_page_num]; // data (page code)
} FIFO;
// Initialization
Void Init ();
// A group of operations on columns
// Add Columns
Void add (INT pageid );
// Find the specified data in the column
Int find (INT pageid );
// Print
Void printqueue ();
Int main ()
{
Init ();
// Random 10 Data Tests
Time_t T;
Srand (unsigned (Time (& T )));
Int I;
Int TMP;
For (I = 0; I <10; ++ I)
{
TMP = rand () % 10;
Add (TMP );
Printqueue ();
}
Return 0;
}
Void Init ()
{
FIFO. Number = 0;
Int I;
For (I = 0; I <memery_page_num; I ++)
{
FIFO. Data [I] =-1;
}
}
Void add (INT pageid)
{
Printf ("add page % d/N", pageid );
If (FIFO. Number <memery_page_num)
{
FIFO. Data [FIFO. Number ++] = pageid;
}
Else
{
// Page replacement is not required when pages are in the memory.
If (! Find (pageid) // the page to be found does not exist in memory
{
// Move the data to one unit left and add new data to the right
Int I;
For (I = 0; I <MEMERY_PAGE_NUM-1; I ++)
{
FIFO. Data [I] = FIFO. Data [I + 1];
}
// Add new data
FIFO. Data [I] = pageid;
}
}
}
Int find (INT pageid)
{
Int I;
For (I = 0; I <memery_page_num; I ++)
{
If (FIFO. Data [I] = pageid)
{
Return 1;
}
}
Return 0;
}
Void printqueue ()
{
Printf ("****************************/N" );
Int I;
Putch ('| ');
For (I = 0; I <memery_page_num; I ++)
{
If (FIFO. Data [I] =-1)
Printf ("| ");
Else
Printf ("% d |", FIFO. Data [I]);
}
Putch ('/N ');
}
// The page conversion algorithm has not been used recently
# Include <stdio. h>
# Include <conio. h>
# Include <time. h>
# Include <stdlib. h>
// Specify the number of pages in memory.
# Define memery_page_num 5
// Define a special queue
Struct lruqueue
{
Int number;
Int data [memery_page_num];
} LRU;
// Initialization
Void Init ();
// Define a group of operations
Void add (INT pageid );
Int find (INT pageid );
Void Update (int pos );
Void printqueue ();
Int main ()
{
Init ();
// Random 10 Data Tests
Time_t T;
Srand (unsigned (Time (& T )));
Int I;
Int TMP;
For (I = 0; I <10; ++ I)
{
TMP = rand () % 10;
Add (TMP );
Printqueue ();
}
Return 0;
}
Void add (INT pageid)
{
Printf ("add Memory Page % d/N", pageid );
If (LRU. Number <memery_page_num)
{
Int Pos = find (pageid );
If (POS)
{
Update (POS );
}
Else
{
LRU. Data [LRU. Number ++] = pageid;
}
}
Else
{
Int Pos = find (pageid );
If (POS)
{
Update (POS );
}
Else
{
// Move the data to one unit left and add new data to the right
Int I;
For (I = 0; I <MEMERY_PAGE_NUM-1; I ++)
{
LRU. Data [I] = LRU. Data [I + 1];
}
// Add new data
LRU. Data [I] = pageid;
}
}
}
Int find (INT pageid)
{
Int I;
For (I = 0; I <memery_page_num; I ++)
{
If (LRU. Data [I] = pageid)
{
Return I;
}
}
Return 0;
}
Void Update (int pos)
{
Int TMP = LRU. Data [POS];
Int I;
For (I = Pos; I <LRU. Number; ++ I)
{
LRU. Data [I] = LRU. Data [I + 1];
}
LRU. Data [LRU. Number-1] = TMP;
}
Void Init ()
{
LRU. Number = 0;
Int I;
For (I = 0; I <memery_page_num; I ++)
{
LRU. Data [I] =-1;
}
}
Void printqueue ()
{
Printf ("****************************/N" );
Int I;
Putch ('| ');
For (I = 0; I <memery_page_num; I ++)
{
If (LRU. Data [I] =-1)
Printf ("| ");
Else
Printf ("% d |", LRU. Data [I]);
}
Putch ('/N ');
}