Algorithm serial (7) - 3 page placement algorithms of operating systems

xiaoxiao2021-03-05  22

1. Problem Description and Design Thoughts: During the process of running, if the page they want to access does not transfer them into memory, the memory has no shouts, in order to ensure that the process can function properly, the system must be in memory Touch a page of a program or data to send a disk. However, which page should be called out, so it is necessary to determine according to a certain algorithm. The following is the design idea of ​​three algorithms. Optimal: Best Replacement Algorithm. The selected page, will be never used later, or the page that is no longer accessed within the longest (future) time. FiFo: First-find the algorithm. The algorithm is always eliminated by the page that enters the memory, selects the page to eliminate the length of the longest resident time in memory. LRU: Recently, the replacement algorithm is not used. The algorithm gives each page access field to record the time t that the page experienced since the last visit, and when you need to eliminate a page, select the maximum amount of T value in the existing page to eliminate.

#include

#define bsize 3 # Define psize 20

Struct PageInfor {INT Content; // Type Int Timer; // Access Tag};

Class PRA {Public: PRA (Void); Int FindSpace (Void); / / Find if there is a free memory INT FINDEXIST (INT CURPAGE); / / Find in memory if there is this page INT FINDREPLACE (VOID); // Find to Replacement page Void Display (void); // Displays Void FIFO (Void); // FIFO Algorithm Void Lru (Void); // LRU Algorithm Void Optimal (Void); // Optimal Algorithm Void BlockClear (Void); // Block Recovery PageInfor * block; // Physical Block PageInfor * Page; // Page Number Strings

Private:

}

PRA :: PRA (void) {INT QString [20] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7 0, 1};

Block = new pageinfor [bsize]; for (int i = 0; i

Page = new pageinfor [psize]; for (i = 0; i

INT PRA :: FindSpace (void) {for (int i = 0; i

INT PRA :: FINDEXIST (int Curpage) {

For (int i = 0; i

Return -1;

INT PRA :: FindReplace (void) {INT POS = 0;

For (int i = 0; i = block [pOS] .timer) POS = i; // finds to replace the page, return to Block position Return POS;} Void PRA :: Display (void) {

For (int i = 0; i

Void Pra :: Optimal (void) {Int exist, space, position;

For (int i = 0; i

Void Pra :: lru (void) {Int exist, space, position;

For (int i = 0; i

Void PRA :: FIFO (void) {

Int exist, space, position;

For (int i = 0; i

Else {space = FINDSPACE (); if (space! = -1) {Block [Space] = Page [i]; display ();} else {position = FindReplace (); block [position] = page [i]; Display ();}} for (int J = 0; j

Void main (void) {cout << "| ---------- page placement algorithm ------------" << Endl; cout << "| --- Power by ZhanjianTao (028054115) --- | "<< endl; cout <<" | -------------------------------------------------------------------------------------------------------------------------------------------- ----- | "<< Endl; cout <<" page number reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 "<< endl; cout <<" -------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------- "<< endl; cout <<" Select <1> Apply Optimal Algorithm "<< Endl; Cout <<" Select <2> App FIFO Algorithm "<< Endl; Cout <<" Select <3> Application LRU Algorithm "<< Endl; Cout <<" Select <0> Exit "<< Endl; int SELECT; PRA TEST;

While (select) {cin >> select; switch (select) {case 0: Break; case 1: cout << "Optimal algorithm results are as follows:" << endl; test.optimal (); test.blockclear (); cout << "----------------------" << Endl; Break; Case 2: cout << "The results of the FIFO algorithm are as follows:" << Endl; test .Fifo (); test.blockclear (); cout << "----------------------" << Endl; Break; cas 3: cout << "The LRU algorithm is as follows:" << Endl; test.lru (); test.blockclear (); cout << "----------------------" << endl; break; default: cout << "Please enter the correct function number" << Endl; Break;}}}

转载请注明原文地址:https://www.9cbs.com/read-36252.html

New Post(0)