1. About Cache Memory
•Cache memory is an extremely fast memory that acts as a buffer between RAM and
the CPU.
•Cache memory is a special memory with very high-speed. It is used to speed up
processing capacity of CPU.
•Cache memory is costlier than main memory or disk memory but economical than CPU
registers.
•Cache is a smaller and faster memory holds frequently requested data and instructions
so that they are immediately available to the CPU when needed.
•Cache memory is used to reduce the average time to access data from the main
memory.
2. How Does Cache Memory Work?
• Cache memory increases computer's performance. The cache memory is located very close to the
CPU.
• When the processor needs to read or write a location in main memory, it first checks cache memory
for a corresponding entry.
• If the processor finds that memory location in the cache, cache hit condition occurred and data is
read from cache.
• If the processor does not find the memory location in the cache, cache miss condition occurred. In
cache miss condition, the cache allocates a new entry and copies data from main memory, then the
request is fulfilled from the contents of the cache.
➢ The performance of cache memory is frequently measured in terms of a quantity called Hit ratio.
Hit Ratio = hit / (hit + miss) = No of hits / total accesses
Miss Ratio = miss / (hit + miss) = No of miss / total accesses
3. Cache Mapping
There are three different types of mapping used for the purpose of cache memory which are as follows:-
1. Direct Mapping 2. Associative Mapping 3. Set-Associative Mapping
1. Direct Mapping- In direct mapping, there is only one place in cache memory where a given block
of data from RAM can be stored. This means that the CPU only has to look in one place in the
cache to see the data or instructions that it is looking for are present, and if it is, it will be found
very quickly.
2. Associative Mapping- Associative mapping is known as fully associated mapping. This is the
opposite of direct mapping. In this mapping any block of data or instructions from RAM can be
placed in any cache memory block. This means that the CPU has to search the entire cache
memory to see data what it is looking for. The chances of a cache hit are much higher in this
mapping.
3. Set-Associative Mapping- Set-associative mapping allows each word that is present in
the cache memory can have two or more words in the main memory for the same index address.
• Set associative mapping combines the best of direct and associative cache
mapping techniques.
• Set-associative mapping is a combination of direct and associative mapping.
The cache lines are grouped into sets. The number of lines in a set can vary from 2 to 16. The data can
be stored in any of the lines in the set.
4. Types of Cache
Primary Cache –
A primary cache is always located on the processor chip. This cache is small and its access time is comparable
fast to that of processor registers.
Secondary Cache –
Secondary cache is placed between the primary cache and the rest of the memory. It is referred to as the level 2
(L2) cache. Often, the Level 2 cache is also located on the processor chip.
Important Notes About Cache Memory-
✓ Computer cannot run without the cache. Cache helps CPU to speed up execution.
✓ Cache memory is not any separate chip. It is integrated along with the processor and
placed closer.
✓ Cache memory is important because it improves the efficiency of data retrieval.
✓ It stores program, instructions and data that are used repeatedly in the operation of
programs which CPU is likely to used.
6. What is Virtual Memory?
1. Virtual Memory is a space where large programs can store themselves in form of pages
while their execution, and only the required pages or portions of processes are loaded into
the main memory.
2. “ A computer can address more memory than the amount physically installed on the system.
This extra memory is actually called Virtual Memory and it is a section of a hard disk that's
set up to emulate the computer's RAM.”
3. Virtual memory is a ”Memory Management Capability” of an operating system
which uses hardware and software to allow a computer to compensate for
physical memory shortages by temporarily transferring data from RAM to disk storage.
4. This process is done temporarily and is designed to work as a combination of RAM and
space on the hard disk.
5. This technique is useful as large virtual memory is provided for user programs when a very
small physical memory RAM is there.
6. Virtual memory provide benefits in terms of costs, physical space, multitasking capabilities,
and data security.
7. Virtual Memory is a logical unit of computer memory that increases the capacity of main
memory by storing larger size program than the main memory in the computer system.
7. What Is Demand Paging?
Demand Paging- Demand Paging is a method of Virtual
Memory Management. In this, when a process is swapped in, its
all pages are not swapped in at once. They are swapped in only
when the process needs them.
❑ Initially only those pages are loaded which will be required
the process immediately.
❑ The pages that are not moved into the memory are
marked as invalid in the page table.
❑ In case of pages that are loaded in the memory, they are
marked as valid along with the information about where
to find the swapped-out page.
❑ Pages are only loaded when demanded by the process.
This is called Pure Demand Paging.
❑ If the referred page is not present in the main memory,
then there will be a miss and the concept is called Page
miss or page fault.
❑ Virtual memory is also called lazy swapper because the
swapping of pages is done only when the CPU requires it.
❑ Virtual memory is commonly implemented in demand
paging.
Important jobs of virtual memory in Operating Systems-
Virtual Memory
8. Page Replacement
In Demand Paging only certain pages of a process are loaded initially into the memory which allows us to get
more number of processes into the memory at the same time.
But problem occurs when a process requests for more pages and no free memory is available to bring
them in. So, for solving this problem computer uses page replacement techniques/algorithms:-
1. Put the process in the wait queue, until any other process finishes its execution thereby freeing frames.
2. Remove some other process completely from the memory to free frames.
3. Find some pages that are not being used right now, move them to the disk to get free frames. This technique
is called Page Replacement and is most commonly used. We have some algorithms to carry on page
replacement efficiently. These algorithms are – Optimal Page Replacement Algorithms, Basic Page
Replacement Algorithm, FIFO Page Replacement Algorithm, LRU (Least Recently Used) Page Replacement
Algorithm.
Benefits of Virtual Memory:-
1. Large programs can be written, as virtual space is available compared to physical memory.
2. Less I/O required, leads to faster and easy swapping of processes.
3. More physical memory available, as programs are stored on virtual memory so they occupy very less space on
actual physical memory.
4. Simplified memory management, more efficient use of primary memory, increased capacity, program
isolation, and flexible memory allocation.
Disadvantage:- virtual memory operates at much slower speeds than computer RAM. If the OS juggles data
between virtual memory and RAM continuously, it can slow down the computer considerably.
9. Example-1 Consider page reference string 1, 3, 0, 3, 5, 6 with 3-page frames. Find number of page faults.
10. Optimal Page replacement –
In this algorithm, pages are replaced which would not be used for the longest duration of time in the future.
Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4-page frame. Find number of page
fault.
11. Least Recently Used –
In this algorithm page will be replaced which is least recently used.
Example-3 Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4-page frames.
Find number of page faults.
12. Thrashing:-
• In OS, it’s is a common phenomenon in computer systems. It occurs when the system spends more time
swapping pages between main memory and secondary storage (like a hard disk) than actually performing
useful tasks.
• This happens when the system's memory is overloaded with processes that need more memory than is
available.
• It is caused by a high number of page faults, which happens when the system needs to retrieve a page from
the disk because it is not present in memory.
Key points about thrashing:-
•Cause:
Thrashing is primarily caused by insufficient physical memory, leading to
frequent page faults.
•Impact:
Thrashing significantly reduces system performance, making processes run
very slowly and potentially causing a system to appear unresponsive.
•Page faults:
Page faults occur when a process attempts to access a page that is not
currently in main memory.
•Paging:
Paging is a memory management technique where memory is divided into fixed-size blocks called pages.
•Swap space:
Swap space (or paging) is used to store pages that are not currently in main memory.
13. Causes of thrashing:-
1. Insufficient physical memory:
If the system doesn't have enough physical memory to hold the active pages of all running processes, frequent swapping will
occur.
2. High multiprogramming level:
If too many processes are running simultaneously and demanding memory, thrashing can occur, especially if memory
allocation is inefficient.
3. Inefficient memory allocation strategies:
Algorithms that allocate memory poorly can lead to thrashing by not providing enough frames to processes.
14. Locality of reference:- Locality of reference, also known as the principle of locality, describes the tendency of a program to
access the same set of memory locations repeatedly over a short period. This principle is fundamental in computer science and
is leveraged in memory management and cache optimization. It has two main types: temporal and spatial locality.
Temporal Locality:-
•A program exhibits temporal locality if it accesses the same memory location multiple times within a short time frame.
•This suggests that the program is likely to reuse data or instructions it has recently used.
•Cache memory strategies rely on this principle, assuming that if a data is accessed, it's likely to be accessed again soon, so it's
stored in the faster cache.
Spatial Locality:-
•A program exhibits spatial locality if it accesses memory locations that are physically close to each other.
•This suggests that if a program accesses a specific location, it's likely to access locations nearby soon after.
•This is also leveraged by cache memory, which anticipates that if one location is accessed, nearby locations might be accessed
as well, so it fetches them into the cache.
Importance and Applications:-
•Cache Memory Optimization:- Understanding locality of reference is crucial for designing efficient cache systems that store
frequently accessed data and instructions for faster retrieval.
•Memory Management:- It helps in optimizing memory allocation and usage by anticipating which data and instructions are
likely to be needed in the near future.
•Parallel Processing:- In parallel processing systems, exploiting locality helps reduce memory access latency and improve
overall performance.
•Algorithm Design:- Locality can guide the design of algorithms and data structures to ensure that frequently accessed data is
organized spatially and accessed temporally in a way that maximizes cache utilization.