0% found this document useful (0 votes)
16 views91 pages

OS_CH-3-4

Memory management is a critical function of an operating system that ensures efficient allocation and deallocation of memory to processes while maximizing performance. It involves tracking memory usage, managing swapping between main memory and disk, and protecting memory spaces of different processes. Key components include physical memory, memory management hardware like the MMU, and techniques such as paging, segmentation, and overlays.

Uploaded by

brotadese50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views91 pages

OS_CH-3-4

Memory management is a critical function of an operating system that ensures efficient allocation and deallocation of memory to processes while maximizing performance. It involves tracking memory usage, managing swapping between main memory and disk, and protecting memory spaces of different processes. Key components include physical memory, memory management hardware like the MMU, and techniques such as paging, segmentation, and overlays.

Uploaded by

brotadese50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

1

CHAPTER THREE

MEMORY MANAGEMENT
Outlines

Working sets
Physical memory and memory Paging and
and Caching
management hardware segmentation
thrashing

Hardware Overlays
Page
Replacement
Swapping and
replacement
policies
Partitions
Overview of memory management
• Memory management is a core function of an operating system (OS), ensuring that each
process has enough memory to execute efficiently while maximizing system performance
and resource utilization.
• Memory is one of the most vital resources in a computer system
• Memory is a large array of words or bytes, each with its own address.
• The CPU fetches instructions and data of a program from memory; thus,
• Both the program and its data must reside in the main (RAM and ROM) memory.

• The purpose of memory management is to ensure fair, secure, orderly, and efficient use of
memory by providing:
• Sharing of memory- Transparency, Safety, Efficiency and
• Relocation-Ability of a program to run in different memory locations.
• Memory protection- address translation
Cont..
 Computers have :
• a few megabytes of very fast, expensive, volatile cache memory

• a few gigabytes of medium-speed, medium-priced, volatile main memory

• a few terabytes of slow, cheap, nonvolatile magnetic orsolid-state disk Storage


 Memory manager :
• keep track of which parts of memory are in use,
• allocate memory to processes when they need it, and deallocate it when they are done
• manage swapping between main memory and disks when main memory is too small to
hold the process
Memory hierarchy

 Memories are made up of registers. Each register in the memory is one storage
location/address
Cont,
 The OS has a part called Memory manager that manipulates and perform an operation
on memory hierarchy.
Registers < 1 nsec

Cache Memory 2 nsec

Main Memory 10 nsec

Magnetic Disk 10 msec

Magnetic Tape 100 sec

◦ Hence the Memory Manager has a responsibility to


➢ Keeps track of which memory are in use or not.
➢ Allocate memory to processes and De allocate
➢ Manage Swapping between main memory and disk etc.
• Memory is an important resource that needs to be managed properly.
Why?-
• They are limited in nature!
• Since they are limited in nature, the operating system obliged to manage the memory.

What is programmers/expert users need?


Private, Infinitely large, infinitely fast memory …
Nonvolatile …

• Just as processes share the CPU, they also share physical memory

• Hence the operating system owes to manage the memory.


How?
Memory….
• Memory management handles the allocation and deallocation of memory space to
processes while ensuring optimal system performance.
• The task of memory management includes keeping track of used and free memory
space, as well as when, where, and how much memory to allocate and deallocate.
• It is also responsible for swapping processes in and out of main memory.

Memory Management Responsibilities


• The operating system is responsible for:
• Tracking which parts of memory are used and by whom.
• Deciding which processes to load into memory.
• Allocating and deallocating memory space as needed.
• Protecting each process's memory space from others.
Physical Memory and Memory Management Hardware

Physical Memory
• Refers to the actual RAM (Random Access Memory) installed in the system/computer.
• It is the primary working memory that stores:
• Currently running processes.
• The operating system.
• Data that is being accessed and manipulated.
• Processes must reside in physical memory to execute.
• The OS must allocate memory space to processes, manage free memory, and handle
memory protection.
• Memory is divided into small fixed-size blocks called frames.

Example: Assume a computer has 8 GB RAM.


• The OS may reserve the first 1 GB for itself.
• The remaining 7 GB is allocated to user applications.
Cont..

• It temporarily holds data, instructions, and intermediate results of active programs.


• The CPU can directly access data from physical memory using physical addresses.

Characteristics of Physical Memory:


• Volatile: Data is lost when the system is turned off.
• Fast Access: Faster than disk storage (secondary memory).
• Limited: Has a fixed size (e.g., 4GB, 8GB, 16GB, etc.).
• Shared: Multiple processes share physical memory space.
Memory Management Hardware
•Involves hardware features that support memory organization and protection, including:
• Base and Limit Registers: Protect memory by defining the start and end of valid
address ranges.
• MMU (Memory Management Unit): Translates logical addresses to physical
addresses.
A. Memory Management Unit (MMU)
• A hardware component that maps or translating logical (virtual) addresses into physical
addresses.
• Supports techniques like paging and segmentation.
• Can enforce memory protection using base and limit registers or paging mechanisms.
Cont... A. Memory Management Unit (MMU)
•A hardware component that maps logical (virtual) addresses to physical addresses.
•It allows:
• Memory protection.
• Virtual memory.
• Paging and segmentation.

• Address Translation:
•Logical Address: Generated by the CPU.
•Physical Address: Actual address in memory (after translation by MMU).

•Example: Logical Address = 204, Base = 1000,


• Physical Address = 1000 + 204 = 1204
Cont...
B. Base and Limit Registers
• Base Register: Contains the starting physical address where a process’s memory begins.
• Limit Register: Defines the size (or length) of the address space for the process.
• When a process tries to access memory at logical address X, the hardware checks if:
• X >= 0 and X < limit
• Then, the physical address is computed as: Physical Address = Base + X

• Example:
• Suppose: Base = 1000, and Limit = 500
• Then the process can access physical memory from address 1000 to 1499 only.
• If it tries to access address 1600, the OS throws an exception → memory protection fault.
Protection
 Protection : protecting multiple process from unwanted interference against each other
 Address Protection With Base & Limit Registers
Cont..
 How protection implemented?
• Each logical address must fall within the range specified by the limit register.
• The MMU maps the logical address dynamically by adding the value in the relocation
register
 Example: If, in a dynamic partition memory management system, the current value of
the base register(base) is 52994 and the current value of the bounds register(Limit) is
5131, compute the physical addresses that correspond to the following logical addresses
 A) 5130
 B) 650
 C) 5132
• In dynamic partition memory management, the system uses base and bounds registers
to perform address translation and protection.
• Base Register: Holds the starting physical address of the process’s allocated memory.
• Bounds Register: Holds the maximum size (limit) of the process's memory (i.e., valid logical
address range: 0 to bounds - 1).

• To translate a logical address (LA) to a physical address (PA):


• If LA<Bounds⇒PA=Base+LA
• Otherwise, it’s an error (out of bounds memory access).

Given:
•Base Register = 52994
•Bounds Register = 5131
•Therefore, valid logical addresses: 0 to 5130 (inclusive)
Lets compute:

A. Logical Address = 5130


• Since 5130 ≤ 5130, it's within bounds
• Physical Address = Base + Logical Address = 52994 + 5130 =58124

B. Logical Address = 650


• Since 650 ≤ 5130, it's within bounds
• Physical Address = 52994 + 650 = 53644

C. Logical Address = 5132


• Since 5132 > 5130, it's out of bounds ❌
• Result: Address violation (Segmentation Fault) or Trap/Error
Final Answers:

Logical Address Within Bounds? Physical Address / Result

A) 5130 Yes 58124


B) 650 Yes 53644
C) 5132 No Address Error (Trap)
Hardware Overlays
• Hardware overlays (or simply overlays) are a memory management technique that allows large programs to run on
systems with limited physical memory by loading only the required parts of a program into memory at any given
time.
• Overlays are programming constructs supported by the operating system and require help from compilers, linkers,
and runtime loaders.
Why Overlays Were Needed

• Q. What to do when program size is larger than the amount of memory/partition (that exists or
can be) allocated to it?
• Answer:- There are two basic solutions in real memory management: Overlays and Dynamic
Link Libraries (DLLs)
• Before virtual memory systems (like paging or segmentation) became widely used, physical
memory was very limited (often in the kilobyte range). Developers needed a way to:
• Run programs larger than RAM.
• Avoid loading the entire program into memory at once.
• Overlays: Keep in memory only the overlay (those instructions and data that are) needed at any given
phase/time.
• Overlays can be used only for programs that fit this model, i.e., multi-pass programs like compilers.
• Overlays are designed/implemented by programmer. Needs an overlay driver.
• No special support needed from operating system, but program design of overlays structure is complex.
How Hardware Overlays Work
• The idea is to: Divide a program into several independent parts called overlay modules.
• Load only one module (or a group) into a specific region of memory (overlay region).
• Replace it with another module when needed.
• Overlays are defined and managed using an overlay structure or tree which determines:
-Which modules can coexist.
-Which modules will replace each other.

• The root is always in memory.


• Overlay1, Overlay2, and Overlay3 share the same memory area Root (Permanent Code)
(overlay region) and are mutually exclusive. |
----------------------------------
• Only one overlay is loaded into memory at a time. | ||
Overlay1Overlay2Overlay3
Hardware Overlays- Example 1: Real-World Analogy

• Imagine a small desk (RAM) and many books (program parts):


• You can only open one book on the desk at a time.
• When you finish with one book (module), you put it back and open another.
• You always keep the table of contents (main routine) on the desk.

• Early systems like MS-DOS and UNIX V6 supported overlays via linkers (e.g., ld) and
overlay management utilities.
Example2 Q: The overlay tree for a program is as shown below:

• What will be the size of the partition (in physical


memory) required to load (and run) this program?

• Answer:
• Using the overlay concept we need not actually have the entire program inside the main
memory.
• Only we need to have the part which are required at that instance of time, either we need
Root-A-D or Root-A-E or Root-B-F or Root-C-G part.

So we have 14KB size of partition


Logical versus Physical Address Space

• An address generated by the CPU is a logical address whereas address actually available
on memory unit is a physical address OR Physical address – address seen by the
memory unit.
• Logical address is also known a Virtual address which are independent of location in
physical memory data lives.
• The set of all physical addresses corresponding to these logical addresses is referred to
as a physical address space.
• The set of all logical addresses generated by a program is referred to as a logical address
space.
• Logical/virtual addresses are translated by hardware into physical addresses called
MMU.
Logical vsphysical address space….
 The concept of a logical address space that is bound to a separate physical address space is
central to proper memory management.

 Logical(virtual) address: address generated by CPU while program is running


 Logical address space is set of all logical address
 Set of logical addresses generated by a program is called logical address space.

 Physical address is actual location in memory unit (address seen by memory unit.)
 User never access directly the physical address
 Physical address space is set of all physical address corresponding to the logical address
in Logical address space.
Swapping
 To increase CPU utilization in multiprogramming, a memory management scheme known as
swapping can be used.
 Is a mechanism in which a process can be swapped temporarily out of main memory (or move) to
secondary storage (disk) and make that memory available to other processes,
 And then later time, the system swaps back the process from the secondary storage to main
memory (back into memory for continued execution.)

 A process is swapped out will be swapped back into the same memory space that it occupies
previously.
• If binding is done at assembly or load time, then the process cannot be moved to different
location.
• If execution-time binding is being used, then it is possible to swap a
process into a different memory space

 Example: If user process is of size 2048KB and on a standard hard disk where swapping will take
place has a data transfer rate around 1 MB per second.
 Then it will take 2048KB / 1024KB per second = 200 milliseconds (2sec)
Swapping
 To increase CPU utilization in multiprogramming, a memory management scheme known as
swapping can be used.

 Swapping is a mechanism in which a process can be swapped temporarily out of main memory (or
move) to secondary storage (disk) and make that memory available to other processes,

 And then later time, the system swaps back the process from the secondary storage to main
memory (back into memory for continued execution.)
 The purpose of swapping in an operating system is to access data on a hard disc and move it to
RAM so that application programs can use it.
 A process is swapped out will be swapped back into the same memory space that it occupies
previously.
Cont..
11

• Swapping has been subdivided


into two concepts: swap-in and
swap-out.
•Swap-out is a technique for moving
a process from RAM to the hard
disc.
•Swap-in is a method of transferring
a program from a hard disc to main
memory, or RAM.
Swapping
• Memory allocation changes as processes
come into memory and leave it.
• The shaded regions are unused memory.
Fig explanation

The operation of a swapping system is illustrated in Fig. Initially only process A is in


memory.
• Then processes B and C are created or swapped in from disk. In Fig. (d) A is swapped out
to disk. Then D comes in and B goes out.
• Finally A comes in again. Since A is now at a different location, addresses contained in it
must be relocated, either by software when it is swapped in or (more likely) by hardware
during program execution.
Swapping…
Allocating space for a growing stack and a
 Allocating space for a growing data segment. growing data segment.
Real-Life Analogy
• Imagine your desk (RAM) has space for 2 books. You want to read a 3rd book, so you put
one book back into your bag (disk), place the 3rd book on the desk, and when needed,
swap them again.

• Example Scenario • With swapping:


• Let’s say: • Load P1 (300 MB)
• RAM size = 512 MB • Load part of P2 (250 MB - 300 MB = -50 MB →
• Process P1 = 300 MB not enough space)
• Swap out P1 to disk
• Process P2 = 250 MB
• Load P2
• → Total = 550 MB > 512 MB
• When P1 is needed again, swap it back in,
• Without swapping: possibly replacing P2
• Cannot load both P1 and P2..
Partitioning
• Memory partitioning is a memory management scheme in which the main memory is
divided into fixed or variable-sized blocks, called partitions, to allocate space for
processes.
• It helps the Operating System (OS) manage multiple processes in RAM by deciding how to
allocate and where to load processes in memory.

• Types of Memory Partitioning


• Fixed Partitioning (Static Partitioning)
• Memory is divided into a fixed number of partitions, usually at system startup.
• Each partition can hold only one process at a time.
Fixed partitioning
 Fixed partitioning: number of partitions (non-overlapping) in RAM are fixed but size of each
partition may or may not be same(equal) size. Is called static partitioning

• Example: Assume 512 KB RAM divided into 4


fixed partitions:
• Partition 1: 128 KB
• Partition 2: 128 KB
• Partition 3: 128 KB
• Partition 4: 128 KB

• Processes:
• P1: 100 KB → fits in Partition 1
• P2: 90 KB → Partition 2
• P3: 130 KB → ❌ Too big (must wait or be denied)
Dynamic/Variable Partitioning

• Dynamic partitioning is a variable size partitioning scheme


which performs the allocation dynamically.

• When a process arrives, a partition of size equal to the size


of process is created. Then, that partition is allocated to the
process.
• This method takes into account the size of the file or a
process and also estimates the maximum size, up to what the
file or process can grow?

• Relocation-register scheme used to protect user processes


from each other, and from changing operating-system code
and data.
Dynamic Partitioning (Variable Partitioning)
• Memory is not pre-divided.
• Partitions are created dynamically at runtime to fit a process’s exact size.
• Example:
• Assume 512 KB RAM.
• Processes:
• P1: 100 KB → Allocated 100 KB
• P2: 120 KB → Allocated 120 KB
• P3: 200 KB → Allocated 200 KB
• Remaining: 92 KB → If next process requires 100 KB → ❌ Cannot allocate
Memory Allocation Strategies in Dynamic Partitioning
• When dynamic partitioning is used, the OS must decide where to place a new process.
Some common strategies:
1. First-Fit
• Allocates the first available block large enough.
• Fast but may cause fragmentation at the beginning of memory.
• In the first fit, the partition is allocated which is the first
sufficient block from the top of Main Memory.
• It scans memory from the beginning and chooses the first
available block that is large enough.
• Thus it allocates the first hole that is large enough.
Memory Allocation Strategies in Dynamic Partitioning
2. Best-Fit
• Allocates the smallest block that is sufficient.
• Reduces waste but is slow and may create many small holes.
• Allocate the process to the partition which is the smallest
but, sufficient among the free available partition.
• It searches the entire list of holes to find the smallest hole
whose size is greater than or equal to the size of the process.
• Tries to find a hole which is close to actual process size
needed.
Memory Allocation Strategies in Dynamic Partitioning
3. Worst-Fit
• Allocates from the largest available block.
• May leave useful mid-sized blocks free.

• Allocate the process to the partition which is the largest


sufficient among the freely available partitions available in
the main memory.
• It is opposite to the best-fit algorithm.
• It searches the entire list of holes to find the largest hole and
allocate it to process.
Example:
• Given Free blocks:
Block Size Process: Needs
• Block1: 100 KB Process1: 150KB
• Block2 200 KB
• Block3 300 KB
Solutions
• First-Fit: Process 1 Allocates to Block2- 200 KB (first match)
• Best-Fit: Process 1 Allocates to Block2- 200 KB (closest fit)
• Worst-Fit: Process 1 Allocates to Block3- 300 KB (largest)
Example2
Cont.….
Cont.….
Exercise

 The following jobs are loaded into memory,


 Show how they are loaded using best-fit, first-fit and worst- fit memory allocation
method?
 List of Jobs Size 100k, 10k, 35k, 15k, 23k, 6k, 25k, 55k, 88k
,100k
 Memory Block (hole) Size 50k, 200k ,70k, 115k ,15k.
Fragmentation
• As processes are loaded and removed from memory, the free memory space is broken
into little pieces.
• It happens after sometimes that processes cannot be allocated to memory blocks
considering their small size and memory blocks remains unused.
• This unused block is called Fragmentation.
• Fragmentation cause waste of memory and a compaction technique can be used to create
more free memory out of fragmented memory:
Two types of fragmentation
A. Internal Fragmentation
• Memory block assigned to process is bigger.
• Some portion of memory is left unused as it can not be used by another process.

 Thus it occurs when allocated memory may be slightly larger than requested memory Or
when small program occupies an entire partition.

 Occurs in fixed partitioning. Memory inside a partition but not used by the process is
wasted.

 Solution: Allocate very small free partition as a part of the larger request.

 Example: Partition = 128 KB, Process = 100 KB → 28 KB wasted.


Types of fragmentation…
B. External Fragmentation:
• External Fragmentation Occurs in dynamic partitioning.
• Occurs when there are many small pieces of free memory.
 Entire partitions or free space (holes) not used because no process fits in the size of any of the
holes
 Total free memory is enough but scattered in small blocks.
 Solution : Compaction.
 Example: Free blocks = 3 KB + 5 KB + 6 KB (Total = 14 KB), Process needs 10 KB → ❌ Can't
be allocated.
• Dynamic partition is possible when OS combine contiguous internal fragmentation and make
enough for other process accommodation that leads to reduce internal fragmentation.
• Which is called compaction technique.
Fragmentation
 Suppose Process of size 7MB comes. But the process cannot be accommodated inspite of
available free space because of contiguous allocation (as spanning is not allowed).

 Hence, 7MB becomes part of External Fragmentation.


Compaction
• A technique used in dynamic partitioning to combine all free spaces into one large
block by moving processes together.

• Example: Before: [ P1 | Free 5 KB | P2 | Free 10 KB | P3 ]

• After compaction: [ P1 | P2 | P3 | Free 15 KB ]


Summary for Fixed Partitioning vs Dynamic Partitioning

Type Fixed Partitioning Dynamic Partitioning

Flexibility • Low • High

Fragmentation • Internal • External

Performance • Fast allocation • Slower (may need compaction)

Use Case • Simple/legacy systems • General OSs before paging


Paging and segmentation
• Paging and Segmentation are two memory management techniques that help efficiently use for
physical memory, Manage processes, and Provide isolation and protection.

Paging
 Is a memory management scheme by which a computer stores and retrieves data from secondary
storage for use in main memory.

 It divides the logical memory into fixed-size blocks called pages and the physical memory into
blocks of the same size called frames. Paging avoids external fragmentation

 In this scheme, the operating system retrieves data from secondary storage in same-size blocks
called pages.
Pages vs Frames
• Page and page frame are same size.

• Page: A fixed-size block of logical memory.


• Frame: A fixed-size block of physical memory.
• Page Table: A data structure used to map virtual pages to physical frames.
• Page/Frame Number: Index in the page table.
Process Page Table
❖ A page table is the data structure used by a virtual memory system in a computer OS to store the mapping
between virtual addresses and physical addresses.
❖ When an active process requests data, the MMU retrieves corresponding pages into frames located in physical
memory for faster processing. Therefore, process page table Contains the frame location for each page in the
process.
Process Page Table
❖The page table is where the operating system stores its mappings of virtual addresses to physical
addresses.
Page replacement & replacement policies
• When using paging in memory management, the OS must decide two important things:
1. Where to place a page in physical memory – Page Placement Policy
2. Which page to remove when physical memory is full – Page Replacement Policy
• These policies ensure efficient use of memory, maintain system performance, and minimize
page faults.

Page Placement Policy


• Determines where in physical memory a new page should be placed when it is brought in from disk.
• In a pure paging system, any free frame can be used.
• So placement is simple: Place the page in any available frame.
• Placement is mostly influenced by:
- Frame availability
- Hardware constraints (cache locality, NUMA systems)
- Optimization strategies (e.g., keeping related pages close)
Example: Suppose physical memory has 5 free frames: Frame 2, 5, 8, 11, 14
• If a page fault occurs and the OS needs to load Page 4, it can place it in any of the available frames (say, Frame 5).
Page Replacement policy
• A page replacement policy is used to decide which page to remove from memory when there is
no free frame and a new page must be loaded due to a page fault.
Common Page Replacement Algorithms
1. FIFO (First-In, First-Out) Replacement Algorithm :
• Removes the oldest page in memory (the one that came in first).
• Replace the page thathas been in memory for the longest period of time before
2. LRU (Least Recently Used) Replacement Algorithm :
• Removes the page that has not been used for the longest time.
• Replace the page that has not been used for the longest period of time
3. Optimal Page Replacement Algorithm:
• Replaces the page that will not be used for the longest time in the future.
• Replace the page that will not be used for the longest period of time in the future
Basic Page Replacement
1. Find the location of the desired page on disk.
2. Find a free frame:
- If there is a free frame, use it.
- If there is no free frame, use a page replacement algorithm to select a victim frame.
3. Read the desired page into the (newly) free frame. Update the page and frame tables.
4. Restart the process.
 Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are
written to disk.
 Page replacement completes separation between logical memory and physical memory –
large virtual memory can be provided on a smaller physical memory.
Page Replacement Algorithms
• Want lowest page-fault rate.
• Evaluate algorithm by running it on a particular string of memory references (reference
string) and computing the number of page faults on that string.
• If a process tries to access a page that was not brought into memory, access to such page is
called page-fault
• But a process try to access a page that was in memory is called page hit

• WHEN WE CAN SAY Page Fault?

• A page fault happens when a program tries to access a page (a block of memory) that is not currently loaded into the
physical memory (RAM).
• In other words:
• If the page is already in one of the frames, no page fault.
• If the page is NOT in any frame, then a page fault occurs, and the operating system must:
• Analogy: Imagine a line of people waiting to use a single computer at a library.
• When a new person comes and the computer is full, the person who has been waiting the longest gets off, regardless of
whether they are about to finish or just started.
• How it works: FIFO is like a queue. The first page that enters the memory is the first one to be removed when a new
page needs to come in. It doesn't care how often a page is used; it only tracks when it arrived.

• Idea: When a page needs to be replaced, remove the page that was loaded earliest.
In all our examples, the reference string is 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0,1
For a memory with three frames (Buffer size = 3).

Total Page Faut: 15

What happens when we have 4 frames?


• Replace page that will not be used for longest period of time in future.
• Analogy: Imagine a librarian who knows exactly which books you will need to read in the future and in
what order.
• When the bookshelf is full and you need a new book, the librarian removes the book that you will not need
for the longest time.
• How it works: The Optimal algorithm looks into the future.
• It replaces the page that will not be used for the longest period.
• This is ideal but impossible to implement in a real operating system because we can't predict the future perfectly.
• Idea: Replace the page that is not needed for the longest time in future.

Total Page Faut: 9


• Analogy: Imagine you have three books on your desk. Every time you use a book, you put it on top of the pile.
• When you need a new book and your desk is full, you take away the book that's at the bottom of the pile – the one
you haven't used in the longest time.
• How it works: LRU keeps track of when each page in memory was last accessed.
• When a page needs to be replaced, the algorithm chooses the page that has not been used for the longest time.

Total Page Faut: 12


Exercise
 Reference string: 1, 2, 3, 4, 2, 1, 0, 6, 7, 9, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6.
 How many page faults would occur for the following replacement algorithms, assuming four
frames?
 Remember all frames are initially empty, so your first unique pages will all cost one fault each.
 Assuming 3, 5, & 6 frames.
 LRU:
 FIFO:
 Optimal:
Working Sets and Thrashing
What is a Working Set?
• A working set is the set of pages (or memory segments) a process is actively using during a
specific period of time.
• Defined by a working set window (Δ), which is a fixed number of most recent memory
references.
• Helps the operating system predict memory needs for a process.
• Concept introduced by Peter Denning (1968) to manage virtual memory efficiently.

• Working Set (WS) Model


• Let: Δ = working set window (e.g., 10,000 memory accesses)
• WS(t, Δ) = set of pages referenced by the process in time interval (t − Δ, t)
• So, WS(t, Δ) is the active memory footprint of the process.
Example:
• Imagine a process accesses these pages over time:
Time: 1 2 3 4 5 6 7 8 9 10
Pages: A B C A B C D E F G

• With Δ = 6 (i.e., last 6 references):


• At time t = 10: WS(10, 6) = {C, D, E, F, G, A}
• This working set tells the OS: "These are the pages this process needs now."

Why Is Working Set Useful?


• Improves paging performance by knowing which pages to keep in memory.
• Reduces page faults.
• Helps prevent thrashing.
Thrashing
• Thrashing is a condition where the system spends more time swapping pages in and out of memory
than executing processes.
• It happens when the total demand for memory exceeds the available physical memory. Or it
occurs when memory is overcommitted.
• Excessive paging leading to low CPU utilization and performance degradation.

Causes of Thrashing:
• Too many processes.
• Insufficient memory allocation (Not enough RAM.)
• Poor page replacement policy.
• Ignoring the working set model.

Solutions
• Reduce degree of multiprogramming.
• Use working set-based memory allocation.
• Page fault frequency (PFF) control.
Example of Thrashing:
• Let’s say: 4 processes are running. Each needs 5 pages in its working set.
• Total memory = 16 pages
• But 4 × 5 = 20 pages > 16 → Not enough!
• OS tries to keep all 4 process running:
• Page faults increase.
• CPU idle → Performance drops drastically.

• Solution: Working Set-Based Memory Management


• Track WS(t, Δ) of each process.
• Load only processes whose total working set size fits in memory.
• Suspend or swap out processes that exceed available memory.
Caching
• Caching is a technique where frequently used data is stored in a smaller, faster memory so
that it can be accessed more quickly.
• Temporary storage of frequently accessed data in a smaller, faster memory (cache) for
quicker access.
• It’s used to bridge the speed gap between fast CPU and slow main memory or storage.
• Caches are used in:
• CPU → memory
• Disk → RAM
• Web → browser cache
Cache Memory in Operating Systems
• Located between CPU and RAM
• Stores recently used or frequently accessed data
• Very fast (uses SRAM)
End ….
1

CHAPTER FOUR

DEVICE MANAGEMENT
Outlines

Abstracting
Serial and device Direct Recovery
Buffering
parallel differences memory from
strategies
devices access failures

Device Management
Device management
After the completion of this chapter, students will able to
Understand I/O devices and I/O operations
Understand the way of communication in computer system
Differentiate Serial and Parallel Information Transfer
Understand the major category of I/O devices
Distinguish I/O hardware devices parts
Know Polling concepts
Know Direct Memory Access
Understand Buffering concepts and types of buffer
know Spooling
Identify Device drivers
Overview of Device management
• Device management is the part of an OS that manages all hardware devices like disks, printers, keyboards,
and network cards.
• The OS provides a uniform interface to interact with diverse hardware and ensures efficient, reliable, and
secure communication.
 Keep tracks of all the devices and the programs
 Monitoring the status of each device
 Take the decision that which process gets the device when & for how long.
 Efficiently allocates and deallocates the devices
 Optimize the performance of individual devices
Most fit into the general categories of the following:
storage devices (disks, tapes),
transmission devices (network cards, modems), and
human-interface devices (screen, keyboard, mouse).
Serial versus Parallel Information Transfer

• The CPU is the central point for most information.


• When you start a program, the CPU instructs the storage device to load the program
into RAM.
• When you create data and print it, the CPU instructs the printer to output the data.
• Because of the different types of devices that send and receive information,
• Two major types of data transfers take place within a computer:
• Parallel and
• Serial Transfer
Parallel Transfer
• Parallel transfers use multiple “lanes” for data and programs, and in keeping with the bits byte or
even in words of the nature of computer information.
• Parallel transfers take place between the following devices:
• Parallel Port (LPT) (printer) port and parallel printer
• CPU and RAM
• CPU and interface cards Parallel Port (LPT)
• SCSI(Small Computer System Interface) port and SCSI devices: (e.g to connect DVD with PC)
• ATA(Advanced Technology Attachment)/IDE (Integrated Development Environment) host
adapter and ATA/IDE drives
• RAM and interface cards (either via the CPU or directly with DMA)
• Why are parallel transfers so popular?
• Multiple bits of information are sent at the same time
• At identical clock speeds, parallel transfers are faster than serial transfers because more data is
being transferred.(because of its capability to send multiple bits of data simultaneously)
 Advantages: faster transmission, simpler hardware.
 Disadvantages: expensive cabling, limited transmission distance, susceptibility to interference.
Cont…
• Parallel Devices:
• Transmit multiple bits simultaneously (typically a byte or word at a time) using multiple data
lines.
• Common examples: Printers (old), internal buses, hard drives (older ATA)
• Faster but more expensive and bulkier cables
• Suitable for short-distance communication
Parallel transfers problems:
• Many wires or traces (wire-like connections on the motherboard or expansion cards) are
needed, leading to interference concerns and thick, expensive cables.
• Excessively long parallel cables or traces can cause data to arrive at different times. This
is referred to as signal skew
• Differences in voltage between wires or traces can cause jitter.

• Parallel cables that are too long can cause signal skew, allowing the parallel signals to
become “out of step” with each other.
Serial Transfers

• A serial transfer uses a single “lane” in the computer for information transfers.
• This sounds like a recipe for slowdowns, but it all depends on how fast the speed limit is on the
“data highway”.
• The following ports and devices in the computer use serial transfers:
• Serial (also called RS-232 or COM) ports and devices
• USB (Universal Serial Bus) 1.1 and 2.0 ports and devices
• Modems (which can be internal devices or can connect to serial or USB ports)
• IEEE-1394 (FireWire, i.Link) ports and devices
• Serial ATA (SATA) host adapters and drives
Cont
• Serial Devices:
• Transmit data one bit at a time over a single communication line.
• Common examples: Keyboard, mouse, USB devices, modem
• Low cost, simpler cabling
• Slower compared to parallel communication
Cont…
• Serial transfers have the following characteristics:
• One bit at a time is transferred to the device.
• Transmission speeds can vary greatly, depending on the sender and receiver.
• Very few connections are needed in the cable and ports (one transmit, one receive, and a
few control and ground wires).
• Cable lengths can be longer with serial devices. For example, an UltraDMA/66 ATA/IDE
cable can be only 18 inches long for reliable data transmission, whereas a Serial ATA cable
can be almost twice as long.
• The extra speed is possible because serial transfers don’t have to worry about interference or other
problems caused by running so many data lines together.
Comparison
Feature Serial Devices Parallel Devices

Data Transfer 1 bit at a time Multiple bits at a time

Speed Slower Faster

Cost Low Higher

Distance Better over long range Suitable for short range

Examples USB, Mouse, Keyboard Parallel port printer


Abstracting Device Differences
• Devices vary in speed, control commands, data formats, etc.
• The OS abstracts these differences to provide a uniform interface for device interaction.

• Device Driver: A software component that abstracts hardware-specific operations.


• Uniform Naming: Devices are accessed via logical names (e.g., /dev/sda, COM1, LPT1).
• Device Classes:
• Block Devices: Store and retrieve blocks of data (e.g., hard disks).
• Character Devices: Handle data streams (e.g., keyboard, serial ports).
• I/O System Calls: OS provides system calls like read(), write(), open(), close() that work
similarly across different devices.
• Example: A read() system call can be used to read from:
• A file on disk (block device)
• A keyboard input (character device) OS ensures the actual hardware interaction is handled
internally via drivers.
Abstracting Device Differences
I/O Devices can be categorized into following category.
Human readable: Human Readable devices are suitable for communicating with the computer
user.
• Examples are printers, video display terminals, keyboard etc.
Machine readable: devices are suitable for communicating with electronic equipment.
• Examples are disk and tape drives, sensors, controllers and actuators.
Communication: devices are suitable for communicating with remote devices.
• Examples are digital line drivers and modems.
Buffering Strategies
• Buffering is a technique used to manage differences in speed between producer and consumer of data (e.g.,
CPU and I/O devices).
Types of Buffering:
1.Single Buffering:
1. Only one buffer used.
2. CPU may be idle while waiting for I/O to complete.
3. Simple, but can lead to performance loss.
2.Double Buffering:
1. Two buffers: While one is used for I/O, the other is processed.
2. Reduces waiting time and improves throughput.
3.Circular Buffering:
1. Multiple buffers in a circular queue.
2. Suitable for real-time streaming (e.g., audio/video).
3. Efficient but complex to manage.
Cont…
Example:
While reading from a disk:
•Buffer A is filled with data
•CPU processes Buffer B
•When B is done, switch back to A (in double buffering)
Direct Memory Access (DMA)
• DMA is a technique that allows devices to transfer data directly to/from memory without
involving the CPU for each byte.
• How It Works:
• A DMA controller takes control of the system bus.
• The CPU initializes the DMA (provides source, destination, size), then resumes other
tasks.
• DMA completes the data transfer and raises an interrupt when done.
• Advantages:
• Frees CPU from handling every byte transfer
• Speeds up data transfer
• Reduces overhead
Direct Memory Access
• DMA is an operation in which data is copied from one resource to another resource in computer system without
the involvement of the CPU
• Many computers avoid burdening the main CPU with PIO by offloading some of this work to a special-purpose processor
called a Direct-Memory-Access (DMA) controller.
• An alternative to DMA is the programmed Input/Output(PIO)interface in which all data transmitted between devices goes
via the processor.
• DMA controller is a second processor in the system but dedicated to an I/O function.
• To initiate a DMA transfer, the host writes a DMA command block into memory.
• This block contains a pointer to the source of a transfer, a pointer to the destination of the transfer, and a count of the
number of bytes to be transferred.
• The CPU writes the address of this command block to the DMA controller, then goes on with other work.
• The DMA controller proceeds to operate the memory bus directly, placing addresses on the bus to perform transfers
without the help of the main CPU.
• A simple DMA controller is a standard component in PCs.
Cont…
• The task of DMAC is to transfer data faster and efficiently than processor.
• The copy of data performed from:-
• I/O - device to memory
• Memory to I/O –device
• I/O device to I/O device
• Memory to memory
• Handshaking between the DMA controller and the device controller is performed via a pair of
wires called DMA-request and DMA-acknowledge.
• The device controller places a signal on the DMA-request wire when a word of data is available
for transfer.
• This signal causes the DMA controller to seize the memory bus, to place the desired address on
the memory-address wires, and to place a signal on the DMA-acknowledge wire.
• When the device controller receives the DMA-acknowledge signal, it transfers the word of data
to memory and removes the DMA-request signal.

Recovery from Failures
• Devices and I/O operations can fail due to hardware issues, power loss, disconnection, or software bugs.
• The OS must detect and handle these failures gracefully.
• Recovery from failure refers to the set of techniques used to restore a system back to a correct working
state after a hardware, software, or power failure.
• Goal: Ensure data integrity, system stability, and minimal disruption after a fault.
Why Failure Recovery Is Important
• Avoid data loss
• Maintain system reliability
• Ensure availability of services
• Protect against hardware and software errors
Recovery from Failures
Types of Failures:
• Transient Errors: Temporary (e.g., network timeout)
• Permanent Failures: Hardware damage (e.g., bad disk sectors)
• Intermittent Errors: Unpredictable (e.g., loose cable)
Types of Failures in Systems
Failure Type Description Example
Physical problems with Hard drive crash, memory
Hardware Failure
hardware components failure
Errors in program code or
Software Failure OS crash due to a bug
system software
Server shutting down during a
Power Failure Unexpected loss of electricity
storm
Communication Failure Network or transmission errors Packet loss during file transfer
Mistakes made by users or
Operator Error Accidental file deletion
admins
Recovery Techniques:
1.Retry Mechanism:
1. Automatically retry the operation (e.g., resend network packets)
2.Logging and Journaling:
1. Keep track of operations (e.g., file system journaling) for crash recovery
3.Timeouts and Error Codes:
1. Device drivers report errors to the OS which can inform user/processes
4.Failover:
1. Use alternative hardware/resource if one fails (e.g., RAID disks)
5.Alerts and Interrupts:
1. Devices generate interrupts for error conditions (e.g., paper jam in printer)
Cont…
Example:
In a disk write operation:
•If disk is busy or not responding → retry
•If sector is bad → mark as bad and try alternate sector
•If entire device fails → alert user or switch to backup disk (if configured)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy