100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What Is the Medium-Term Scheduler and How Does Swapping Work?

Learn what the medium-term scheduler does, how process swapping works, and how it differs from paging, with an interview-ready example.

mediumQ222 of 224 in Operating Systems Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

The medium-term scheduler temporarily removes a process from memory (swaps it out to disk) to reduce the degree of multiprogramming, then later swaps it back in when memory pressure eases, sitting between the long-term scheduler’s admission decisions and the short-term scheduler’s CPU dispatch.

Unlike the long-term scheduler, which decides once whether to admit a process, the medium-term scheduler can act repeatedly on a process already running or ready, suspending it by writing its memory image to a swap area on disk and marking it suspended-ready or suspended-blocked. This frees physical memory for other processes and is typically triggered when memory becomes overcommitted or a process’s priority drops. When conditions improve — memory frees up or the process’s priority is needed again — the medium-term scheduler swaps the process back into memory, restoring it to the ready queue. Swapping is expensive because it involves full memory-image transfers to and from disk, which is why it is used as a coarser-grained control than short-term scheduling, and why systems under heavy memory pressure that swap excessively can slide into thrashing if swapping itself becomes too frequent.

  • Explains how the OS relieves memory pressure without killing processes
  • Clarifies suspended-ready and suspended-blocked as extra process states
  • Connects swapping cost to why it is a coarse, infrequent control
  • Distinguishes swapping (whole process) from paging (individual pages)

AI Mentor Explanation

The medium-term scheduler is like a team manager temporarily benching a fit player mid-series to make room in the squad budget for a specialist signing, sending the benched player’s full kit and files off-site rather than cutting them entirely. When budget or need allows, the manager recalls that player, unpacking their kit and reinstating them into active training. This full pack-up and recall is expensive and done sparingly, unlike the captain’s constant over-by-over bowling changes.

Step-by-Step Explanation

  1. Step 1

    Memory pressure detected

    The medium-term scheduler notices memory is overcommitted or a process's priority has dropped.

  2. Step 2

    Swap out

    The full memory image of a chosen process is written to a swap area on disk, freeing its physical frames.

  3. Step 3

    Suspended state

    The process is marked suspended-ready (or suspended-blocked), removed from the ready queue but not terminated.

  4. Step 4

    Swap in

    When memory frees up, the medium-term scheduler reloads the full image from disk and returns the process to the ready queue.

What Interviewer Expects

  • Correctly describing swapping as moving a whole process image, not individual pages
  • Explaining why the medium-term scheduler exists between long-term and short-term
  • Naming suspended-ready / suspended-blocked as extra process states
  • Awareness that excessive swapping contributes to thrashing

Common Mistakes

  • Confusing swapping (whole process) with paging (individual pages)
  • Thinking only the long-term scheduler controls memory residency
  • Forgetting that a swapped-out process is suspended, not terminated
  • Not recognizing swapping as an expensive, infrequent operation

Best Answer (HR Friendly)

The medium-term scheduler is the OS’s pressure valve for memory: if too many programs are loaded at once, it temporarily writes an entire program’s memory out to disk to free up space, and brings it back later once room opens up. It sits between the long-term decision of whether to let a program in at all and the short-term decision of which loaded program runs next, handling the in-between case of freeing up memory without killing anything.

Code Example

Simplified swap-out / swap-in via the medium-term scheduler
enum state { READY, RUNNING, SUSPENDED_READY };

struct pcb {
    int pid;
    enum state state;
    void *memory_image;   /* NULL once swapped out */
};

void swap_out(struct pcb *p) {
    write_to_disk(p->pid, p->memory_image);   /* full image to swap area */
    free_memory(p->memory_image);
    p->memory_image = NULL;
    p->state = SUSPENDED_READY;               /* removed from ready queue */
}

void swap_in(struct pcb *p) {
    p->memory_image = allocate_memory();
    read_from_disk(p->pid, p->memory_image);  /* full image restored */
    p->state = READY;                         /* back in the ready queue */
}

Follow-up Questions

  • How does swapping differ from paging out an individual page?
  • What triggers the medium-term scheduler to select a victim process?
  • Why is swapping considered more expensive than a context switch?
  • How can excessive swapping lead to thrashing?

MCQ Practice

1. What does the medium-term scheduler primarily manage?

The medium-term scheduler swaps entire process images out to disk to relieve memory pressure, and swaps them back in later.

2. A process that has been swapped out is typically marked as?

Swapping suspends the process rather than killing it, using states like suspended-ready or suspended-blocked.

3. How does swapping differ from paging?

Swapping operates on a whole process's memory image, while paging operates at the finer granularity of individual pages.

Flash Cards

What does the medium-term scheduler do?Temporarily swaps whole processes out of memory to disk, and back in later, to manage memory pressure.

What state does a swapped-out process enter?Suspended-ready (or suspended-blocked) — not terminated.

Swapping vs paging?Swapping moves an entire process image; paging moves individual pages.

Why is swapping infrequent?It is expensive, involving full memory-image transfers to and from disk.

1 / 4

Continue Learning