Logo

dev-resources.site

for different kinds of informations.

Cache : Direct Mapping ,Assosiate Mapping & Set Assosiate Mapping

Published at
10/15/2023
Categories
operatingsystem
softwaredevelopment
softwareengineering
computerscience
Author
syedmuhammadaliraza
Author
19 person written this
syedmuhammadaliraza
open
Cache : Direct Mapping ,Assosiate Mapping & Set Assosiate Mapping

Introduction:

Effective data management is essential to improve performance in operational systems. Cache, which is used to store frequently accessed data for quick retrieval, plays an important role in improving system responsiveness. The three common types of caches used are direct mapping, associative mapping, and set-associative mapping. In this article, we will explore each method, providing examples and real-life coding scenarios to illustrate their applications.

Direct mapping:

Direct mapping is a mapping technique where each block of main memory is mapped to only one cache. The mapping is determined using a hash function or simple arithmetic. This simplicity makes direct mapping easy to implement, but it can lead to cache misses in locations that block large amounts of memory.

Example:

Consider 8 cache blocks and 32 main memory blocks. Mapping can be done like this:

  • Map cache block 0 to main memory block 0
  • Cache block 1 maps to main memory block 8
  • Map cache block 2 to main memory block 16
def direct_mapping_cache(memory_block, cache_size):
    block your memory % cache_size

# Examples of usage:
cache_size = 8
main_memory block = 16
cache_block = direct_mapping_cache (block_memory_block, size_cache)
print (f "map memory block {main_memory_block} to Block Cache {map {block}");
Enter fullscreen mode Exit fullscreen mode

Associative mapping:

In Associative Mapping, any main memory block can be mapped to any cache image. Mapping is done using tags associated with each person. This flexibility eliminates the conflict problem, but adds complexity to finding specific blocks in the cache.

Example:

Adopt a character that combines all 4 blocks. The cache can accommodate blocks of main memory:

CacheEntry class:
    def __init__ ( self , label , data ):
        self.tag = tag
        self.data = data

def Associative_mapping_cache(memory_blocking, cache):
    To log in as a guest:
        if enter.tag == memory_block:
            opening

# Examples of usage:
cache_size = 4
cache = [CacheEntry(5, "Data1"), CacheEntry(12, "Data2"), CacheEntry(8, "Data3")]

memory_block_to_find = 12
cache_entry = Associative_mapping_cache ( cache_block_to_find , cache )
print(f "data in cache for memory block {memory_block_to_find}: pain cache_entry.data}");
Enter fullscreen mode Exit fullscreen mode

Set-Associative Mapping:

Set-associative Mapping combines the direct and associative aspects of mapping. The cache is divided into sets, and each set contains several blocks. Each block in main memory can be mapped to any block in a given set. It strikes a balance between simplicity and flexibility, alleviating some of the problems encountered with direct mapping.

Example:

Consider the 2-way 4-set associative representation:

CacheSet class:
    def __init__ ( self ):
        self.entry = []

def set_associative_mapping_cache(blocking_memori, cachecaches, associative):
    set_index = block_memori%len (cache_sets)
    target_set = cache_sets [set_index]

    To access target_set.entry:
        if enter.tag == memory_block:
            opening

# Examples of usage:
association = 2
cache_sets = [CacheSet() for range(4)]
cache_sets [0]. entry = [CacheEntry(5,"Data1"), CacheEntry(12,"Data2")]

memory_block_to_find = 12
cache_entry = set_associative_mapping_cache (cache_block_to_find, cache_sets, associative)
print(f "data in cache for memory block {memory_block_to_find}: pain cache_entry.data}");
Enter fullscreen mode Exit fullscreen mode

In this example, a block of memory is mapped to a specific set and then the desired block is searched, providing a compromise between the simplicity of direct set mapping and the flexibility of associative maps.

In conclusion, understanding this type of cache is important to design an efficient memory management system in the operating system. Each mapping method comes with trade-offs, and the choice depends on the specific system requirements. Whether it is the simplicity of direct mapping, the flexibility of associative mapping, or the compactness of associative mapping, these cache patterns play an important role in improving the performance of modern computing systems.

operatingsystem Article's
30 articles in total
Favicon
Understanding Process Management in Operating Systems
Favicon
Introduction to Operating Systems
Favicon
A Deep Dive into Operating Systems
Favicon
Understanding the Core Components of an Operating System and How They Work
Favicon
Unveiling the Magic Behind Your Computer: A Deep Dive into Operating Systems
Favicon
Understanding How Your OS Handles Print Requests to the Console 🖥️
Favicon
A revolutionary operating system
Favicon
40+ Linux Commands You Need to Know: The Ultimate Guide for Ubuntu Users
Favicon
Understanding Operating Systems: The Backbone of Modern Computing
Favicon
Demystifying OS Concepts (Part 2): Other Synchronization Primitives
Favicon
Demystifying OS Concepts: Introducing OSViz
Favicon
Mastering Package Management in Fedora Linux: Beginner's Guide
Favicon
Systemd: Managing Services and Processes In Fedora Linux
Favicon
Unlocking the Mysteries of the Clipboard: Your Computer's Unsung Hero
Favicon
Resolving Memory Fragmentation for Linkedlist Heap Allocator
Favicon
CPU Scheduling Algorithms in OS
Favicon
Securing Virtual Memory in Operating Systems
Favicon
Exploring Operating Systems and Distributions: A Comprehensive Overview
Favicon
Exploring Operating Systems and Distributions: A Comprehensive Overview
Favicon
Operating System Security in a Hybrid Cloud Environment
Favicon
Systemd vs sysVinit - Initializing the system
Favicon
Design and Implementation Challenges in Mobile Device Operating Systems: Overcoming Complexities
Favicon
Which is More Secure: Linux or Windows?
Favicon
Understanding Virtual Memory and Paging
Favicon
Exploring Different Mobile Operating Systems
Favicon
Demystifying the Inner Workings of Operating Systems
Favicon
Deadlock in Operating System
Favicon
What Is An Operating System (OS)? Demystifying The Core Concepts
Favicon
Cache : Direct Mapping ,Assosiate Mapping & Set Assosiate Mapping
Favicon
Process Management in Operating Systems

Featured ones: