先进先出(FIFO)虚拟存储管理页面淘汰算法(C)

#include
#include
#include

int indexOf(char *s, char c, int len) {
    for (int i = 0; i < len; i++) {
        if (s[i] == c) {
            return i;
        }
    }

    return -1;
}

void moveForward(char *s, char c, int len) {
    len--;
    for (int i = 0; i < len; i++) {
        s[i] = s[i + 1];
    }
    s[len] = c;
}

void main() {
    // 进程允许拥有的页数
    int pages;
    // 页面访问字符串
    char s[100];
    printf("Number of pages allowed by the process: ");
    scanf("%d", &pages);
    printf("Please enter an access string: ");
    scanf("%s", s);


    char* memory;
    // 缺页次数
    int times = 0;
    int index = 0;

    for (int i = 0, len = strlen(s); i < len; i++) {
        char c = s[i];
        if (index < pages) {
            if (indexOf(memory, c, pages) == -1) {
                memory[index] = c;
                index++;
                times++;
            }
        } else {
            if (indexOf(memory, c, pages) == -1) {
                printf("eliminate %c \n", memory[0]);
                moveForward(memory, c, pages);
                times++;
            }
        }
    }
    printf("Total number of page missing: %d", times);
}

你可能感兴趣的:(算法,操作系统)