Numerical on LRU, FIFO


In this article, we will explore LRU(Least Recently Used Algorithm) and FIFO(First In First Out) numerical, their flowcharts and their use cases as well.

Least Recently Used Algorithm

Popular page replacement algorithms used in operating systems and cache management systems include the LRU (Least Recently Used) technique. By replacing the least recently used page in a cache when a new page needs to be loaded, it seeks to reduce the amount of page faults.

Example − Let's assume we have a cache with a capacity of 3 pages and a sequence of page requests: 2, 3, 1, 4, 2, 1, 5, 2.

Numerical of LRU (Least Recently Used) Algorithm

Step 1 − Initially, the cache is empty.

 Page Request

 Cache

 Cache State

2

Step 2 − Page 2 is requested, and since the cache is empty, it is loaded into the cache.

 Page Request

Cache

 Cache State

2

 2

2

Step 3 − Page 3 is requested, and it is not in the cache. Page 2 is the least recently used page, so it is replaced with page 3.

Page Request

Cache

Cache State

3

2

 3

Step 4 − Page 1 is requested, and it is not in the cache. Page 2 is the least recently used page, so it is replaced with page 1.

Page Request

Cache

Cache State

1

2

 3,1

Step 5 − Page 4 is requested, and it is not in the cache. Page 2 is the least recently used page, so it is replaced with page 4.

Page Request

Cache

Cache State

4

4

3,1

Step 6 − Page 2 is requested, and it is not in the cache. Page 3 is the least recently used page, so it is replaced with page 2.

Page Request

Cache

Cache State

2

4

1,2

Step 7 − Page 1 is requested, and it is in the cache. The cache remains unchanged.

Page Request

Cache

Cache State

1

4

1,2

Step 8 − Page 5 is requested, and it is not in the cache. Page 4 is the least recently used page, so it is replaced with page 5.

Page Request

Cache

Cache State

5

5

1,2

Step 9 − Page 2 is requested, and it is in the cache. The cache remains unchanged.

Page Request

Cache

Cache State

2

5

1,2

LRU Algorithm Summary −

$$Cache state: 1, 2$$

$$Number of page faults: 6$$

Flow chart of LRU algorithm

Use cases of LRU algorithm

There are several applications for the LRU (Least Recently Used) algorithm in computer science and software engineering. Here are some typical situations where LRU is used −

Network Packet Buffering − LRU is used in network packet buffering, a method of temporarily storing packets in memory as they wait to be processed or transmitted. When the buffer is full, LRU helps manage the buffer by removing the oldest packets, ensuring timely handling of network traffic.

Web Browser History − LRU can be used to control the browser history in web browsers. To keep the history reasonable, the most recent websites are kept there while the least recent ones are gradually eliminated.

First-In-First-Out Algorithm

Operating systems and cache management systems use the First-In-First-Out (FIFO) method, which is a straightforward page replacement mechanism. According to the first-in-first-out principle, the page that has been in the cache for the longest time is removed.

Example − Let's assume we have a cache with a capacity of 3 pages and a sequence of page requests: 2, 3, 1, 4, 2, 1, 5, 2.

Numerical of FIFO (First-In-First-Out) Algorithm −

Step 1 − Initially, the cache is empty.

Page Request

Cache

Cache State

2

Step 2 − Page 2 is requested, and since the cache is empty, it is loaded into the cache.

Page Request

Cache

Cache State

2

2

2

Step 3 − Page 3 is requested, and it is not in the cache. The cache is full, so we replace the oldest page, which is page 2, with page 3.

Page Request

Cache

Cache State

3

2

3

Step 4 − Page 1 is requested, and it is not in the cache. The cache is full, so we replace the oldest page, which is page 2, with page 1.

Page Request

Cache

Cache State

1

2

3,1

Step 5 − Page 4 is requested, and it is not in the cache. The cache is full, so we replace the oldest page, which is page 3, with page 4.

Page Request

Cache

Cache State

4

4

3,1

Step 6 − Page 2 is requested, and it is in the cache. The cache remains unchanged.

Page Request

Cache

Cache State

2

4

3,1

Step 7 − Page 1 is requested, and it is in the cache. The cache remains unchanged.

Page Request

Cache

Cache State

1

4

3,1

Step 8 − Page 5 is requested, and it is not in the cache. The cache is full, so we replace the oldest page, which is page 4, with page 5.

Page Request

Cache

Cache State

5

5

3,1

Step 9 − Page 2 is requested, and it is in the cache. The cache remains unchanged.

Page Request

Cache

Cache State

2

5

3,1

FIFO Algorithm Summary −

$$Cache state: 3, 1$$

$$Number of page faults: 5 $$

Flow chart of FIFO algorithm

Use cases of FIFO algorithm

The First-In-First-Out (FIFO) method has numerous applications across numerous areas. Here are a few typical situations when FIFO is used −

Print Queue Management − The FIFO algorithm is frequently used to manage print queues in networked printing systems. To ensure fairness and maintain the submission sequence, print jobs are processed in the order that they are received.

Data Streaming and Buffering − When buffering data streams, FIFO is used to temporarily store incoming data in a buffer before processing it. By doing this, the integrity and sequence of the data stream are maintained, ensuring that the data is processed in the order it was received.

Conclusion

Two popular page replacement algorithms used in operating systems and cache management are LRU (Least Recently Used) and FIFO (First-In-First-Out).

When a new page needs to be loaded into the cache, the LRU algorithm replaces the least recently used page. It is predicated on the idea that the page that has been accessed the least lately is the one that is least likely to be revisited soon. The least recently used page may be quickly identified and removed thanks to the LRU algorithm, which keeps track of the order in which pages are accessed.

On the other hand, when a new page needs to be loaded, the FIFO algorithm replaces the one that has been in the cache for the longest. It removes the page that loaded initially in a first-in, first-out arrangement. This algorithm does not take into account the order of page accesses.

Updated on: 17-Jul-2023

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements