Two simple memory management page replacement algorithms (FIFO and LRU) page-replacement algorithm in C Language

Source: Internet
Author: User

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 ');
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.