Operating system page replacement algorithm experiment page replacement algorithm experiment operating system experiment

Posted by I Am Chris on Tue, 25 Jan 2022 11:30:34 +0100

Design experiment report of Software College

College: Software College; major: Computer Science and technology; grade / class: Java class 2, grade 19

The second semester of 2020-2021 academic year

Course name

operating system

Instructor

Student ID name

Wei Yiming

Experimental site

Laboratory 111, computer college building

Experimental time

June 11, June 18 10:10-11:50

entry name

Page replacement algorithm

Type of experiment

Comprehensive

  • Experimental purpose

1. Deepen the understanding of virtual memory.

2. Master the implementation principle of common page replacement algorithm.

    • OPT page replacement algorithm

The pages selected by OPT to be eliminated are pages that have been transferred into memory and will never be used in the future, or pages that will not be accessed for the longest time. Therefore, how to find such pages is the key of the algorithm. A step variable can be set for each page, and its initial value is a large enough number. For pages that are not in memory, its value is reset to zero. For pages that are in memory, its value is reset to the distance between the currently accessed page and the page that appears for the first time later. Therefore, the larger the value, the page is no longer accessed for the longest time, You can select it as the checkout page.

    • FIFO page replacement algorithm

FIFO always selects the page that enters the memory first to be eliminated. Therefore, a first in first out busy page frame queue can be set. The page newly transferred into the memory is hung at the end of the queue. When there is no idle page frame, a page frame can be taken from the head of the queue as an idle page frame, and the required page can be transferred in.

    • LRU page replacement algorithm

LRU makes decisions according to the usage of pages after they are transferred into memory. It uses "recent past" as the approximation of "recent future", and selects the most recently unused pages to be eliminated. The algorithm is mainly realized by means of the access time in the page structure to record the last access time of a page. Therefore, when it is necessary to eliminate a page, select the page with the smallest time value among the pages in memory, that is, the most recently unused page.

    • LFU page replacement algorithm

LFU requires that a counter (i.e. counter in the page structure) be configured for each page. Once a page is accessed, the counter value will be increased by 1. When a page replacement needs to be selected, the page with the lowest counter value, i.e. the page with the least number of accesses in memory, will be selected for elimination.

    • NUR page replacement algorithm

NUR requires that an access bit be set for each page (the access bit can still be represented by counter in the page structure). When a page is accessed, its access bit counter is set to 1. When page replacement is required, the replacement algorithm checks each page in memory sequentially from the replacement pointer (pointing to the first page initially). If its access bit is 0, select the page to replace it. Otherwise, the replacement pointer moves down and continues to look down. If the page with access bit 0 is not found after all pages in memory are scanned, the replacement pointer will point to the first page again, and the access position of all pages in memory will be 0. When the next round of scanning starts, the page with counter 0 will be found.

  • Experimental equipment

linux system in virtual machine of laboratory room

  • Experimental requirements
  1. Based on the FIFO replacement algorithm, the LRU page replacement algorithm is written and implemented;
  2. According to the following access sequence: 12560365365604270435,

When the number of physical blocks is 3 and 4 respectively, FIFO and LRU algorithms are called respectively to calculate and output the corresponding hit rate.

  • Experimental code
    #include <stdio.h>
    
    /*Initialize queue*/
    void initializeList(int list[],int number){
        int i;
        for ( i = 0; i < number; i ++) {
            list[i] = -1;
        }
    }
    /*Show queue status*/
    void showList(int list[], int number){
       int i;
        for ( i = 0; i < number; i ++) {
            printf("%2d",list[i]);
        }
        printf("\n");
    }
    
    /*Show current memory status*/
    void showMemoryList(int list[],int phyBlockNum){
        int i;
    
        for ( i = 0; i < phyBlockNum; i ++) {
            if (list[i] == -1) {
                break;
            }
            printf(" |%d|",list[i]);
        }
        printf("\n");
    }
    
    void informationCount(int missingCount,int replaceCount,int pageNum){
        printf("Page missing times:%d   Page missing rate:%d/%d\n",missingCount,missingCount,pageNum);
        double result = (double)(pageNum - missingCount)/(double)pageNum;
        printf("Replacement times:%d  hit rate:%.2f\n",replaceCount,result);
    }
    
    /*Find the next location to visit for this page*/
    int getNextPosition(int currentPage,int currentPosition,int strList[],int pageNum){
        int i;
        for ( i = currentPosition+1; i < pageNum; i ++) {
            if (strList[i] == currentPage) {
                return i;
            }
        }
        
        return 100;
    }
    
    
    /*FIFO replacement algorithm*/
    void replacePageByFIFO(int memoryList[],int phyNum,int strList[],int pageNum){
        
        /*Replacement times*/
        int replaceCount = 0;
        /*Page missing times*/
        int missingCount = 0;
        
        /*Records the index of the current earliest entry into memory*/
        int pointer = 0;
        
        /*Record access to the current page: 0 not accessed*/
        int isVisited = 0;
        int i;
        for (i = 0; i < pageNum; i ++) {
            isVisited = 0;
            
            /*Determine whether replacement is needed - > the memory is full and the page to be accessed is not in memory*/
            int j;
            for (j = 0; j < phyNum; j ++) {
                if (memoryList[j] == strList[i]) {
                    /*The page already exists in memory*/
                    /*Modify access*/
                    isVisited = 1;
                    /*Modify access time*/
                    /*Exhibition*/
                    printf("%d\n",strList[i]);
                    break;
                }
                if (memoryList[j] == -1) {
                    /*Page is not in memory and memory is not full - > save directly*/
                    memoryList[j] = strList[i];
                    /*Modify access*/
                    isVisited = 1;
                    missingCount ++;
                   /* Exhibition*/
                    printf("%d\n",strList[i]);
                    showMemoryList(memoryList, phyNum);
                    break;
                }
            }
            
            if (!isVisited) {
                /*The current page has not been accessed - > page replacement is required*/
                /*Save this page directly into the recorded subscript*/
                memoryList[pointer] = strList[i];
                
                /*The subscript points to the next*/
                pointer ++;
                
                /*If you get to the last, set the subscript to zero*/
                if (pointer > phyNum-1) {
                    pointer = 0;
                }
                
                
                missingCount ++;
                replaceCount ++;
                
               /* Exhibition*/
                printf("%d\n",strList[i]);
                showMemoryList(memoryList, phyNum);
            }
        }
        informationCount(missingCount, replaceCount, pageNum);
    }
    
    
    
    /*The longest unused permutation algorithm*/
    void replacePageByLRU(int memoryList[],int phyNum,int strList[],int pageNum){
        
        /*Replacement times*/
        int replaceCount = 0;
        /*Page missing times*/
        int missingCount = 0;
    
        /*Record the time since the last access in memory*/
        int timeRecord[phyNum];
        /*initialization*/
        initializeList(timeRecord, phyNum);
    
        /*Record the access of the current page: 0 not accessed*/
        int isVisited = 0;
        
        /*Record the number of pages that are already in memory*/
        int pageCount = 0;
        int i;
        for ( i = 0; i < pageNum; i ++) {
            isVisited = 0;
            
           /* Time plus one*/
           int p;
            for (p = 0; p < pageCount; p ++) {
                if (memoryList[p] != -1) {
                    timeRecord[p] ++;
                }
            }
            
            /*Need replacement*/
            int j;
            for ( j = 0; j < phyNum; j ++) {
                if (memoryList[j] == strList[i]) {
                    /*The page already exists in memory*/
                    /*Modify access*/
                    isVisited = 1;
                   /*Reset access time*/
                    timeRecord[j] = -1;
                    /*Exhibition*/
                    printf("%d\n",strList[i]);
                    break;
                }
                if (memoryList[j] == -1) {
                    /*Page is not in memory and memory is not full - > save directly*/
                    memoryList[j] = strList[i];
                    pageCount ++;
                    /*Modify access*/
                    isVisited = 1;
                    /*Modify access time*/
                    timeRecord[j] ++;
                    
                    missingCount ++;
                    /*Exhibition*/
                    printf("%d\n",strList[i]);
                    showMemoryList(memoryList, phyNum);
                    break;
                }
            }
    
            if (!isVisited) {
               /* Need replacement*/
                /*1.Traverse the time record table to find the memory subscript of the page that has not been visited for the longest time*/
                int max = 0;
                int k;
                for ( k = 0; k < phyNum; k ++) {
                    if (timeRecord[max] < timeRecord[k]) {
                        max = k;
                    }
                }
    
                /*2.Change out the page of this location*/
                memoryList[max] = strList[i];
                timeRecord[max] = -1;
                
                missingCount ++;
                replaceCount ++;
    
                /*Exhibition*/
                printf("%d\n",strList[i]);
                showMemoryList(memoryList, phyNum);
                
            }
        }
        informationCount(missingCount, replaceCount, pageNum);
    }
    
    int main(int argc, const char * argv[]) {
        
        /*Number of physical blocks*/
        int phyBlockNum;
        printf("Please enter the number of physical blocks:\n");
        scanf("%d",&phyBlockNum);
        
        /*Generate memory queue*/
        int memoryList[phyBlockNum];
        /*Initialize memory state*/
        initializeList(memoryList, phyBlockNum);
        showMemoryList(memoryList,phyBlockNum);
        
        /*Number of pages*/
        int pageNum;
        printf("Please enter the total number of pages to visit:\n");
        scanf("%d",&pageNum);
        
        /*Save page number reference string*/
        int pageNumStrList[pageNum];
        printf("Please enter the page number to visit:\n");
        int i;
        for (i = 0; i < pageNum; i ++) {
            scanf("%d",&pageNumStrList[i]);
        }
        showList(pageNumStrList, pageNum);
        
        int chose;
        while (1) {
            printf("Please select the required replacement algorithm:\n");
            printf("1.FIFO 2.LRU 3.sign out\n");
            scanf("%d",&chose);
            
            switch (chose) {
                
                case 1:
                    showList(pageNumStrList, pageNum);
                    replacePageByFIFO(memoryList, phyBlockNum, pageNumStrList, pageNum);
                    /*Reinitialize memory*/
                    initializeList(memoryList , phyBlockNum);
                    break;
                case 2:
                    showList(pageNumStrList, pageNum);
                    replacePageByLRU(memoryList, phyBlockNum, pageNumStrList, pageNum);
                    /*Reinitialize memory*/
                    initializeList(memoryList, phyBlockNum);
                    break;
                default:
                    return 0;
                    break;
            }
        }
        
        return 0;
    }
    

  • Result analysis and summary

The number of physical blocks is 3

Call FIF0 algorithm result:

Result of calling LRU algorithm:

The physical block is 4

Result of calling FIFO algorithm:

Result of calling LRU algorithm: