0
0
Operating Systemsknowledge~5 mins

Thrashing and working set model in Operating Systems - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Thrashing and working set model
O(n)
Understanding Time Complexity

When a computer runs many programs, it needs to manage memory efficiently. Thrashing happens when the system spends too much time swapping data in and out of memory instead of doing useful work.

We want to understand how the system's work changes as the number of programs or memory needs grow.

Scenario Under Consideration

Analyze the time complexity of this simplified memory management loop.


while (true) {
  for (each process) {
    if (process needs page) {
      if (page not in memory) {
        swap page in;
      }
    }
  }
}
    

This loop checks each process repeatedly to see if it needs pages in memory and swaps them in if missing.

Identify Repeating Operations

Look at what repeats often in this code.

  • Primary operation: Checking each process's page needs and swapping pages.
  • How many times: The outer loop runs forever, and inside it, the system checks all processes repeatedly.
How Execution Grows With Input

As the number of processes or pages grows, the system spends more time swapping pages.

Input Size (number of processes)Approx. Operations (page checks and swaps)
10Moderate checks and occasional swaps
100Many checks and frequent swaps, slowing work
1000Very high checks and constant swapping, system thrashes

Pattern observation: As processes increase, the system spends more time swapping pages, which grows quickly and hurts performance.

Final Time Complexity

Time Complexity: O(n)

This means the time spent checking and swapping grows roughly in direct proportion to the number of processes.

Common Mistake

[X] Wrong: "Adding more memory always stops thrashing immediately."

[OK] Correct: Sometimes, even with more memory, if the working set (pages needed) is too large or poorly managed, thrashing can still happen.

Interview Connect

Understanding how system performance changes with more processes and memory needs helps you explain real-world computer behavior clearly and confidently.

Self-Check

What if the system used a better working set model that predicts page needs more accurately? How would the time complexity change?