# Mid Term Scheduler & Swapping

The Medium-term Scheduler, also known as the Swapper, plays a crucial role in memory management in an operating system.

**What it does**:

* The medium-term scheduler is responsible for **swapping out processes** from main memory to secondary memory (disk) and vice versa. This is known as swapping.
* It removes processes from the CPU for a duration and reduces the degree of multiprogramming. After some time, these processes can again be reintroduced into main memory.
* The process execution will again be resumed from the point it left the CPU.

**Why it does it**:

* Swapping may be necessary to **improve the process mix** or because a change in memory requirements has overcommitted available memory, requiring memory to be freed up.
* It helps in maintaining a **perfect balance between the I/O bound and the CPU bound**.
* It **reduces the degree of multiprogramming**.

<figure><img src="https://1794968486-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fmr8TiLFLTzUjsTWGBqMp%2Fuploads%2Ft3wYyaLr0kuUt8fXgabb%2Fimage.png?alt=media&#x26;token=4cbcca89-a690-4e9d-8a95-1678c61e0f3e" alt="" width="563"><figcaption><p>Code Help Figure</p></figcaption></figure>

For example, the medium-term scheduler may decide to swap out a process which has not been active for some time, or a process which has a low priority, or a process which is page faulting frequently, or a process which is taking up a large amount of memory in order to free up main memory for other processes⁵. The process is then swapped back in later when more memory is available, or when the process has been unblocked and is no longer waiting for a resource⁵.

### Summary of Swapping&#x20;

**Swapping is a memory management technique used in operating systems**. Here's what it does:

* **Swap Out**: Swapping involves temporarily moving (swapping out) an idle or blocked process from the main memory to secondary memory (like a hard disk). This is done to ensure proper memory utilization and memory availability for those processes which are ready to be executed.
* **Swap In**: The process that was swapped out can be brought back (swapped in) into the main memory from the secondary memory when it needs to be executed.

**Why it's used**:

* **Memory Utilization**: Swapping helps in managing the limited main memory more efficiently. It ensures that memory is allocated to those processes which are currently in execution or are ready to execute.
* **Multiprogramming**: Swapping allows more processes to be loaded into main memory than can fit at one time, thus enabling multiprogramming.
* **Blocked Process Management**: If a process is blocked (waiting for I/O operation to complete, for example), instead of keeping it in main memory, the operating system can swap out the process to make room for other processes².

However, swapping has its disadvantages. It may lead to inefficiency if a resource or a variable is commonly used by multiple processes. Also, the process of swapping requires significant time (known as swap time) which can affect system performance. Despite these challenges, swapping is a fundamental aspect of memory management in operating systems.

In summary, the medium-term scheduler plays a vital role in managing memory utilization and ensuring efficient execution of processes by performing swapping operations¹²⁴⁵.
