4.2.24 · D4 · HinglishOperating Systems

ExercisesFragmentation — internal vs external, compaction

2,556 words12 min read↑ Read in English

4.2.24 · D4 · Coding › Operating Systems › Fragmentation — internal vs external, compaction

Shuru karne se pehle, ek one-screen toolbox hai jo hum baar baar use karenge. Har symbol ko words mein spell out kiya gaya hai taaki pehli line koi bhi padh sake.


Level 1 — Recognition

Goal: sirf waste ka flavour naam karo aur formulas padho — abhi koi arithmetic tricks nahi.

Exercise 1.1

Ek process ko ek page diya jaata hai lekin woh sirf use karta hai. Fragmentation type ka naam batao aur wasted amount do.

Recall Solution 1.1

Waste process ke already owned ek block ke andar hai → internal fragmentation. Wasted .

Exercise 1.2

Free memory , , ke holes mein split hai. Ek process maangti hai aur refuse kar di jaati hai. Fragmentation type ka naam batao.

Recall Solution 1.2

Total free , phir bhi koi single hole itna bada nahi hai. Waste blocks ke beech mein rehti hai → external fragmentation.

Exercise 1.3

In do schemes mein se, Paging ya Segmentation, kaun sa external fragmentation se suffer karta hai, aur kaun sa internal se?

Recall Solution 1.3

Paging fixed-size frames use karta hai → koi bhi free frame kisi bhi request ke liye fit hota hai → koi external nahi, lekin partly-used last page se internal hota hai. Segmentation variable-size segments use karta hai → awkward leftover holes → external.


Level 2 — Application

Goal: dono formulas mein numbers plug karo, khaaskar ceiling ke saath.

Exercise 2.1

Ek process ko bytes chahiye. Blocks bytes ke hain. Blocks ki sankhya aur internal fragmentation compute karo.

Recall Solution 2.1

Blocks: . Kyun 6? Paanch blocks dete hain, toh ek aur zaroor hai. Given: bytes. Internal waste: bytes.

Exercise 2.2

, , bytes sizes ke teen processes ko har ek ke saath page kiya jaata hai. Teeno mein total internal fragmentation nikalo.

Recall Solution 2.2
  • : , given , waste .
  • : , given , waste . Kyun 2 blocks? Ek pura block se ek byte zyada hone par bhi ek poora doosra block force hota hai.
  • : , given , waste . Ek perfect multiple kuch bhi waste nahi karta. Total internal bytes.

Exercise 2.3

Free holes , , hain. External fragmentation ratio compute karo.

Recall Solution 2.3

Total free . Largest hole . Aadhi se zyada free memory se bade kisi bhi request ke liye unusable hai.


Level 3 — Analysis

Goal: averages, trade-offs, aur allocations ki sequences ke baare mein reason karo.

Exercise 3.1

Ek system processes run karta hai, har ek bytes ke saath paged hai, aur process sizes ke relative effectively random hain. Total internal fragmentation estimate karo, aur reasoning explain karo.

Recall Solution 3.1

Ek random size ke liye, aakhri (partial) block average mein aadha bhara hota hai, isliye expected waste per process bytes. Kyun ? Leftover , par uniform hai, isliye wasted tail average hota hai. Total estimate bytes .

Exercise 3.2

Wahi processes. Ek engineer block size ko half karke kar deta hai. Expected internal fragmentation ka kya hota hai, aur hidden cost kya hai?

Recall Solution 3.2

Naya expected waste per process bytes; total bytes . Internal waste roughly half ho jaati hai. Hidden cost: chote blocks matlab process per zyada blocks, isliye page table badhti hai (store aur walk karne ke liye zyada entries). Yeh ek trade-off hai, free win nahi — Memory Allocation Strategies dekho.

Exercise 3.3

ki memory variable partitions se allocate ki jaati hai. Requests aati hain: allocate (A), (B), (C); phir B free karo; phir (D) request karo. First-fit (Dynamic Memory Allocation / Memory Allocation Strategies) use karte hue, kya D fit hota hai? Jab D aata hai us moment ka external fragmentation ratio compute karo.

Recall Solution 3.3

A, B, C ke baad layout (addresses low→high): , tail hole ke saath . B free karo: layout . Ab free holes: aur ; total free , largest . D ko chahiye: first-fit holes scan karta hai, aur paata hai → D fail, chahe . Neeche dii picture dekho.

Figure — Fragmentation — internal vs external, compaction

Level 4 — Synthesis

Goal: tools combine karo — compaction, ratios, aur before/after reasoning.

Exercise 4.1

Exercise 3.3 ki failing state lo: inside . Compaction apply karo, phir request D () dobara attempt karo. Layout, naya ratio, aur kitne kilobytes move hue dikhao.

Recall Solution 4.1

Compact: C ko A ke against slide karo taaki dono holes upar pool ho jayein: . Ab single free block hai → D fit ho jaata hai. D rakhne ke baad: . Naya ratio (D rakhne se pehle): ka ek hole → (perfectly consolidated). Bytes moved: sirf ko slide karna pada (A pehle se bottom par hai). Toh copy hue. Cost bytes moved ke proportional hai.

Figure — Fragmentation — internal vs external, compaction

Exercise 4.2

Ek colleague propose karta hai: "ratio ko par rakhne ke liye har free ke baad compact karo." Iske khilaf do reasons do, aur woh scheme batao jo is poore problem ko sidestep karta hai.

Recall Solution 4.2
  1. Compaction memory copy karta hai aur processes ko pause karta hai ("stop the world"); har free ke baad karna un blocks ko relocate karne mein bahut CPU time waste karta hai jo shayad kuch seconds baad phir free ho jayein.
  2. Iske liye processes ko run time par relocatable hona chahiye aur har event par base registers re-update karne padte hain — har event par overhead. Sidestep: Paging fixed frames use karta hai isliye external fragmentation ho hi nahi sakti — koi bhi frame kisi bhi request ke liye fit hota hai — small internal waste ki keemat par. Virtual Memory bhi dekho, jo paging ke upar layer karta hai.

Exercise 4.3

Ek region poori tarah each ke fixed partitions mein carve ki gayi hai. Naun processes sizes MB ki hain, har ek ek partition mein rakhi gayi hain. Total internal fragmentation aur baaki empty partitions ki sankhya compute karo.

Recall Solution 4.3

Partitions available . Processes placed , toh empty partitions . Internal waste per placed process (har ek exactly ek partition fill karta hai): Note: woh empty partitions () unallocated free space hain, internal fragmentation nahi — internal sirf allocated partitions ke andar waste count karta hai.


Level 5 — Mastery

Goal: design karo, ek full trade-off quantify karo, aur choice defend karo.

Exercise 5.1

Tumhe processes serve karni hain jo average bytes each ki hain, ek aisi machine par jahan page-table cost bytes per page-table entry hai (process ke har use hone wale frame ke liye ek entry). Block size vs ko (a) total expected internal fragmentation aur (b) total page-table bytes par compare karo. Tum kaun sa choose karoge, aur kyun?

Recall Solution 5.1

(a) Internal fragmentation (expected per process):

  • : bytes .
  • : bytes .

(b) Page-table bytes (entries per process , ):

  • : entries → bytes.
  • : entries → bytes.

Combined overhead (waste + table):

  • : bytes.
  • : bytes.

choose karo: iska total overhead yahan bahut kam hai kyunki internal-fragmentation saving () extra page-table cost () se kahin zyada hai. Caveat: agar processes tiny hoon (har ek almost ek page ke barabar), toh table cost dominate kar sakti hai — hamesha numbers run karo.

Exercise 5.2

Design decision: ek real-time system lambi pauses tolerate nahi kar sakta lekin variable-size segments run karta hai jo externally fragment hote hain. Yeh compaction use karne se mana karta hai (bahut disruptive hai). Ek aisi scheme propose karo jo external fragmentation ko without a stop-the-world pause remove kare, aur woh fragmentation cost batao jo tum return mein accept karte ho.

Recall Solution 5.2

Segmentation (variable blocks) se Paging (fixed frames) par switch karo. Kyunki har frame identical hai aur koi bhi frame kisi bhi request ko satisfy karta hai, external fragmentation bilkul khatam ho jaata hai bina kisi relocation pass ke — koi copying nahi, koi pause nahi. Cost accepted: har process ke last partial page par thodi internal fragmentation (expected ). Yeh classic trade hai: thodi internal waste do aur external waste aur compaction pauses se azaadi pao. Virtual Memory ko upar layer karne se phir tumhe saare frames ek saath resident rakhne ki bhi zaroorat nahi rehti.


Recall Har level ka one-line recap

L1 flavour naam karo ::: block ke andar = internal; blocks ke beech = external. L2 formulas plug karo ::: aur ; hamesha round up karo. L3 averages ke baare mein reason karo ::: expected internal waste per process; chota help karta hai lekin page table bloat hoti hai. L4 compaction apply karo ::: yeh holes ko ratio par merge karta hai, cost bytes moved; empty partitions internal waste nahi hain. L5 design & defend karo ::: total overhead = internal waste + table cost minimize karo; paging external ko bina pause ke khatam karta hai.