Operating System - Process Schedulers



In an operating system, a process scheduling refer to the method by which the system determines which process will be allocated CPU time for execution. The main goal of process scheduling is to ensure that all processes get CPU time in a fair manner while ensuring maximum CPU utilization. This chapter will discuss the different types of process schedulers used in operating systems.

Types of Process Schedulers

There are three main types of process schedulers in an operating system −

Long Term Scheduler

The long term scheduler, also known as the job scheduler, is used to load processes from the disk to the main memory (i.e., the RAM). This means the process will move to ready state. It is the duty of the long term scheduler to control the degree of multiprogramming in the system. The long term scheduler selects processes from the job pool and loads them into memory based on certain criteria, such as priority, resource requirements, and CPU burst time.

The long term scheduler is invoked less frequently than the short term scheduler, typically every few seconds or minutes. Also the long term scheduler is slowest of all three schedulers because it involves disk I/O operations.

Example

For example, if there are 10 processes in the job pool and the long term scheduler is set to allow a maximum of 5 processes in memory at any given time, it will select 5 processes from the job pool and load them into memory. The remaining processes will remain in the job pool until the long term scheduler selects them for loading into memory.

// Job Pool
jobPool = [P1, P2, P3, P4, P5, P6, P7, P8, P9, P10]

maxProcessesInMemory = 5
[processesInMemory] = longTermScheduler(jobPool, maxProcessesInMemory)

// After scheduling
[processesInMemory] = [P6, P2, P9, P1, P4]
jobPool = [P3, P5, P7, P8, P10]

// Pseudocode for Long Term Scheduler
function longTermScheduler(jobPool, maxProcessesInMemory){
    let processesInMemory = []
    for(let i = 0; i < maxProcessesInMemory; i++){
        let process = selectProcessFromJobPool(jobPool)
        processesInMemory.push(process)
        removeFromJobPool(jobPool, process)
    }
    return processesInMemory
}

Short Term Scheduler

The short term scheduler, also known as the CPU scheduler, is responsible for selecting a process from the ready queue and allocating CPU to it for execution. The decision for allocation of CPU is made based on the scheduling algorithm used by the operating system. There are several scheduling algorithms, such as FCFS, SJF, and Round Robin, are the popular ones.

The image below shows difference between long term and short term scheduler −

Long Term and Short Term Scheduler
  • The short term scheduler is responsible for managing the ready queue, which contains all the processes that are ready to run.
  • The short term scheduler is called more frequently than the long term scheduler, usually every few milliseconds.
  • Also the short term scheduler is the fastest of all three schedulers because it needs to make quick decisions for allocation of CPU.
  • The short term scheduler uses dispatcher to allocate CPU to a process. A dispatcher is a special program that performs context switching between processes.
  • The dispatcher will save context (process control block) of previously running process if not finished. Then it will load the context of the next process to be executed and transfer control to that process.
  • The time taken by dispatcher to perform context switching is known as dispatch latency.

Example

For example, if there are 3 processes in the ready queue - P1, P2, and P3, and the short term scheduler is using the Round Robin scheduling algorithm with a time quantum of 5 milliseconds, it will allocate CPU to each process for 5 milliseconds in a cyclic manner.

// Ready Queue
readyQueue = [P1, P2, P3]
burstTimes = {
    P1: 12, // milliseconds
    P2: 7,  // milliseconds
    P3: 10  // milliseconds
}

timeQuantum = 5 // milliseconds

Time = 0
-> P1 runs from 0 to 5 (remaining burst time: 7)
Time = 5
-> P2 runs from 5 to 10 (remaining burst time: 2)
Time = 10
-> P3 runs from 10 to 15 (remaining burst time: 5)
Time = 15
-> P1 runs from 15 to 20 (remaining burst time: 2)
Time = 20
-> P2 runs from 20 to 22 (completes execution)
Time = 22
-> P3 runs from 22 to 27 (completes execution)
Time = 27
-> P1 runs from 27 to 29 (completes execution)

Medium Term Scheduler

The medium term scheduler is responsible for temporarily removing processes from the main memory and placing them in secondary storage, such as a hard disk. This process is known as swapping. This is often done to free up memory for other processes that need to be loaded into memory.

The key features of medium term scheduler are −

  • The medium term scheduler reduces the degree of multiprogramming in the system by swapping out processes that are not currently needed.
  • The medium term scheduler is invoked occasionally, when the system is under heavy load or when memory is low.
  • The medium term scheduler is slower than the short term scheduler but faster than the long term scheduler, as it involves both memory and disk I/O operations.
  • The medium term scheduler helps to improve the overall performance of the system.

The medium term scheduler can be visualized in the diagram below −

Medium Term Scheduler

Example

For example, if there are 5 processes in memory - P1, P2, P3, P4, and P5, and the medium term scheduler needs to free up memory for a new process P6, it may choose to swap out process P3 to secondary storage.

// Processes in Memory
processesInMemory = [P1, P2, P3, P4, P5]

newProcess = P6

Swap out P3 to secondary storage
processesInMemory = [P1, P2, P4, P5]

Load P6 into memory
processesInMemory = [P1, P2, P4, P5, P6]

Comparison of Process Schedulers

The following table summarizes the key differences between the three types of process schedulers −

Feature Long Term Scheduler Short Term Scheduler Medium Term Scheduler
Also Known As Job Scheduler CPU Scheduler Swapper
Function Loads processes from disk to memory Selects process from ready queue for CPU allocation Swaps processes between memory and secondary storage
Frequency of Calling Less Frequently (every few seconds or minutes) More Frequently (every few milliseconds) Occasionally (when memory is low)
Speed Slowest (involves disk I/O) Fastest (quick decisions for CPU allocation) Moderate (involves memory and disk I/O)
Multiprogramming Controls degree of multiprogramming Manages ready queue Reduces degree of multiprogramming

Conclusion

There are three types of process schedulers in an operating system. The long term scheduler controls the degree of multiprogramming by loading processes into memory, the short term scheduler allocates CPU time to processes in the ready queue, and the medium term scheduler manages memory by swapping processes in and out of secondary storage.

Advertisements