"Operating system-memory management" Implementation of a simple clock replacement algorithm

Source: Internet
Author: User

Analysis: The clock replacement algorithm is realized by using cyclic queue. In the operating system, the textbook explains this.

Code implementation:

#include <iostream>
using namespace Std;
typedef struct NUMBERNODE
{
int *data;
BOOL *ask;//Visit question mark
}numnode;
typedef struct NODE
{
Numnode Numnode;
int maxsize;
int rear;
int front;
}queue;
queue* Create ();
void push (queue *);
void Pop (queue *);
int Turnyemian (queue *myque,int Telem);
queue* Create ()
{
Queue *myque=new queue;
myque->numnode.data=new int;
myque->numnode.ask=new bool;
myque->rear=0;
myque->front=0;
int i;
int m;
printf ("How many elements are there in a queue? \ n");
scanf ("%d", &m);
for (i=0;i<=m;i++)
myque->numnode.ask[i]=false;
myque->maxsize=m+1;
return myque;
}
void push (Queue *myque)
{
if ((myque->rear+1)%myque->maxsize!=myque->front)
{
printf ("Please enter the page number you are visiting? \ n");
scanf ("%d", &myque->numnode.data[myque->rear]);
myque->numnode.ask[myque->rear]=true;
Myque->rear= (myque->rear+1)%myque->maxsize;
}
Else
{
int Telem;
printf ("This time the queue is full of pages, to be page replacement \ n");
printf ("page number you want to visit? \ n");
scanf ("%d", &telem);
Turnyemian (Myque,telem);
}
}
int Turnyemian (Queue *myque,int Telem)
{
int key=myque->front;
while (1)
{
if ((Myque->numnode.ask[key]==true) && ((key+1)%myque->maxsize!=myque->front))
{
myque->numnode.ask[key]=false;
key= (key+1)%myque->maxsize;
}
if ((key+1)%myque->maxsize==myque->front)
{
key= (key+1)%myque->maxsize;
}
if (Myque->numnode.ask[key]==false)
{
myque->numnode.data[key]=telem;//to replace this page with the value of Telem
myque->numnode.ask[key]=true;
printf ("Replace this page with%d \ n", Myque->numnode.data[key]);
return 0;
}
}
}
void Pop (queue *myque)
{
if (myque->front!=myque->rear)
{
printf ("You have deleted the Loop queue element%d", Myque->numnode.data[myque->front]);
Myque->front= (myque->front+1)%myque->maxsize;
}
Else
{
printf ("The queue is empty and can no longer be deleted");
}

}
void Prints (queue *myque)
{
int p=myque->front;
while (P!=myque->rear)
{
if (P==myque->front)
{
printf ("%d", myque->numnode.data[p]);
p++;
}
Else
{
printf ("<-%d", myque->numnode.data[p]);
p++;
}
}

printf ("\ n");
}
int main (void)
{
Queue *myque=new queue;
Myque=create ();
int key;
while (1)
{
printf ("************* Please choose the following: *************\n");
printf ("1. add element \ n");
printf ("2. Delete element \ n");
printf ("3. Show loop queue \ n");
printf ("4. Exit system \ n");
scanf ("%d", &key);
Switch (key)
{
Case 1:push (Myque);
Case 2:pop (Myque);
Case 3:prints (Myque);
Case 4:return 0;
Default
Break
}
}
return 0;
}

Results show:


The original 3 pages


First page substitution


Second replacement


Third replacement


Fourth time replacement


At this point, four permutations are complete.

"Operating system-memory management" Implementation of a simple clock replacement algorithm

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.