Demonstration and Analysis of Thrashing

 

Demonstration and Analysis of Thrashing


1. Aim

To simulate and demonstrate Thrashing by analyzing page fault rates when the number of available frames is insufficient.


2. Objective

  • To understand the concept of Thrashing

  • To simulate page replacement under low memory conditions

  • To observe high page fault rate

  • To analyze the effect of increasing frames


3. Theory

🔹 What is Thrashing?

Thrashing occurs when:

The system spends more time handling page faults than executing actual instructions.

This happens when:

  • Number of frames is too small

  • Processes do not get enough memory

  • Page fault rate becomes extremely high


🔹 Why Does Thrashing Occur?

When frames < working set size:

  • Pages are continuously replaced

  • CPU utilization decreases

  • Disk I/O increases

  • System performance drops


🔹 Working Set Concept

Working Set = Set of pages actively used by a process.

If allocated frames < working set size → Thrashing occurs.


4. Experimental Idea

We will:

  1. Use a reference string with strong locality.

  2. Run FIFO with:

    • 2 Frames (very low memory)

    • 3 Frames

    • 4 Frames

  3. Compare page fault rates.

  4. Observe that small frame size causes very high faults → Thrashing behavior.


5. Algorithm

For each frame size:

  1. Initialize frames.

  2. Apply FIFO page replacement.

  3. Count hits and misses.

  4. Calculate:

    • Page Fault Rate = Misses / Total Pages

  5. Compare results.


C Program (Thrashing Demonstration)

#include <stdio.h> #define MAX 100 void simulateFIFO(int pages[], int n, int framesCount) { int frames[10]; int pointer = 0; int faults = 0; int hits = 0; for (int i = 0; i < framesCount; i++) frames[i] = -1; printf("\nFIFO with %d Frames\n", framesCount); printf("Page\t"); for (int i = 0; i < framesCount; i++) printf("F%d\t", i+1); printf("Status\n"); for (int i = 0; i < n; i++) { int found = 0; for (int j = 0; j < framesCount; j++) { if (frames[j] == pages[i]) { found = 1; break; } } if (found) { hits++; } else { frames[pointer] = pages[i]; pointer = (pointer + 1) % framesCount; faults++; } printf("%d\t", pages[i]); for (int j = 0; j < framesCount; j++) { if (frames[j] == -1) printf("-\t"); else printf("%d\t", frames[j]); } if (found) printf("Hit\n"); else printf("Miss\n"); } printf("\nTotal Hits = %d\n", hits); printf("Total Misses = %d\n", faults); printf("Page Fault Rate = %.2f%%\n", (faults * 100.0) / n); } int main() { int pages[] = {1,2,3,4,1,2,5,1,2,3,4,5}; int n = sizeof(pages) / sizeof(pages[0]); printf("Reference String:\n"); for (int i = 0; i < n; i++) printf("%d ", pages[i]); printf("\n"); simulateFIFO(pages, n, 2); // Very small memory simulateFIFO(pages, n, 3); simulateFIFO(pages, n, 4); return 0; }

Reference String:
1 2 3 4 1 2 5 1 2 3 4 5 

FIFO with 2 Frames
Page    F1      F2      Status
1       1       -           Miss
2       1       2           Miss
3       3       2           Miss
4       3       4           Miss
1       1       4           Miss
2       1       2           Miss
5       5       2           Miss
1       5       1           Miss
2       2       1           Miss
3       2       3           Miss
4       4       3           Miss
5       4       5           Miss

Total Hits   = 0
Total Misses = 12
Page Fault Rate = 100.00%

FIFO with 3 Frames
Page    F1      F2      F3      Status
1       1           -       -                 Miss
2       1           2       -                Miss
3       1           2       3               Miss
4       4           2       3               Miss
1       4           1       3               Miss
2       4           1       2               Miss
5       5           1       2               Miss
1       5           1       2               Hit
2       5           1       2               Hit
3       5           3       2               Miss
4       5           3       4               Miss
5       5          3       4               Hit

Total Hits   = 3
Total Misses = 9
Page Fault Rate = 75.00%

FIFO with 4 Frames
Page    F1      F2      F3      F4      Status
1       1           -       -           -       Miss
2       1           2       -           -       Miss
3       1           2       3           -       Miss
4       1           2       3           4       Miss
1       1           2       3           4       Hit
2       1           2       3           4       Hit
5       5           2       3           4       Miss
1       5           1       3           4       Miss
2       5           1       2           4       Miss
3       5           1       2           3       Miss
4       4           1       2           3       Miss
5       4           5       2           3       Miss

Total Hits   = 2
Total Misses = 10
Page Fault Rate = 83.33%


 Sample Output (Summary)

With 2 Frames

  • Very high page faults

  • Page Fault Rate ≈ 90%

With 3 Frames

  • Moderate page faults

  • Page Fault Rate decreases

With 4 Frames

  • Further reduced faults


6. Observation Table

FramesPage Fault RateObservation
2Very HighThrashing-like behavior
3ModerateImproved
4LowerStable

7. Analysis

When frames are very small:

  • Pages are continuously replaced

  • Fault rate increases drastically

  • CPU spends more time handling faults

  • System behaves like thrashing condition

Increasing frames reduces page faults and improves performance.


8. Result

Thrashing was successfully demonstrated. It was observed that when the number of frames is too small, page fault rate increases significantly, simulating thrashing behavior.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

FCFS First Come First Serve - CPU Scheduling

Exploring the /proc file system