FIFO算法,LRU算法,LFU算法的c语言代码
时间: 2024-05-22 19:09:43 浏览: 20
FIFO算法(先进先出):根据进入队列的先后顺序,优先淘汰最先进入队列的页面。
C语言代码实现:
```
#define MAXSIZE 10 // 假设物理块大小为10
int queue[MAXSIZE]; // 模拟队列
int front = 0; // 队头指针
int rear = 0; // 队尾指针
void FIFO(int page[], int n) {
int hit = 0, miss = 0;
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = 0; j < rear; j++) {
if (queue[j] == page[i]) { // 缺页率减1
hit++;
flag = 1;
break;
}
}
if (!flag) {
if (rear == MAXSIZE) { // 队列已满,删除队头元素
rear--;
miss++;
for (int j = 0; j < rear; j++) {
queue[j] = queue[j + 1];
}
}
queue[rear] = page[i]; // 将页面加入队尾
rear++;
miss++;
}
}
printf("FIFO: 命中次数:%d,缺页次数:%d,缺页率:%f\n", hit, miss, (float)miss / n);
}
```
LRU算法(最近最少使用):根据页面最近被访问的时间来淘汰长时间未被使用的页面。
C语言代码实现:
```
#define MAXSIZE 10 // 假设物理块大小为10
int stack[MAXSIZE]; // 模拟栈
int top = -1; // 栈顶指针
void LRU(int page[], int n) {
int hit = 0, miss = 0;
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = 0; j <= top; j++) {
if (stack[j] == page[i]) { // 缺页率减1
hit++;
flag = 1;
for (int k = j; k < top; k++) { // 将页面置于栈顶
stack[k] = stack[k + 1];
}
stack[top] = page[i];
break;
}
}
if (!flag) {
if (top == MAXSIZE - 1) { // 栈已满,删除栈底元素
top--;
miss++;
}
for (int j = top + 1; j > 0; j--) { // 将所有元素后移一位
stack[j] = stack[j - 1];
}
stack = page[i]; // 将页面置于栈顶
top++;
miss++;
}
}
printf("LRU: 命中次数:%d,缺页次数:%d,缺页率:%f\n", hit, miss, (float)miss / n);
}
```
LFU算法(最少使用):根据页面被访问的频率来淘汰使用频率最低的页面。
C语言代码实现:
```
#define MAXSIZE 10 // 假设物理块大小为10
int freq[MAXSIZE]; // 页面使用频率数组
int min_freq = 0x7fffffff; // 最小使用频率
int min_index; // 最小使用频率的页面下标
void LFU(int page[], int n) {
int hit = 0, miss = 0;
memset(freq, 0, sizeof(freq)); // 初始化页面使用频率数组为0
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = 0; j < MAXSIZE; j++) {
if (stack[j] == page[i]) { // 缺页率减1
hit++;
flag = 1;
freq[j]++; // 页面使用频率加1
break;
}
}
if (!flag) {
if (top == MAXSIZE - 1) { // 栈已满,删除使用频率最低的页面
for (int j = 0; j < MAXSIZE; j++) {
if (freq[j] < min_freq) {
min_freq = freq[j];
min_index = j;
}
}
stack[min_index] = page[i];
freq[min_index] = 1;
min_freq = 0x7fffffff;
miss++;
}
else {
stack[++top] = page[i];
freq[top] = 1;
miss++;
}
}
}
printf("LFU: 命中次数:%d,缺页次数:%d,缺页率:%f\n", hit, miss, (float)miss / n);
}
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)