为什么我的第二次机会页面替换寻呼机系统的功能不起作用?
该函数仅选择受害者帧并返回它。
如果有空闲帧,则返回它。否则,它必须根据 SC(第二次机会)算法选择当前正在使用的帧。
它返回所选帧的帧号,但不返回 更新所有寻呼机数据。
我的职能是:
// select the frame based on second chance, passing the page
uint64 sc_select_victim_frame(pager_data* pager)
{
// these first 4 lines its like fifo, if frame for this page is not occopied (free)
//then just return it,
then am just incrementing the sc_count for future indexing of fifo,
if(!(pager->frames[0].occupied)){
return pager->SC_count;
}
pager->SC_count +=1;
pager->SC_count = pager->SC_count % pager->num_frames;
for(uint64 i=0; i<pager->num_frames; i++){
if(pager->frames[i].occupied){
if(!pager->second_chance[i]){
pager->second_chance[i] = 1;
}
return i;
}
}
return pager->SC_count;
}
寻呼机结构具有:
typedef struct _pager_data{
uint64 num_pages, num_frames, page_sz, num_procs;
frame* frames;
page_table_entry** page_tables;
uint64 SC_count;
bool* second_chance; // I have created this array to track the second chance
} pager_data;
页表项结构为:
typedef struct _page_table_entry{
uint64 flags : 12;
uint64 frame : 40;
uint64 unused : 12;
} page_table_entry;
最后,我的框架是:
typedef struct _frame{
bool occupied;
uint64 pid, page_number;
} frame;
我已在寻呼机内添加了 bool* secondary_chance;
但我不确定这是否正确。我的第二次机会算法不正确吗?
为什么我的函数无法正常工作?
最佳答案
最后我的解决方案是;
uint64 sc_select_victim_frame(pager_data* pager)
{
if(!(pager->frames[0].occupied)){
pager->SC_count +=1;
pager->SC_count = pager->SC_count % pager->num_frames;
return pager->SC_count;
}
if(pager->second_chance[pager->SC_count] == 0){
pager->second_chance[pager->SC_count] = 1;
pager->SC_count +=1;
pager->SC_count = pager->SC_count % pager->num_frames;
return pager->SC_count;
}
pager->second_chance[pager->SC_count] = 0;
return pager->SC_count;
}
关于c - 第二次机会页面替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58892861/