Coa Unit4
Coa Unit4
Cache memory is a small, fast memory that sits between the CPU and
main memory (RAM), acting as a buffer to improve data access speed. It
stores frequently accessed data and instructions, allowing the CPU to
retrieve them quickly, reducing the time spent waiting for main
memory. Cache memory is organized into levels (L1, L2, L3) and uses
different mapping techniques to manage data storage and retrieval.
Organization:
• Hierarchical Structure:
Cache memory is often organized in a hierarchical manner, with multiple levels
(L1, L2, L3, etc.).
o L1 Cache: The smallest and fastest cache, often located directly on the
CPU chip.
o L2 Cache: Larger and slower than L1, typically shared by multiple CPU
cores.
o L3 Cache: The largest and slowest, may be shared by all CPU cores or
even multiple processors.
• Cache Lines:
Cache memory is organized into blocks called cache lines, which are groups of
bytes that are transferred between the cache and main memory.
• Cache Mapping:
Determines how data is mapped from main memory into the cache lines.
o Direct Mapping: Each memory block can only be stored in one specific
cache line.
o Associative Mapping: Any memory block can be stored in any cache
line.
o Set-Associative Mapping: Combines features of direct and associative
mapping, dividing the cache into sets, and allowing blocks to map to any
line within a set.
Working:
1. 1. CPU Request:
When the CPU needs data or instructions, it first checks the cache memory.
2. 2. Cache Hit:
If the requested data is found in the cache (a "cache hit"), it's retrieved quickly.
3. 3. Cache Miss:
If the data is not in the cache (a "cache miss"), the CPU must fetch it from main
memory, which is slower.
4. 4. Data Transfer:
When a cache miss occurs, the required data is fetched from main memory and
copied into the cache line, along with surrounding data, to improve the chances
of future hits.
5. 5. Replace Data:
If the cache is full, a replacement algorithm is used to determine which data to
replace.
o Write-Through: Changes in the cache are immediately written to main
memory.
o Write-Back: Changes are made in the cache and written to main memory
later, possibly when the cache line is replaced.
6. 6. Locality of Reference:
The cache's effectiveness relies on the principle of locality of reference, where
programs tend to access the same data or nearby data repeatedly. This allows
the cache to store the most frequently used data and instructions, maximizing its
performance.
Direct Mapping
In direct mapping physical address is divided into three parts i.e., Tag bits,
Cache Line Number and Byte offset. The bits in the cache line number
represents the cache line in which the content is present whereas the bits
in tag are the identification bits that represents which block of main
memory is present in cache. The bits in the byte offset decides in which
byte of the identified block the required content is present.
Tag Number of Cache Lines Byte Offset
Demand Paging:
Demand paging is a memory management technique where a process's
code and data are not loaded into main memory (RAM) when the process
begins execution. Instead, pages are loaded on demand, meaning they are
fetched from secondary storage (e.g., hard disk) only when they are
actually needed by the process. This technique significantly reduces the
memory requirements of a process, as only a portion of the process's code
and data need to be in RAM at any given time.
• Process Isolation: Each process has its own virtual address space, protecting
processes from each other.
Drawbacks of Virtual Memory:
• Performance Overhead: Accessing data on disk is much slower than accessing it in
RAM, so virtual memory can lead to performance degradation, especially when
swapping is frequent.
• Swapping Issues: If too many pages are swapped, the system can become slow
and unresponsive.