Operating Systems Memory Management and Bash Scripting
Classified in Computers
Written on in
with a size of 4.74 KB
Memory Allocation Strategies
πΉ First Fit
π Take the first block that fits.
Example:
Blocks: 120, 450, 300, 700, 200
Processes: 250, 100, 500, 180
Step by step:
- 250 β goes into 450 β left: 200
- 100 β goes into 120 β left: 20
- 500 β goes into 700 β left: 200
- 180 β goes into 300 β left: 120
Final memory:20, 200, 120, 200, 200
πΉ Best Fit
π Use the smallest block that fits.
Same example:
- 250 β 300 β left 50
- 100 β 120 β left 20
- 500 β 700 β left 200
- 180 β 200 β left 20
Final:20, 450, 50, 200, 20
π Note: Causes more fragmentation (many small pieces).
πΉ Worst Fit
π Use the largest block.
Same example:
- 250 β 700 β left 450
- 100 β 450 β left 350
- 500 β β no block large enough
- 180 β 350 β left 170
πΉ Next Fit
π Same as First Fit, but continue from the last position.
βοΈ Do NOT restart from the beginning.
π§© Key Concepts
- Best Fit: More fragmentation.
- Worst Fit: Keeps large blocks available.
- General Rule: Always subtract memory after allocation.
π¦ Address Translation
Formulas
- Page = Address Γ· Page Size
- Offset = Address % Page Size
Example:
Page size = 1024
Address = 2500
- Page = 2
- Offset = 452
π Page Table
Steps:
- Get page and offset.
- Find frame in table.
- Use formula:
Physical Address = (frame Γ page size) + offset
Example:
Page table: Page 2 β Frame 8
Physical: (8 Γ 1024) + 452 = 8644
β‘ TLB (Translation Lookaside Buffer)
- TLB is a fast cache for address translation.
- Hit: Fast access.
- Miss: Slower (requires memory access).
- βοΈ Improves overall performance.
β±οΈ EMAT (Effective Memory Access Time)
Formula: EMAT = h(TLB + M) + (1 β h)(TLB + 2M)
Example:
- Memory (M) = 100 ns
- TLB = 20 ns
- Hit rate (h) = 80%
EMAT = 0.8(20 + 100) + 0.2(20 + 200) = 96 + 44 = 140 ns
π Multilevel Page Table
- Used because single-level tables are too large.
- Saves memory space.
- Slower due to multiple lookup steps.
π Page Replacement Algorithms
- FIFO: Oldest page removed.
- LRU: Least recently used page removed.
- Clock: Circular version of FIFO.
- NFU: Counts usage frequency.
- Aging: Improved version of NFU.
- Working Set: Tracks active pages.
π’ FIFO Example
Reference: 1, 2, 3, 1, 4, 5 | Frames = 3
| Ref | F1 | F2 | F3 | Fault |
|---|---|---|---|---|
| 1 | 1 | β | ||
| 2 | 1 | 2 | β | |
| 3 | 1 | 2 | 3 | β |
| 1 | 1 | 2 | 3 | β |
| 4 | 4 | 2 | 3 | β |
| 5 | 4 | 5 | 3 | β |
Total faults = 5
π₯οΈ Bash Scripting Essentials
Even / Odd Check
if (( $1 % 2 == 0 )); then echo "Even"; else echo "Odd"; fiSum 1 to N
sum=0; for ((i=1; i<= $1; i++)); do sum=$((sum + i)); done; echo $sumFile Exists
if [ -f "$1" ]; then wc -l "$1"; else echo "File not found"; fiCount .txt Files
ls *.txt 2>/dev/null | wc -lπ¨ Common Exam Mistakes
- Forgetting the offset.
- Using the wrong EMAT formula.
- Restarting Next Fit from the beginning.
- Not subtracting memory after allocation.
- Incorrect FIFO order.
π₯ Final Strategy
π Allocation: Go process by process and always update memory.
π Address: Divide for page, Modulo for offset.
π FIFO: Always draw the table.
π Bash: Keep logic simple and use standard syntax.