Replacement Strategy - 19.5.2 | 19. Approximate LRU Implementation | Computer Organisation and Architecture - Vol 3
K12 Students

Academics

AI-Powered learning for Grades 8–12, aligned with major Indian and international curricula.

Professionals

Professional Courses

Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.

Games

Interactive Games

Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.

Interactive Audio Lesson

Listen to a student-teacher conversation explaining the topic in a relatable way.

Approximate LRU

Unlock Audio Lesson

0:00
Teacher
Teacher

Let's start with the Approximate LRU, which is a simpler version of the standard LRU. Can anyone explain how the reference bit works?

Student 1
Student 1

The reference bit is set to 1 whenever a page is accessed, right?

Teacher
Teacher

Exactly! And after a certain interval, we reset all reference bits to 0. This helps us select pages for replacement. Why do you think we do that?

Student 2
Student 2

It helps identify which pages haven't been accessed recently, so they’re probably less important?

Teacher
Teacher

Great observation! Pages with a reference bit of 0 are candidates for replacement. It’s a good approximation to LRU without needing complex hardware.

Student 3
Student 3

So it's like a memory refresh every interval?

Teacher
Teacher

Precisely! Let’s summarize: the approximate LRU uses reference bits to track page usage over time, allowing for efficient memory management.

Sampled LRU

Unlock Audio Lesson

0:00
Teacher
Teacher

Now, let’s talk about the Sampled LRU. Who can tell me how it builds on the Approximate LRU?

Student 2
Student 2

It adds a reference byte to keep track of page access over a period of time, right?

Teacher
Teacher

Exactly! At each time interval, instead of just resetting the reference bits, we copy them into a reference byte. Why do you think we do this?

Student 4
Student 4

It tracks the access pattern more effectively over several intervals?

Teacher
Teacher

Right! The value of this byte informs us about the access history. The page with the smallest reference byte is then chosen for replacement. This provides more information than just a single bit.

Student 1
Student 1

How can we determine which page to replace if two pages have the same byte value?

Teacher
Teacher

Good question! In that case, we’d follow FIFO rules to choose the one that came into memory first. Let's summarize: the Sampled LRU is an enhanced method that provides better tracking by leveraging a reference byte.

Clock and Modified Clock Algorithm

Unlock Audio Lesson

0:00
Teacher
Teacher

Next, we’ll explore the Clock Algorithm. How does this one differ from the others we discussed?

Student 1
Student 1

It uses a circular list to track pages!

Teacher
Teacher

Correct! When a page fault occurs, it checks each page in a circular manner. If the reference bit is 1, it gives a second chance by resetting it to 0. What happens if it finds a page with a reference bit of 0?

Student 3
Student 3

That page is selected for replacement, right?

Teacher
Teacher

Yes! Now, what about the Modified Clock Algorithm? How does it enhance this method?

Student 4
Student 4

It involves a dirty bit, which indicates whether a page has been modified or not!

Teacher
Teacher

Exactly! This helps the system decide to prefer clean pages for replacement, minimizing the overhead involved in writing dirty pages back to disk. So, we have a combination of reference and dirty bits to make smarter decisions!

Belady’s Anomaly

Unlock Audio Lesson

0:00
Teacher
Teacher

Finally, let's discuss Belady’s Anomaly. Has anyone heard of it?

Student 2
Student 2

Isn’t it when increasing the number of page frames leads to more page faults?

Teacher
Teacher

Correct! It’s counterintuitive, as we expect more frames to decrease faults. Can anyone provide a situation where this might happen?

Student 1
Student 1

When certain access patterns repeatedly reference pages that fit into the smaller frame size but exceed a larger one?

Teacher
Teacher

Exactly! It shows how complex memory management is and why understanding access patterns is crucial. To summarize: Belady’s Anomaly underscores that simply adding resources is not always the solution for improved performance.

Introduction & Overview

Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.

Quick Overview

This section discusses various page replacement strategies, including approximate LRU, sampled LRU, clock, and modified clock algorithms, focusing on their implementation and efficiency.

Standard

The section highlights different page replacement strategies that operate on memory management, detailing how approximate LRU, sampled LRU, and clock algorithms utilize reference bits and timestamps to determine which pages to replace. The need for software-based implementations rather than hardware methods is also discussed, along with considerations like page cleanliness and handling of dirty pages.

Detailed

In this section, we explore various page replacement strategies used in operating systems to manage memory resources efficiently. The discussion covers:

  1. Approximate LRU (Least Recently Used): Instead of a complex hardware-based LRU implementation, this strategy uses a reference bit for each page in the page table, which updates on memory references. A page's reference bit is set to 1 when accessed, and periodically reset to 0, allowing the selection of pages with a reference bit of 0 for replacement, as they are likely to be less utilized.
  2. Sampled LRU: An extension of the approximate LRU, this approach adds a reference byte for each page, capturing the reference bit history over time. The operating system reads the reference bits at the end of intervals and uses the normal byte value to determine the replacement strategy, selecting the page with the smallest reference byte, indicating reduced recent access.
  3. Clock Algorithm: This refined method uses a circular linked list to track pages. When a fault occurs, it checks reference bits sequentially, granting second chances to pages with reference bit 1 by resetting them to 0 and continuing the search for a page with bit 0 for replacement.
  4. Modified Clock Algorithm: This version extends the clock algorithm by incorporating a dirty bit (to indicate modifications since the last write). It prefers to replace clean pages (0,0) over dirty (0,1), following a specific order for replacement decisions to minimize the need for writing modified pages back to disk.
  5. Belady’s Anomaly: A surprising phenomenon where increasing the number of page frames can lead to an increase in the number of page faults, contradicting the expectation that more capacity reduces faults. This section provides a detailed exploration of these strategies, including how distributions of access patterns affect performance.

Youtube Videos

One Shot of Computer Organisation and Architecture for Semester exam
One Shot of Computer Organisation and Architecture for Semester exam

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Overview of Replacement Strategies

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

So, if I do not implement this counter method or the stack method in hardware I have to implement that in software, meaning that whenever there is a memory reference, I have to go to the OS and update data structures, which is also impractical because memory references are a very frequent phenomena. So therefore, the exact version of ALU is not often used and instead approximate LRU is commonly used.

Detailed Explanation

When managing pages in memory, it's crucial to have a method for replacing old pages when new ones need to be loaded. There are hardware methods like counter or stack methods, but if they aren't available, software methods must be used. However, these are impractical due to the high frequency of memory references. Hence, a more simplified version called approximate LRU (Least Recently Used) is often utilized. This approximation allows better performance at a lower implementation cost.

Examples & Analogies

Think of it like a library where patrons frequently check out books. If the library doesn't have an automated system to track who borrowed what (the hardware), the staff would need to manually update records each time a book is checked out, which would be inefficient. Instead, they might just remember which books were borrowed most recently (approximate LRU).

Using Reference Bits

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

So, one of the most common implementations is to use an approximate version of ALU, which uses reference bit in the page table for each page. So, what how does this operate? On a reference to a page this bit is set to 1 ok. So, each page has a reference bit when I am referring to this page when I am accessing this page and this page is in memory I am setting this bit from 0 to 1, if it is not already 1.

Detailed Explanation

The approximate LRU strategy leverages a reference bit in the page table for each page. When a page is accessed, its reference bit is set to 1. If the page is accessed multiple times during a specific time frame, the reference bit remains 1. This indicates the page has been used recently. If a page's reference bit is still 0 at the end of this timeframe, it's likely a candidate for replacement since it hasn't been accessed recently.

Examples & Analogies

Imagine you have a box of toys. Each time you play with a toy, you place a sticker on it (setting the reference bit to 1). After a while, if a toy has no stickers, it's likely you haven't played with it recently, so you might decide to put it away to make room for other toys.

Resetting Reference Bits

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Now, at and I have time periods or frames or intervals. So, fixed size intervals after which I check for the after which I set the bits of all pages, I said the reference bits of all pages to 0. So, I when at the start of a time interval, I set the reference bits of all pages to 0 and then within that particular interval every page that is referenced their reference bits are set to 1.

Detailed Explanation

The process operates in fixed time intervals. At the start of each interval, all page reference bits are reset to 0. During that interval, if any page is accessed, its reference bit is set to 1. This method creates a snapshot of page usage that helps in identifying which pages have not been used recently by looking at their reference bit status at the interval's end.

Examples & Analogies

Imagine you are tracking how often you wear different shirts throughout the week. At the start of each week (the time interval), you check which shirts you’ve worn (reset bits to 0) and note the ones you wear during that week (setting bits to 1). By the end of the week, if some shirts have never been worn, you know they can be put away.

Choosing Pages for Replacement

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

And then when the when a particular page has to be replaced, I will try to use a page for which the reference bit is 0. So, a reference bit is 0 means that in the in the current time interval it has not been accessed; that means, it has it is it is of higher probability that it is of higher probability that this page will also not be used in the recent future because, it has not been used in the current timestamp.

Detailed Explanation

When it’s time to replace a page, the strategy chooses a page with a reference bit of 0. This signifies that the page has not been accessed during the current interval, suggesting it is less likely to be needed shortly. This helps optimize memory usage by freeing space from pages that are less likely to be referenced again soon.

Examples & Analogies

Think of a bookshelf: when making space for new books, you look for the ones you haven't read in a while (reference bit 0) because they are likely to be ignored again. You choose to rearrange your shelf by pulling off the books you haven’t picked up recently.

FIFO in Page Replacement

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Now if all bits are same for a for a given reference bit suppose I find out among all pages for which the reference bit is 0, I may like to select the one which has which was which came into memory at the earliest; that means, first in first out, I will use FIFO for a given value of the reference bit.

Detailed Explanation

If multiple pages have a reference bit of 0 when it's time for replacement, the page that will be selected for replacement is the one that has been in memory the longest. This is known as the FIFO (First In First Out) method, ensuring that older, less-used pages are replaced first.

Examples & Analogies

Imagine a line at a bus stop where the first person to arrive is first to leave when a bus arrives (FIFO). So, when the bus comes to pick up passengers, it naturally takes the person who has been waiting the longest first.

Sampled LRU and Reference Bytes

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

The next one is sampled LRU, sampled LRU who is an extension over the approximate LRU which we studied using just one reference bit. And it has slightly higher hardware cost in that, the first byte of each page is used by this strategy.

Detailed Explanation

Sampled LRU extends the approximate LRU strategy by utilizing an extra byte (the reference byte) for each page instead of just a single reference bit. This byte is used to keep track of access patterns over multiple intervals. This method provides a more accurate picture of which pages are frequently or infrequently used, albeit at a higher hardware cost.

Examples & Analogies

Consider a shopping cart where you keep track of how often you pick certain items to determine how to restock them. Instead of just tallying once (one reference bit), you write down the number of times each item was picked in a week (reference byte). This way, you can see which items are consistently in demand.

The Role of the Operating System

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

At set time intervals I have similar time intervals as was for the simple approximate LRU I have similar time intervals. And we take an interrupt and get the OS involved.

Detailed Explanation

In the sampled LRU strategy, at the end of each time interval, an interrupt signals the operating system to take action. The OS reads and records the reference bits into the reference byte for each page before clearing the reference bits. This process helps create historical usage data for each page.

Examples & Analogies

Imagine having a reminder app that collects data about which tasks you complete over some time. At the end of each week, you review your completed tasks (the OS’s involvement), update your progress (recording reference bits into the reference byte), and start fresh with new tasks (resetting reference bits).

Access Patterns and Page Replacement Decisions

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

The numerical value tells me in the last 8 intervals what was the access pattern of it.

Detailed Explanation

The numerical value stored in the reference byte reflects the access pattern of a given page over the last several intervals, indicating how often and recently that page was accessed. A lower numerical value suggests the page has not been used frequently, making it a more suitable candidate for replacement.

Examples & Analogies

Think about a photo album where you keep track of how often family photos are appreciated or viewed. The more likes a photo gets (higher access pattern), the less likely you are to replace it or remove it, while those that are rarely liked (lower numerical value) are prime candidates for removal.

Choosing Pages in the Sampled LRU

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

So, I take the numerical value of this byte. Now what does this decimal value tell? This decimal value tells me in the last 8 intervals what was the access pattern of it.

Detailed Explanation

When selecting a page for replacement in the sampled LRU strategy, the operating system analyzes the numerical value of the reference byte. A low numerical value indicates that the page has been neither frequently nor recently accessed, and thus it is appropriate for removal.

Examples & Analogies

Imagine you're cleaning out your closet and you check how often each piece of clothing was worn recently. If you see a dress hasn’t been worn in a while (low numerical value), you're more likely to get rid of it, creating space for new favorites.

Clock Algorithm for Page Replacement

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

So, we will now look at the clock algorithm or the second chance page replacement algorithm. So, it is an extension, it is it uses the reference bits you uses the reference bits in a different way.

Detailed Explanation

The Clock Algorithm, also known as the Second Chance Algorithm, offers an alternative method for page replacement. It uses reference bits to give pages a 'second chance.' If a page with a reference bit of 1 is encountered, it is skipped over and its bit is reset to 0, giving it another chance to remain in memory.

Examples & Analogies

Imagine a vending machine that gives you a second chance on a popular snack. If someone presses for a snack and the machine recognizes there's still some left, it simply resets the button, allowing that selection to remain available for later rather than getting rid of it immediately.

Working of the Clock Algorithm

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

So, let us say the previous search ended with the pointer; that means, the last page which was accessed was page 6 ok...

Detailed Explanation

In the Clock Algorithm, a pointer keeps track of the last page checked. When a page fault occurs, the algorithm starts searching from this pointer. It checks the reference bits in a circular manner, giving each page a chance based on its reference bit status. Pages with a reference bit of 0 are chosen for replacement first.

Examples & Analogies

Think of a playlist on your music app. When your app looks for a song you missed earlier (the page fault), it starts from your last listened song and skips over those it played recently (reference bit 1), checking for songs you haven't played (reference bit 0) that might be ready to be played again.

Handling Dirty Pages

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Now, one aspect which the second chance algorithm does not take care, or ignores is that was is this page dirty or not is this has this page been modified has been written to or not...

Detailed Explanation

The Second Chance Algorithm does not differentiate between clean and dirty pages. A dirty page needs to be written to disk before being replaced, which adds additional overhead compared to clean pages that can be discarded immediately. This points out a limitation in simply evaluating reference bits without considering the page's state.

Examples & Analogies

Picture a worksheet you’ve worked on. If you've only written notes (clean), you can simply throw it away. However, if you’ve scribbled out certain answers and made edits (dirty), you need to take the time to rewrite a clean version before tossing it. This is what happens with dirty pages in memory.

Modified Clock Replacement Algorithm

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

So, now we will see an extension of the modified clock replacement algorithm, in which we use the 2 bits both the reference bits and the dirty bit...

Detailed Explanation

The Modified Clock Replacement Algorithm incorporates both reference and dirty bits into its evaluation process. This provides better management of pages by categorizing them into four states based on their usage and cleanliness, allowing a more informed replacement decision based on both access history and whether a page needs to be written back.

Examples & Analogies

Think of how you manage your closet: if an item of clothing is outdated (not referenced) and hasn’t been worn (clean), you can easily discard it. If it’s dirty, you still need to wash it before throwing it away, and if it’s stylish but dirty, you might keep it longer due to its potential usefulness.

Practical Example of Page Replacement

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

So, now before proceeding we will take a small example. Consider a computer system with 10 physical page frames, so, I have 10 physical page frames...

Detailed Explanation

This practical example illustrates the differences between different page replacement strategies (LIFO and Optimal) and how they yield varying page fault counts. It highlights the impact of the replacement policy on overall efficiency, emphasizing the importance of selecting the right algorithm based on access patterns.

Examples & Analogies

Imagine organizing a storage unit. If you pull out the last box you placed there (LIFO), you'll lose track of essential items that came in earlier since you're overlooking what you need most. If you plan ahead (Optimal), you'll maintain critical items at the front, ensuring you only go for what you need without unnecessary trips.

Belady's Anomaly

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Now, we go on to understand one aspect of page replacement, which was important for which is important from a theoretical perspective and, also because this was quite a concern for early page replacement designer page replacement policy designers...

Detailed Explanation

Belady's anomaly refers to an unexpected outcome where increasing the number of page frames results in more page faults rather than fewer. This counterintuitive phenomenon contradicts the principle that more available frames should lead to better performance. It highlights challenges in designing effective page replacement algorithms.

Examples & Analogies

Think of a warehouse designed to hold boxes. If you put in fewer boxes (3 frames), it sometimes turns out easier to find what you need than if you have extra space for more shelves (4 frames) where you might stack items inefficiently and end up with more clutter.

Definitions & Key Concepts

Learn essential terms and foundational ideas that form the basis of the topic.

Key Concepts

  • Memory Management: The process of efficiently managing computer memory, including strategies for page replacement.

  • Page Replacement: The act of removing pages from physical memory to make room for new pages as needed.

  • Reference Bit: A bit used to track whether a page has been accessed, playing a critical role in replacement strategies.

Examples & Real-Life Applications

See how the concepts apply in real-world scenarios to understand their practical implications.

Examples

  • Approximate LRU chooses pages with reference bit 0 for replacement due to their presumed lower access frequency.

  • In the clock algorithm, if a page has a reference bit of 1, it is reset to 0 and skipped, simulating a second chance before replacement.

Memory Aids

Use mnemonics, acronyms, or visual cues to help remember key information more easily.

🎵 Rhymes Time

  • If your page bit is 1, don’t fret, reset it - the page isn't done yet!

📖 Fascinating Stories

  • Imagine a librarian organizing books (pages). Books borrowed regularly (accessed) get a green sticker (reference bit). At the end of the day, all stickers are reset, but some say, 'I've been borrowed!'—they get to stay longer.

🧠 Other Memory Gems

  • For remembering the order of page replacement: RCD - Reference, Clean, Dirty (Clock Algorithm)!

🎯 Super Acronyms

To remember page replacement algorithms, think A - Approximate, S - Sampled, C - Clock, M - Modified Clock.

Flash Cards

Review key concepts with flashcards.

Glossary of Terms

Review the Definitions for terms.

  • Term: Approximate LRU

    Definition:

    A memory management strategy that uses reference bits, periodically resetting them to determine which pages can be replaced.

  • Term: Sampled LRU

    Definition:

    An extended version of Approximate LRU that uses a reference byte to capture the history of page accesses over time.

  • Term: Clock Algorithm

    Definition:

    A paging algorithm that organizes pages in a circular list, granting pages a second chance before replacement based on reference bits.

  • Term: Modified Clock Algorithm

    Definition:

    An enhanced version of the Clock Algorithm that incorporates a dirty bit, allowing for more informed page replacement decisions.

  • Term: Belady’s Anomaly

    Definition:

    A phenomenon where increasing the number of page frames in memory leads to an increase in page faults under the FIFO replacement policy.