Process Schedulers In Operating System
A Process Scheduler is a key component of the Operating System kernel that decides which process moves from one state to another and when. Whenever a process travels between its life-cycle states New → Ready → Running → Waiting → Terminated, the schedulers control and manage these transitions by selecting processes from different queues and allocating CPU or other resources.
Process schedulers make sure that:
- The CPU is always kept busy (high utilization)
- All processes get a fair chance (fairness)
- Overall waiting and response times remain low (efficiency)
Types of Process Schedulers
There are three types of process schedulers:
1. Long Term or Job Scheduler
Long Term Scheduler loads a process from disk to main memory for execution. The new process to the 'Ready State'.
- It mainly moves processes from Job Queue to Ready Queue.
- It controls the Degree of Multi-programming, i.e., the number of processes present in a ready state or in main memory at any point in time.
- It is important that the long-term scheduler make a careful selection of both I/O and CPU-bound processes. I/O-bound tasks are which use much of their time in input and output operations while CPU-bound processes are which spend their time on the CPU. The job scheduler increases efficiency by maintaining a balance between the two.
- In some systems, the long-term scheduler might not even exist. For example, in time-sharing systems like Microsoft Windows, there is usually no long-term scheduler. Instead, every new process is directly added to memory for the short-term scheduler to handle.
- Slowest among the three (that is why called long term).
2. Short Term Scheduler
It is also called as CPU scheduler. It is the change of ready state to running state of the process.
- It picks a process from ready queue.
- Its main objective is to make the best use of CPU.
- It mainly calls dispatcher.
- Runs very frequently—often every few milliseconds—because CPU bursts (the time a process spends actually using the CPU before it needs I/O) are short.
Dispatcher:
The dispatcher is the module of the OS that gives control of the CPU to the process selected by the CPU scheduler. The dispatcher is responsible for loading the process selected by the Short-term scheduler on the CPU (Ready to Running State). Context switching is done by the dispatcher only. A dispatcher does the following work:
- Saving context (process control block) of previously running process if not finished.
- Switching system mode to user mode.
- Jumping to the proper location in the newly loaded program.
Dispatcher actually performs the switch so that process starts running.
Key Functions of the Dispatcher
Context Switching:
A context switching is the mechanism to store and restore the state or context of a CPU in Process Control block so that a process execution can be resumed from the same point at a later time. Using this technique, a context switcher enables multiple processes to share a single CPU. Context switching is an essential part of a multitasking operating system features.
Switch to User Mode:
Changes the CPU mode from kernel mode to user mode. Makes sure that it runs in the user mode and not kernel mode, which is for security and privilege break.
Jump to the correct instruction/location:
Starts execution of the process at the instruction where it was stopped (or at the beginning if it’s new).
Dispatcher Latency
- The time taken by the dispatcher to stop one process and start another is called dispatcher latency.
- Lower dispatcher latency = faster and smoother multitasking.
3. Medium-term scheduler
The Medium-Term Scheduler (MTS) is the part of the operating system’s process scheduling mechanism that can temporarily remove (suspend) a process from main memory and place it on disk (swap space), and later bring it back into memory when conditions are better.
- It reduces the degree of multiprogramming (Number of processes present in main memory).
- Free up main memory (RAM) when: The system is running low on RAM. Many high-priority or I/O-intensive jobs need memory.
- Improve CPU utilization by suspending processes that are waiting for long I/O operations.
How it Works (Swapping)
When the MTS decides to suspend a process:
1. Select a candidate process
Often a low-priority or I/O-bound process that has been waiting for a long time.
2. Swap Out
- The process’s current state (PCB + memory image) is saved to a swap area on the disk.
- The process state changes to Suspended (Ready Suspended or Waiting Suspended).
3. Swap In
- When resources become available or the waiting event is over, the process is brought back into RAM.
- Its state changes back to Ready or Waiting.
This movement of a process’s memory image between RAM and disk is called swapping.