java实现fifo算法_java实现页面替换算法(LRU、LFU、FIFO)

2371833771.jpg

34.jpg

缓存算法(淘汰算法),常见算法有LRU、LFU和FIFO等算法,每种算法各有各的优势和缺点及适应环境。

PAGE REPLACEMENT POLICIES

When page fault occurs, the referenced page must be loaded.

If there is no available frame in memory, then one page is selected for replacement.

If the selected page has been modified, it must be copied back to disk (swapped out).

A page replacement algorithm is said to satisfy the inclusion property or is called a stack algorithm if the set of pages in an n-frame memory is always a subset of the pages in a(n + 1) frame memory.

FIFO (First IN, First OUT)

FIFO implements a queue.

A FIFO replacement algorithm links with each page the time when that page was added into the memory

The oldest page is chosen when a page is going to be replaced. We can create a FIFO queue to hold all the pages present in the memory disk. At the head of the queue we replace the page. We insert page at the tail of the queue when a page is added into the memory disk.

Implementation:

1.Two arrays, page[n] and frame[f_size] (queue), where n is the number of pages and f_size is the size of the frame buffer

2.When there is page fault, it replaces the page in the frame after the previously replaced frame

LRU (Least Recently Used)

On a page fault, the frame that was least recently used is replaced.

Implementation:

1.Two arrays, page[n] and frame[f_size] (queue), where n is the number of pages and f_size is the size of the frame buffer

2.Two additional arrays, a[f_size] & b[f_size], where a[] stores the sorted list of pages from most recently used to least recently used and b is the temporary array used to update the list

3.When page fault occurs, it finds the index of the LRU from frame[] based on the last element of a[] and replaces that page

4.Each time a page is referenced, update a[]

LFU (Least Frequently Used)

The page which has the smallest count is going to be replaced. The reason for this selection is that a mostly used page should have a larger reference count.

This algorithm suffers from the situation in which a page is used heavily during the staring phase of aprocess, but then is never again. Since it was used heavily, it has a large frequency count and remains in memory even if it is no longer needed.

Implemention:

1.Two arrays, page[n] and frame[f_size], where n is the number of pages and f_size is the size of the frame buffer

2.An array cnt[f_size] is used to store and keep track of the tally or frequency of usage of the pages

3.When a page fault occurs, it replaces the page with the least frequency of usage

4.If there are more than 1 page that the least frequency of usage, use FIFO logic and replace the page that came first among those least frequently used pages.

ReplacementPolicy.java

package replacementpolicy;

import java.util.*;

class ReplacementPolicy{

public static void main(String args[]){

Scanner scan = new Scanner(System.in);

int frameSize, page=0, choice, n; //Declare variables for: frame size, page, choice, and size n

String inputString; //String variable for the input string and array of Strings for the pages

String pages[];

String frame[]; //The array for the frames

do{

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值