FIFO Anomaly,,

Discussion in 'C++' started by mai, Apr 15, 2007.

  1. mai

    mai Guest

    Hi everyone,
    i'm trying to exhibit FIFO anomaly(page replacement algorithm),, I
    searched over 2000 random strings but i couldnt find any anomaly,, am
    i I doing it right?,, Please help,,,The following is the code,,

    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctime> // For time()
    #include <cstdlib> // For srand() and rand()
    using namespace std;

    struct mem
    {
    int page;
    int extr;
    };
    mem* memArr;
    mem temp;
    int* refPattern;
    int b_val;
    int* arra;

    // This is a function that counts the page faults
    int PageFaultCount(int noframe, int leng_patt){
    int page_fault_num = 0;
    int i,j,k,x,y,d,c;
    int boolValue = 0;
    int boo = 0;

    memArr = (mem*)malloc(noframe*sizeof(mem));
    memArr[0].extr = 1;
    memArr[0].page = refPattern[0];
    page_fault_num = 1;
    d = 1;
    c = 1;
    while ((c < leng_patt) && (d < noframe)){
    boo = 0;
    for(k=0; k<d; k++){
    if (memArr[k].page == refPattern[c]){
    boo++;
    }
    }
    if(boo == 0){
    memArr[d].extr = 0;
    memArr[d].page = refPattern[c];
    page_fault_num++;
    for(j=0; j<=d; j++){
    memArr[j].extr = memArr[j].extr + 1;
    }
    d++;
    }
    c++;
    }
    while ((c < leng_patt)&&(d == noframe)){

    boolValue = 0;
    for(j=0; j< noframe; j++){


    if (memArr[j].page == refPattern[c]){
    boolValue++;
    }
    }
    if(boolValue == 0){
    for(x=0 ; x<(noframe-1); x++){
    for(y=(x+1) ; y < noframe ; y++){
    if(memArr[x].extr > memArr[y].extr){
    temp = memArr[x];
    memArr[x] = memArr[y];
    memArr[y] = temp;
    }
    }
    }
    memArr[noframe-1].page = refPattern[c];
    memArr[noframe-1].extr = 0;
    page_fault_num++;
    for(k=0; k < noframe; k++){
    memArr[k].extr = memArr[k].extr + 1;
    }
    }
    c++;
    }

    return page_fault_num;
    }
    // this is a function that generate random strings
    void GenReferencePatternAndCountPageFault(int num_of_frames, int
    num_of_pages, int length_of_ref_patt){

    int a[20];
    int i,j,x,y,k;
    int min = 0;

    refPattern = (int*)malloc(length_of_ref_patt*sizeof(int));
    //create random reference string

    for(i=0; i<length_of_ref_patt; i++)
    refPattern = (rand()% num_of_pages) + min;

    // here i'm trying to save all the page faults in an array
    for(j = 1; j<= num_of_frames; j++){
    arra[j-1]= PageFaultCount(j ,length_of_ref_patt);

    }
    // and here im trying to find if a larger frame has larger page
    fault to detect the anomaly
    for(x=0 ; x<(num_of_frames-1); x++){
    for(y=(x+1) ; y < num_of_frames ; y++){
    if(a[x] < a[y]){
    b_val = 1;


    }
    }
    }



    }


    int main(){
    int ref_pattern_length, page_count,frame_count_limit, j;

    int i =1;
    b_val = 0;


    cout<<"Please enter the value of frame count limit";
    cin>> frame_count_limit;
    cout<<"Please enter the value page count :";
    cin>> page_count;
    cout<<"Please enter the length of reference pattern: ";
    cin>> ref_pattern_length;
    arra = (int*)malloc(frame_count_limit*sizeof(int));

    srand(time(0));
    while((i<=2000)&&(b_val == 0)){

    GenReferencePatternAndCountPageFault(frame_count_limit,
    page_count, ref_pattern_length);
    i++;
    }
    if(b_val == 1){
    cout<< "There is a FIFO anomaly
    }
    else
    if(b_val == 0){
    cout<<" no anomaly was detected; please try again with different
    input \n";}

    free(refPattern);
    free(arra);
    return 0;
    }
    mai, Apr 15, 2007
    #1
    1. Advertising

  2. mai

    Howard Guest

    "mai" <> wrote in message
    news:...
    > Hi everyone,
    > i'm trying to exhibit FIFO anomaly(page replacement algorithm),, I
    > searched over 2000 random strings but i couldnt find any anomaly,, am
    > i I doing it right?,, Please help,,,The following is the code,,


    I'm not sure how many people here would have any idea what a "FIFO anomaly"
    is. I sure don't.

    We don't know what input you're using, or what you expect the output to be
    for any given input. Perhaps you could give us a specific example of some
    input, its expected result, and what you're seeing instead. You should also
    use a debugger to step through the code and see what it's doing at various
    stages. Or add some output statements at various points to report what it's
    doing. (Or both.)

    -Howard
    Howard, Apr 16, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page