Thrashing and the Working Set Model are key concepts in Operating Systems that deal with memory management and system performance.
Thrashing
6
Thrashing occurs when a system spends more time
swapping pages between main memory and disk than executing actual processes.
Why it happens:
The system is overloaded with too many processes.
Each process doesn’t have enough frames (memory
pages).
Frequent page faults occur → OS keeps swapping pages
in and out.
Effects:
CPU utilization drops sharply.
Disk activity (I/O) increases dramatically.
System performance becomes extremely slow.
Simple intuition:
Instead of doing useful work, the system is “busy
moving data around.”
Working Set Model
6
The Working Set Model is a strategy to prevent
thrashing by ensuring each process has enough memory.
Definition:
The working set of a process is:
The set of pages that the process is actively using
during a given time window (Δ).
Key idea:
Programs exhibit Locality of Reference:
They tend to reuse a small subset of pages at a time.
If all these pages are in memory → few page faults.
If not → thrashing risk.
How it works:
OS tracks pages used in the last Δ time units.
Allocates enough frames to hold this working set.
If memory is insufficient:
Reduce number of processes (load control).
Relationship Between Them
Thrashing = Problem
Working Set Model = Solution
|
Aspect |
Thrashing |
Working Set Model |
|
Nature |
Performance issue |
Memory management technique |
|
Cause |
Too many page faults |
Prevents excessive page faults |
|
Goal |
Avoid it |
Ensure sufficient frames per
process |
|
Result |
Low CPU utilization |
Stable performance |
Quick Analogy
Imagine studying for exams:
Thrashing: Constantly switching between books without
focusing → no progress.
Working Set: Keeping only the necessary books on your
desk → efficient study.
Memory Management in Linux:
Linux memory management is the mechanism by which the
operating system efficiently allocates, tracks, protects, and reclaims memory
for processes and the kernel.
Goals of Memory Management
Linux memory management aims to:
Maximize CPU utilization
Provide process isolation and protection
Support multitasking
Use RAM efficiently
Minimize disk I/O
Enable virtual memory
Types of Memory in Linux
1. Physical Memory
Actual RAM installed in the computer.
2. Virtual Memory
Each process gets its own virtual address space.
Advantages
Process isolation
Larger address space than physical RAM
Easier programming model
Virtual Memory System
Linux uses paging.
A process’s virtual memory is divided into pages
(commonly 4 KB).
Physical memory is divided into frames.
Mapping:
Virtual page → Physical frame
This mapping is handled using page tables.
Address Translation
CPU-generated virtual addresses are translated to
physical addresses using:
Page Table
MMU (Memory Management Unit)
TLB (Translation Lookaside Buffer)
Linux Process Memory Layout
Typical process memory layout:
+-------------------+
| Stack |
| ↓ |
+-------------------+
| Shared Libraries |
+-------------------+
| Heap |
| ↑ |
+-------------------+
| Data Segment |
+-------------------+
| Text Segment |
+-------------------+
Segments Explained
Text Segment
Executable code
Usually read-only
Data Segment
Global/static variables
Heap
Dynamic memory (malloc, new)
Grows upward
Stack
Function calls/local variables
Grows downward
Paging in Linux
Linux uses:
Demand Paging
Copy-on-Write (COW)
Swapping
Demand Paging
Pages are loaded into RAM only when needed.
If page not in memory:
Page fault occurs
Kernel loads page from disk
Benefits:
Reduced memory usage
Faster process startup
Copy-on-Write (COW)
Used during fork().
Parent and child initially share pages.
Actual copying happens only when one modifies a page.
Benefits:
Saves memory
Improves performance
Swapping
When RAM is insufficient:
Inactive pages moved to swap space on disk.
Swap can be:
Swap partition
Swap file
Too much swapping may cause:
No comments:
Post a Comment