BCA / B.Tech 11 min read

Page Replacement Algorithms in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Page Replacement Algorithms in Hindi - General Topics
  • • अवलोकन: Quick Answer: Operating system में Page Replacement Algorithm वह तकनीक है जिससे यह तय होता है कि जब RAM (physical frames) पूरी भर जाए और नया page लोड क...
  • • मुख्य अवधारणाएं: Page replacement algorithms, FIFO LRU optimal Hindi, Second chance replacement,
Quick Answer: Operating system में Page Replacement Algorithm वह तकनीक है जिससे यह तय होता है कि जब RAM (physical frames) पूरी भर जाए और नया page लोड करना हो, तो किस पुराने page को RAM से हटाकर disk पर भेजा जाए। जब CPU किसी ऐसे page की demand करता है जो RAM में मौजूद नहीं होता, तो उसे page fault कहते हैं। सबसे मुख्य algorithms FIFO, LRU और Optimal हैं।

Page Replacement in OS क्या है? (Concept और Need)

Modern operating systems में virtual memory का use होता है। Virtual memory के जरिए OS physical RAM से बड़े programs को भी run कर लेता है। इसके लिए program को बराबर size के blocks में बांटा जाता है, जिन्हें pages कहते हैं। RAM को भी बराबर blocks में बांटा जाता है, जिन्हें frames कहते हैं।

जब कोई process execute होती है, तो उसके सारे pages एक साथ RAM में नहीं लाए जाते। केवल वही pages लोड होते हैं जिनकी CPU को तुरंत ज़रूरत होती है। इसे demand paging कहते हैं।

लेकिन RAM का size limited होता है। जब RAM के सारे frames भर जाते हैं और CPU एक नए page की मांग करता है, तो OS को RAM में मौजूद किसी पुराने page को बाहर (swap out) निकालना पड़ता है, ताकि नए page को अंदर (swap in) लाया जा सके। किस page को बाहर निकाला जाए, यह फैसला Page Replacement Algorithm करता है। Frames allocation के बारे में और जानने के लिए पढ़ें: Allocation of Frames in Hindi.

+---------------+        Page Fault         +------------------+
|   CPU Demand  | ------------------------> | Operating System |
+---------------+                            +------------------+
        |                                             |
        v                                             v
+---------------+   Frames Full: Swap Out   +------------------+
|  Page Table   | <------------------------ | Page Replacement |
+---------------+                           |    Algorithm     |
                                            +------------------+
Advertisement

Page Fault क्या होता है? (Step-by-Step Handling)

जब CPU किसी page को access करना चाहता है और वह page RAM में नहीं मिलता, तो hardware एक trap generate करता है जिसे Page Fault कहते हैं।

Page fault handle करने के मुख्य steps ये हैं:

  1. CPU internal page table check करता है कि memory reference valid है या नहीं।
  2. अगर reference invalid है तो process रुक जाती है। अगर valid है लेकिन page RAM में नहीं है, तो page fault interrupt आता है।
  3. OS process को wait state में भेजकर disk पर requested page को locate करता है।
  4. OS RAM में खाली frame ढूंढता है।
  5. अगर free frame उपलब्ध है, तो page सीधा disk से frame में read होता है।
  6. अगर कोई frame खाली नहीं है, तो page replacement algorithm चलता है और एक victim page चुना जाता है।
  7. Victim page को disk पर write किया जाता है (अगर वह modify हुआ हो)।
  8. खाली हुए frame में नया page load होता है और page table update होती है।
  9. Process को resume किया जाता है और CPU instruction दोबारा run होती है।

OS memory structures को detail में समझने के लिए देखें: Kernel Memory Allocation in Hindi.

Page Replacement Algorithms के प्रकार (Types)

Operating system में 5 मुख्य algorithms use होते हैं:

  • FIFO (First In First Out): जो page सबसे पहले आया, वह सबसे पहले बाहर जाएगा।
  • Optimal Page Replacement (OPT): जो page future में सबसे देर बाद use होगा, उसे हटाया जाता है।
  • LRU (Least Recently Used): जो page past में सबसे लंबे समय से use नहीं हुआ, उसे हटाया जाता है।
  • LFU (Least Frequently Used): जिसका reference count सबसे कम है, उसे बाहर किया जाता है।
  • Second Chance (Clock) Algorithm: FIFO का smart रूप, जिसमें reference bit से page को एक और मौका मिलता है।
Page Replacement Algorithms in Hindi

Page replacement algorithms की तुलना: frames में pages का placement और replacement flow.

1. FIFO (First In First Out) Algorithm

यह सबसे simple algorithm है। इसमें OS memory में loaded pages की एक FIFO queue बनाता है। जब memory full होती है, तो queue के front वाले सबसे पुराने page को replace कर दिया जाता है।

  • Working: नया page आने पर सबसे पहले load हुआ page बाहर निकलता है।
  • Advantage: Simple logic और queue की मदद से easy implementation.
  • Disadvantage: यह बार-बार use होने वाले जरूरी pages को भी सिर्फ पुराने होने पर हटा देता है। इसमें Belady's Anomaly आती है।

Belady's Anomaly क्या है?

आमतौर पर frames बढ़ाने से page faults कम होने चाहिए। लेकिन FIFO में कभी-कभी frames की संख्या बढ़ाने पर page faults घटने के बजाय बढ़ जाते हैं। इसे Belady's Anomaly कहते हैं।

Example: Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 में:

  • 3 frames के साथ page faults = 9
  • 4 frames के साथ page faults = 10 (frames बढ़ने पर भी faults बढ़ गए)

2. Optimal Page Replacement Algorithm (OPT)

Optimal algorithm का नियम है: उस page को replace करो जिसकी ज़रूरत future में सबसे लंबे समय तक नहीं पड़ेगी।

  • Working: Page fault होने पर OS आगे की reference string देखता है और सबसे देर बाद use होने वाले page को हटाता है।
  • Advantage: Minimum page faults देता है। इसमें Belady's Anomaly नहीं होती।
  • Disadvantage: Practical implementation असंभव है, क्योंकि OS को future references पहले से मालूम नहीं हो सकते।
  • Use: इसे benchmark की तरह use करते हैं ताकि बाकी algorithms की performance compare की जा सके।

3. LRU (Least Recently Used) Algorithm

LRU past references पर काम करता है। इसका principle है: जो page हाल ही में use हुआ है, वह आगे भी जल्दी use होगा (Locality of Reference)।

  • Working: OS backward देखता है। जो page सबसे लंबे समय से access नहीं हुआ, उसे victim बनाकर replace करता है।
  • Advantage: Optimal के बहुत करीब results देता है। इसमें Belady's Anomaly नहीं होती।
  • Disadvantage: Hardware support (counters या stack) चाहिए, जिससे system overhead बढ़ता है।

4. LFU और Second Chance (Clock) Algorithms

LFU (Least Frequently Used): इसमें हर page का reference counter होता है। सबसे कम frequency वाले page को हटाया जाता है।

Second Chance (Clock): इसमें हर page के साथ 1 reference bit होती है। Replacement के समय अगर bit = 0 है तो page replace होता है; अगर bit = 1 है तो bit को 0 करके page को queue में पीछे भेज दिया जाता है (दूसरा मौका मिलता है)। Continuous memory concepts के लिए देखें: Contiguous Allocation in Hindi.

Solved Numerical Example (Trace: FIFO vs LRU vs Optimal)

Exam problem: Reference string = 7, 0, 1, 2, 0, 3, 0, 4, 2, 3 और RAM frames = 3.

Comparison Trace Table (3 Frames)

Page Ref FIFO Frames FIFO Result LRU Frames LRU Result OPT Frames OPT Result
7[7, -, -]Fault[7, -, -]Fault[7, -, -]Fault
0[7, 0, -]Fault[7, 0, -]Fault[7, 0, -]Fault
1[7, 0, 1]Fault[7, 0, 1]Fault[7, 0, 1]Fault
2[2, 0, 1]Fault (7 out)[2, 0, 1]Fault (7 out)[2, 0, 1]Fault (7 out)
0[2, 0, 1]Hit[2, 0, 1]Hit[2, 0, 1]Hit
3[2, 3, 1]Fault (0 out)[2, 0, 3]Fault (1 out)[2, 0, 3]Fault (1 out)
0[2, 3, 0]Fault (1 out)[2, 0, 3]Hit[2, 0, 3]Hit
4[4, 3, 0]Fault (2 out)[4, 0, 3]Fault (2 out)[4, 0, 3]Fault (2 out)
2[4, 2, 0]Fault (3 out)[4, 0, 2]Fault (3 out)[2, 0, 3]Fault (4 out)
3[4, 2, 3]Fault (0 out)[4, 3, 2]Fault (0 out)[2, 0, 3]Hit

Final Summary of Trace:

  • FIFO: Page Faults = 9, Hits = 1
  • LRU: Page Faults = 8, Hits = 2
  • Optimal: Page Faults = 6, Hits = 4
Advertisement

Comparison: FIFO vs LRU vs Optimal

Feature FIFO LRU Optimal (OPT)
Logic First In First Out Least Recently Used Longest Future Distance
Page Fault Rate High (कमजोर) Low (बेहतर) Lowest (सर्वश्रेष्ठ)
Belady's Anomaly हाँ, होती है नहीं होती नहीं होती
Implementation Easy (Queue) Medium (Counter/Stack) Impossible (Future unknown)
Practical Use कम use होता है Real OS में widely used केवल Benchmark के लिए

C Program for LRU Page Replacement Algorithm

BCA/MCA exams के लिए simple और standard C program:

#include <stdio.h>

int findLRU(int time[], int n) {
    int i, min = time[0], pos = 0;
    for (i = 1; i < n; ++i) {
        if (time[i] < min) {
            min = time[i];
            pos = i;
        }
    }
    return pos;
}

int main() {
    int frames[3] = {-1, -1, -1};
    int time[3] = {0, 0, 0};
    int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3};
    int n = 10, no_frames = 3;
    int i, j, pos, faults = 0, counter = 0, flag1, flag2;

    for (i = 0; i < n; ++i) {
        flag1 = flag2 = 0;
        for (j = 0; j < no_frames; ++j) {
            if (frames[j] == pages[i]) {
                counter++;
                time[j] = counter;
                flag1 = flag2 = 1;
                break;
            }
        }
        if (flag1 == 0) {
            for (j = 0; j < no_frames; ++j) {
                if (frames[j] == -1) {
                    counter++;
                    faults++;
                    frames[j] = pages[i];
                    time[j] = counter;
                    flag2 = 1;
                    break;
                }
            }
        }
        if (flag2 == 0) {
            pos = findLRU(time, no_frames);
            counter++;
            faults++;
            frames[pos] = pages[i];
            time[pos] = counter;
        }
    }
    printf("Total Page Faults = %d\n", faults);
    printf("Total Hits = %d\n", n - faults);
    return 0;
}

Program Output:

Total Page Faults = 8
Total Hits = 2

Exam में कैसे लिखें (5 Marks Answer Format)

यूनिवर्सिटी exam में 5 marks के सवाल का answer इस structure में लिखें:

  1. Definition: Demand paging में memory full होने पर नए page के लिए पुराने page को swap out करने की तकनीक Page Replacement Algorithm कहलाती है।
  2. Page Fault: जब CPU का requested page RAM में नहीं मिलता, तो उसे page fault कहते हैं।
  3. FIFO: सबसे पुराने page को replace करता है। Queue से implement होता है। इसमें Belady's Anomaly संभव है।
  4. LRU: जो page सबसे देर से use नहीं हुआ, उसे हटाता है। Locality of reference पर काम करता है।
  5. Optimal: Future में सबसे देर बाद use होने वाले page को हटाता है। यह benchmark है।
  6. Mini Trace: 3 frames बनाकर 3 steps का छोटा trace diagram जरूर बनाएं।

Students की Common Mistakes

  • FIFO में Access Time देखना: FIFO केवल page load होने का time देखता है। Page बीच में कितनी भी बार hit हो, उसका removal order नहीं बदलता।
  • Optimal को Real OS में लागू मानना: Optimal theoretical है क्योंकि OS future references predict नहीं कर सकता।
  • LRU में Belady's Anomaly मान लेना: Belady's Anomaly सिर्फ FIFO में होती है, LRU में कभी नहीं।
  • Hit होने पर frames भूल जाना: Hit होने पर frame data वही रहता है, केवल LRU counter update होता है।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Page replacement algorithm क्या है? (RAM full होने पर victim page चुनने का algorithm)
  2. Page Fault क्या है? (जब requested page RAM में नहीं मिलता)
  3. Belady's Anomaly क्या है? (Frames बढ़ाने पर भी page faults का बढ़ जाना)
  4. Optimal algorithm practical क्यों नहीं है? (भविष्य के page references का पहले से पता न होना)
  5. Dirty bit का क्या use है? (Page modify हुआ है या नहीं, यह track करना ताकि unnecessary disk write न हो)

Long Questions (5–10 marks)

  1. Page Replacement क्या है? FIFO, LRU और Optimal algorithms को trace table के साथ समझाइए।
  2. Belady's Anomaly क्या है? उपयुक्त reference string लेकर सिद्ध कीजिए कि FIFO में frames बढ़ाने पर faults बढ़ सकते हैं।
  3. Second Chance (Clock) algorithm की working detail में समझाइए।
  4. LRU algorithm के implementation के लिए Counter और Stack methods समझाइए।

FAQs

Which page replacement algorithm is best in OS?

Theoretical रूप से Optimal Algorithm सबसे best है क्योंकि इसमें minimum page faults आते हैं। Practical systems में LRU और उसके variants (जैसे Clock algorithm) सबसे best माने जाते हैं।

What are page faults in OS?

जब CPU किसी virtual memory address को access करता है और वह page RAM में load नहीं होता, तो उसे page fault कहते हैं। OS इसे disk से page लाकर handle करता है।

LRU और FIFO में मुख्य अंतर क्या है?

FIFO केवल arrival time देखता है (सबसे पहले आया page पहले हटता है), जबकि LRU access time देखता है (सबसे कम हालिया use हुआ page हटता है)।

Page replacement के समय dirty bit का क्या काम है?

Dirty bit यह बताती है कि RAM में page modify हुआ है या नहीं। अगर bit 1 है, तो page को disk पर दुबारा write करना पड़ता है। अगर bit 0 है, तो disk write skip हो जाता है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Operating system में Page Replacement Algorithm वह तकनीक है जिससे यह तय होता है कि जब RAM (physical frames) पूरी भर जाए और नया page लोड करना हो, तो किस पुराने page को RAM से हटाकर disk पर भेजा जाए।
Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स के पाठ्यक्रम में Page Replacement Algorithms in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Page Replacement Algorithms in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Page Replacement Algorithms in Hindi
Distributed System in Hindi
Real Time System in Hindi
System Calls in Hindi
System Program in Hindi
Structure of Operating System in Hindi
Layers Design of Structure in Hindi
Unix in Hindi
Virtual Machine in Hindi
Kernel-Based Operating System in Hindi
Process in Hindi – OS में Process क्या है
Interacting Processes in Hindi
Threads in Hindi
Fundamental of Schedulling in Hindi
Scheduling Criteria in Hindi
Long Medium Short Term Scheduling in Hindi
Scheduling Algorithms in Hindi
Structure of Concurrent System in Hindi
Critical Region in Hindi
Critical Section in Hindi
Inter process Communication in Hindi
Monitor in Hindi
Semaphore in OS in Hindi – Counting और Binary Semaphore
Semaphores Implementation & Uses in Hindi
Logical and Physical Address in Hindi
Swapping in Hindi
Contiguous Allocation in Hindi
Segmentation in Hindi
Paging in OS in Hindi – Paging और Segmentation
Paging with Segmentation in Hindi
kernel Memory Allocation in Hindi
Virtual Memory in Hindi
Virtual Memory with demand Paging in Hindi
Deadlock & Characterization in Hindi
Methods for Handling Deadlocks in Hindi
Deadlock Thrashing in Hindi
Allocation of Frames in Hindi
Virtual Memory using Segmentation in Hindi
Interprocess Communication protocol in Hindi
Network Operating System in Hindi
Design Issues in Distributed File System in Hindi
Network Structure in Hindi
Structure Distributed System in Hindi
File System and Coordination in Hindi
Linux in Hindi
Commands of Linux in Hindi | लिनक्स के कमांड्स हिंदी में
Programmer Interface & File manipulation in Hindi
Process control in Linux in Hindi
Linux Kernel & Signals in Hindi
File System in Linux in Hindi
block & inodes in Hindi
System Editor in Hindi
Character Transliteration in Hindi
ED in Hindi
Vi Editor & There Commands in Hindi
Shell Script in Hindi
Looping & Decision Making in Hindi
Variables & File name Expansion in Hindi
Array in Hindi OS
Sub Program in Hindi
C interface with Linux in Hindi
Simpel Shell Programs in Hindi
BCA Part-1 | Operating Systems | Semester-I | MDSU Exam Paper 2023 (Held in 2024)
What is Operating System (OS) in Hindi
Operating System (OS) All Important Questions and Answers in Hindi (MDSU)
BCA || Operating System 2025 Paper || MDSU Exam Paper
BCA | OS(Operating System) 2023 Paper | MDSU Exam Paper
Type of Operating System in Hindi
Goals of Operating System in Hindi
Operation of Operating system in Hindi
Resource Allocation & Functions in Hindi
class of Operating system in Hindi
Batch Processing in Hindi
Multi Processing in Hindi
Time Sharing in Hindi