FIFO invocation algorithm (incomplete version)

Source: Internet
Author: User
Tags rand random seed

#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define STACK_SIZE 4
struct aa{//node representing a physical page
int page; Virtual memory page number stored on physical page
Aa* Next; Point to next physical page
};
typedef struct
{
int elem[stack_size];
int top;
int top2;
}seq;

void Soseq (Seq *s)
{s->top=-1;
s->top2=0;
}

Void Main ()
{
Srand ((unsigned) time (0));//random seed
int i,addr,pagen,ii,m,answer,ffalse,count with current times;
Double sum;
AA *head,*tail,*temp,*table;
ffalse=0;//page failure (number of misses)
Answer=0;
int pagenums[150]; Store page address stream
m=4;//4 Physical pages
Table=new (AA);//Establish First physical page node
head=table;//link header pointer to node
table->page=-1;// Initial value 1 means that no virtual memory page is placed
temp=table;//temporary pointer to the first physical page
for (ii=2;ii<=m;ii++)//Continue Building M-1 physical page
{table=new (AA); Create a new physical page node
Table->page=-1;
temp->next=table; The previous node's next points to this node, forming the linked list
temp=table;//temporary pointer to new physical page node
if (ii==m) {table->next=null;}//Last physical page node next pointer to null
}
tail=table;//tail pointer to the end of the list
Count=0;
i=0;

while (i<150)//per loop, constructs an address in 150 addresses of the address stream
{
if (count==0) {addr = (rand ()%120+1)%120; pagen = ADDR/10;} The
///count=0 means that the command address of the [0,119] is randomly selected together with a point m, the order
//executes an instruction (+1), that is, the address of the m+1 is obtained; Pagenum is the page number of the location.
if (count==1) {addr =rand ()% (addr + 1); Pagen = ADDR/10;}
//Count=1 indicates that an instruction is randomly selected and executed in the previous address [0,m+1], and that the address of the instruction is M ';
if (count==2) {Pagen = ((addr + 1)%120)/10;}
//sequentially executes an instruction whose address is M ' +1;
if (count==3) {Pagen = (rand ()% (120-(addr+2)) + addr +2)%120/10;}
//In the post-address [M ' +2,119] randomly select an instruction and execute;//
++count;
Pagenums[i]= Pagen;
if (count==4) {count=0;}//Enter the next 4 cycles, then 4 steps to generate 4 addresses
i++;
}
//Output page address stream
for (i=0;i<150;i++) {cout<< pagenums[i]<< "";}
cout<<endl;
//----------------------------------------------------------------------------------------
//The following is the least recently used algorithm for FIFO

Seq *s1;
s1= (seq *) malloc (sizeof (SEQ));
Soseq (S1);
int y=0;
for (int q=0;q<150;q++)
{int x=pagenums[q];
if (s1->top!=stack_size-1)
{s1->top++;
S1->elem[s1->top]=x;}
Else
if (s1->elem[0]!=x&&s1->elem[1]!=x&&s1->elem[2]!=x&&s1->elem[3]!=x)
{s1->elem[s1->top2]=x;
s1->top2++;
y++;
if (s1->top2==3)
{s1->top2=0;}
}
}
sum=1.0-y/120.0;
cout<< "FIFO algorithm hit rate:" <<sum<<endl;
}

FIFO invocation algorithm (incomplete version)

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.