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:

  1. Get page and offset.
  2. Find frame in table.
  3. 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

RefF1F2F3Fault
11βœ”
212βœ”
3123βœ”
1123✘
4423βœ”
5453βœ”

Total faults = 5


πŸ–₯️ Bash Scripting Essentials

Even / Odd Check

if (( $1 % 2 == 0 )); then echo "Even"; else echo "Odd"; fi

Sum 1 to N

sum=0; for ((i=1; i<= $1; i++)); do sum=$((sum + i)); done; echo $sum

File Exists

if [ -f "$1" ]; then wc -l "$1"; else echo "File not found"; fi

Count .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.

Related entries: