Fragmentation, Segmentation

Fragmentation ખાલી memory slots ની બરબાદી છે, જ્યારે Segmentation એક program ને functions અને blocks જેવા logical ટુકડાઓમાં તોડવું છે જેથી memory માં સાફસૂથરું fit થાય.

10 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

ગાયબ 40 MB નું રહસ્ય

ધારો કે તમે તમારા college computer lab માં બેઠા છો, એક ભારે Android development project load કરવાનો પ્રયાસ કરતાં જેને 40 MB સતત RAM જોઈએ. તમે system monitor તપાસો છો, અને એ સાફ 50 MB મફત memory દેખાડે છે. Perfect, બરાબર ને? પણ જ્યારે તમે run click કરો છો, તમને એક ખરાબ 'Out of Memory' crash મળે છે. 50 MB મફત જગ્યા વાળું એક system એક 40 MB program સમાવવામાં કેવી રીતે નિષ્ફળ થઈ શકે? જવાબ fragmentation નામની એક છુપી memory-બરબાદ કરતી બીમારીમાં છે, જે operating systems ને તમારા code ને જોવાની રીત સંપૂર્ણપણે બદલવા મજબૂર કરે છે.

Theory

Movie Theater Seating નો ફાંદો

કલ્પના કરો કે તમે અને ત્રણ મિત્રો એક movie theater જાઓ છો. ticket window executive તમને કહે છે, 'હા, અમારી પાસે 4 ખાલી seats ઉપલબ્ધ છે!' પણ જ્યારે તમે અંદર જાઓ છો, તમે એક ખાલી seat આગળની હરોળમાં, બે વચ્ચે, અને એક છેલ્લી હરોળમાં જુઓ છો. તમારા group માટે પૂરતી seats છે, પણ તમે 4 ના એક contiguous block તરીકે સાથે બેસી શકતા નથી. આ external fragmentation છે. હવે એક અલગ નિયમની કલ્પના કરો જ્યાં theater દરેક વ્યક્તિને એક પૂરો 3-seat couch book કરવા મજબૂર કરે છે ભલે એ એકલા આવે. જો તમે એ couch પર એકલા બેસો છો, તમારા block ની અંદર 2 seats બરબાદ થાય છે. એ internal fragmentation છે!

Theory

Fragmentation અને Segmentation સમજવું

memory management માં, memory બરબાદી બે રીતે થાય છે. Internal Fragmentation ત્યારે થાય છે જ્યારે memory fixed partitions માં વહેંચાયેલી હોય, અને એક process ને એની જરૂર કરતાં થોડો મોટો એક slot અપાય છે, એ partition ની અંદર બચેલી જગ્યા સંપૂર્ણપણે બરબાદ અને કોઈ બીજા દ્વારા અનુપયોગી છોડતાં. External Fragmentation ત્યારે થાય છે જ્યારે variable partitions સમય સાથે મફત memory ના નાના, વિખરાયેલા blocks બનાવે છે: કુલ મફત memory એક નવી process માટે પૂરતી મોટી છે, પણ એને allocate કરી શકાતી નથી કારણ કે એ સતત નથી. external fragmentation ઠીક કરવા, operating systems Segmentation વાપરે છે, એક non-contiguous memory allocation technique જ્યાં એક program ને એની કુદરતી logical units (જેમ main functions, stacks, કે global variables) માં વહેંચાય છે જેને segments કહે છે, એમને physical RAM ના અલગ ભાગોમાં store કરવાની પરવાનગી આપતાં.

At a glance

Internal અને External memory fragmentation વચ્ચેના મુખ્ય તફાવત.

FeatureInternal FragmentationExternal Fragmentation
એ ક્યાં થાય છેએક allocated partition block ની અંદરallocated partition blocks ની વચ્ચે
પ્રાથમિક કારણFixed size memory partitioningVariable size memory partitioning
ઉકેલvariable partitioning કે નાના slots વાપરોCompaction કે non-contiguous allocation
Process StatusProcess slot ઘેરે છે પણ એક ભાગ બરબાદ કરે છેકોઈ process વિખરાયેલા slots ઘેરી શકતી નથી

Think first

એક Classic University Exam Numerics સમસ્યા હલ કરવી

એક system માં આ ક્રમમાં ત્રણ મફત memory blocks છે: 20 KB, 50 KB, અને 30 KB. બે processes આવે છે: P1 ને 15 KB જોઈએ અને P2 ને 45 KB. જો system fixed partitioning વાપરે છે જ્યાં દરેક process એક પૂરો block લે છે, internal fragmentation calculate કરો. જો એક ત્રીજી process P3 ને 40 KB જોઈએ પછી આવે, સમજાવો કે એ કેમ નિષ્ફળ થાય છે અને fragmentation પ્રકારનું નામ જણાવો.

Show the answer

પગલે-પગલે calculate કરીએ:

1. P1 (15 KB) ને પહેલો block (20 KB) allocate થાય છે. આ block ની અંદર બરબાદ જગ્યા = 20 KB - 15 KB = 5 KB. આ Internal Fragmentation છે.

2. P2 (45 KB) ને બીજો block (50 KB) allocate થાય છે. આ block ની અંદર બરબાદ જગ્યા = 50 KB - 45 KB = 5 KB. આ પણ Internal Fragmentation છે.

3. અત્યાર સુધી કુલ Internal Fragmentation = 5 KB + 5 KB = 10 KB.

4. હવે, P3 (40 KB) આવે છે. બચેલો એકમાત્ર block ત્રીજો block (30 KB) છે, જે બહુ નાનો છે. P3 load થઈ શકતી નથી.

જો આપણે એને બદલે variable partitioning વાપર્યું હોત? P1 20 KB block થી બિલકુલ 15 KB લેત (એક 5 KB hole છોડતાં). P2 50 KB block થી બિલકુલ 45 KB લેત (એક 5 KB hole છોડતાં). ત્રીજો block 30 KB રહે છે. system માં કુલ મફત જગ્યા 5 KB + 5 KB + 30 KB = 40 KB હોત. P3 ને બિલકુલ 40 KB જોઈએ, પણ એ load થઈ શકતી નથી કારણ કે મફત જગ્યા ત્રણ અલગ holes (5 KB, 5 KB, 30 KB) તરીકે વિખરાયેલી છે. આ નિષ્ફળતાને External Fragmentation કહે છે.

Quiz

Segmentation વાપરતી વખતે Operating System એક ખાસ memory address કેવી રીતે શોધે છે?

  1. દરેક એક વાર address 0000 થી પૂરી RAM scan કરીને
  2. એક જોડ વાપરતાં Segment Table જોઈને: Segment Number અને Offset
  3. પૂરા program ને આંખ મીંચીને fixed 4 KB blocks માં બદલીને
  4. user ને physical address hex codes જાતે enter કરવા કહીને
Show the answer

એક જોડ વાપરતાં Segment Table જોઈને: Segment Number અને Offset

Segmentation એક logical address વાપરે છે જેમાં એક tuple હોય છે: (segment number, offset). CPU segment number ને એક Segment Table માં જુએ છે જેથી એ segment નો base physical address અને limit શોધે. એ પછી RAM માં ચોક્કસ સ્થાન શોધવા offset ને base address માં ઉમેરે છે, protection અને flexibility સુનિશ્ચિત કરતાં.

Think first

Segment Table Guard તપાસ

ધારો કે એક segment નો base address 2000 અને એક limit 150 છે. શું થાય છે જો એક process એ segment ની અંદર 160 ના એક offset સુધી પહોંચવાનો પ્રયાસ કરે? ફેલાવતાં પહેલાં એક અનુમાન લગાવો.

Show the answer

system તરત એક trap કે એક segmentation fault error generate કરશે! segment limit segment ના કાયદેસર કદને દર્શાવે છે. કારણ કે offset (160) મંજૂર limit (150) થી વધુ છે, hardware ની address-translation unit એક ગેરકાયદેસર memory access પ્રયાસ શોધે છે અને બીજા programs ની રક્ષા માટે એને તરત અટકાવે છે.

Watch out

Fragmentation Nomenclature નો ફાંદો

university exams માં, external fragmentation નો અર્થ એ નથી કે memory RAM ની બહાર બરબાદ થાય છે! students ઘણી વાર પોતાની answer sheets માં આ રમૂજી ભૂલ કરે છે. External નો અર્થ બરબાદી allocated blocks ની બહાર અને વચ્ચે થાય છે કારણ કે holes બહુ વિખરાયેલા છે. Internal નો અર્થ બરબાદી એક process ને allocate કરેલા એક ખાસ block ની અંદર થાય છે. પૂરા marks score કરવા આને સાફ રાખો.

Theory

ખરી દુનિયા કનેક્શન: Segmentation Faults

શું તમે ક્યારેય એક C કે C++ program લખ્યો અને કુખ્યાત 'Segmentation Fault (core dumped)' crash સાથે અથડાયા? હવે તમે બિલકુલ જાણો છો કે પડદા પાછળ શું થાય છે. તમારા code એ એક offset સુધી પહોંચવાનો પ્રયાસ કર્યો જે એક segment ની limit થી આગળ ગયો કે એક read-only code segment માં લખવાનો પ્રયાસ કર્યો. આધુનિક systems application execution માટે નક્કર security આપવા segmentation ને paging સાથે જોડે છે.

Summary

Key takeaways

  • Internal fragmentation એક allocated fixed-size block ની અંદર બરબાદ memory છે.
  • External fragmentation ત્યારે થાય છે જ્યારે કુલ મફત જગ્યા હાજર છે પણ નાના chunks માં વિખરાયેલી છે.
  • Segmentation એક program ને એક programmer ના દૃષ્ટિકોણ સાથે મેળ ખાતા logical blocks માં તોડે છે.
  • દરેક segment ની Segment Table માં એક entry હોય છે જે એનો physical base address અને limit size store કરે છે.
  • અનધિકૃત memory access અટકાવવા offsets ને segment limits સામે સખ્તાઈથી તપાસાય છે.
  • Memory hook: Internal box ની અંદર છે, External blocks ની વચ્ચે છે, Segmentation logical structures નું સન્માન કરે છે!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from File and Memory Management

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Fragmentation, Segmentation · Operating System · Gri-Learn