任务是模拟虚拟内存页面的最近最少使用 (LRU) 替换。编写一个 C 程序,lru.c,它接受两个参数,都是整数。这将分别是模拟物理内存的页面大小和模拟虚拟内存的页面大小。lru.c 然后应该从标准输入读取整数直到 EOF。每个整数将是被访问的虚拟页面的编号。lru.c 应该打印一行输出以指示给定大小的物理内存和 LRU 替换发生了什么动作。下面有机翻,请专家写一下内容
// Simulate LRU replacement of page frames
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
// represent an entry in a simple inverted page table
typedef struct ipt_entry {
int virtual_page; // == -1 if physical page free
int last_access_time;
} ipt_entry_t;
void lru(int n_physical_pages, int n_virtual_pages);
void access_page(int virtual_page, int access_time, int n_physical_pages, struct ipt_entry *ipt);
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <n-physical-pages> <n-virtual-pages>\n", argv[0]);
return 1;
}
lru(atoi(argv[1]), atoi(argv[2]));
return 0;
}
void lru(int n_physical_pages, int n_virtual_pages) {
printf("Simulating %d pages of physical memory, %d pages of virtual memory\n",
n_physical_pages, n_virtual_pages);
struct ipt_entry *ipt = malloc(n_physical_pages * sizeof *ipt);
assert(ipt);
for (int i = 0; i < n_physical_pages; i++) {
ipt[i].virtual_page = -1;
ipt[i].last_access_time = -1;
}
int virtual_page;
for (int access_time = 0; scanf("%d", &virtual_page) == 1; access_time++) {
assert(virtual_page >= 0 && virtual_page < n_virtual_pages);
access_page(virtual_page, access_time, n_physical_pages, ipt);
}
}
// if virtual_page is not in ipt, the first free page is used
// if there is no free page, the least-recently-used page is evicted
//
// a single line of output describing the page access is always printed
// the last_access_time in ipt is always updated
void access_page(int virtual_page, int access_time, int n_physical_pages, struct ipt_entry *ipt) {
// PUT YOUR CODE HERE TO HANDLE THE 3 cases
//
// 1) The virtual page is already in a physical page
//
// 2) The virtual page is not in a physical page,
// and there is free physical page
//
// 3) The virtual page is not in a physical page,
// and there is no free physical page
//
// don't forgot to update the last_access_time of the virtual_page
printf("Time %d: virtual page %d accessed\n", access_time, virtual_page);
}
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。