Introduction to Scheduling algorithms




CPU Scheduling is a process of determining which process will own CPU for execution while another process is on hold. The main task of CPU scheduling is to make sure that whenever
the CPU remains idle, the OS at least select one of the processes available in the ready queue for execution. The selection process will be carried out by the CPU scheduler. It selects one of the processes in memory
that are ready for execution.
There are two types of Scheduling algorithms:



Preemptive Scheduling

In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is important to run a task with a higher priority before another lower priority task, even if the lower priority task is still running. The lower priority task holds for some time and resumes when the higher priority task finishes its execution.



Non-Preemptive Scheduling

In this type of scheduling method, the CPU has been allocated to a specific process. The process that keeps the CPU busy will release the CPU either by switching context or terminating. It is the only method that can be used for various hardware platforms. That's because it doesn't need special hardware (for example, a timer) like preemptive scheduling.



When scheduling is Preemptive or Non-Preemptive?

To determine if scheduling is preemptive or non-preemptive, consider these four parameters:


  • A process switches from the running to the waiting state.
  • Specific process switches from the running state to the ready state.
  • Specific process switches from the waiting state to the ready state.
  • Process finished its execution and terminated.

Only conditions 1 and 4 apply, the scheduling is called non- preemptive.


All other scheduling are preemptive.






First Come First Serve(FCFS)

First Come First Serve is the full form of FCFS. It is the easiest and most simple CPU scheduling algorithm. In this type of algorithm, the process which requests the CPU gets the CPU allocation first. This scheduling method can be managed with a FIFO queue.

As the process enters the ready queue, its PCB (Process Control Block) is linked with the tail of the queue. So, when CPU becomes free, it should be assigned to the process at the beginning of the queue.



Characteristics of FCFS method:

  • It offers non-preemptive and pre-emptive scheduling algorithm.
  • Jobs are always executed on a first-come, first-serve basis
  • It is easy to implement and use.
  • However, this method is poor in performance, and the general wait time is quite high.


Shortest Job First Pre-emptive(SJFP)

Also,known as Shortest Remaining Time.In this method, the process will be allocated to the task, which is closest to its completion. This method prevents a newer ready state process from holding the completion of an older process.



Characteristics of SJFP method:

  • This method is mostly applied in batch environments where short jobs are required to be given preference.
  • This is not an ideal method to implement it in a shared system where the required CPU time is unknown.
  • Associate with each process as the length of its next CPU burst. So that operating system uses these lengths, which helps to schedule the process with the shortest possible time.


Shortest Job First Non Pre-emptive(SJFN)

It is another type of scheduling technique in which the process with the shortest burst time is given to CPU first for execution.



Characteristics of SJFN method:.

  • The average waiting time by using SJF is less than FCFS.
  • Since processes with shorter burst time is executed first hence the turnaround time is also short by using SJF.
  • SJF gives an improved output by selecting a shorter job first to execute.
  • This scheduling technique is useful for batch-time processing, where waiting for jobs to complete is not critical.


Longest Job First Pre-emptive(LJFP)

With this algorithm, the process having the maximum remaining time is processed first. In this, we will check for the maximum remaining time after an interval of time(say 1 unit) that is there another process having more Burst Time arrived up to that time.



How does LJFP Scheduling work?

  • The First step is to sort the processes according to their Arrival time in increasing order
  • The next step is to choose the process that least arrival time but having the most Burst Time. After that process it for 1 unit. After one unit you need to check if up to that time of execution; any other process is arrived or not
  • Just repeat the above steps until the execution of all processes.


Characteristics of LRTF Scheduling

  • It is a CPU scheduling algorithm that is used to determine the process to be executed first among all incoming processes in a systematic way.
  • This algorithm follows the preemptive approach because in this CPU is allocated to any process only for a fixed slice of time.
  • In this algorithm, processes are selected on the basis of the highest burst time(the one with the highest burst time is processed first) and this process runs till the fixed slice of time. After that, the selection process takes place again.
  • Due to the high value of the average waiting time, this algorithm is not optimal.


Longest Job First Non Pre-emptive(LJFN)

This algorithm mainly keeps the track of Burst time of all processes that are available at the arrival time itself and then it will assign the processor to the process having the longest burst time. In this algorithm, once a process starts its execution then it cannot be interrupted in between its processing. Any other process can be executed only after the assigned process has completed its processing and has been terminated.

This scheduling is similar to the SJF scheduling algorithm. But, in this scheduling algorithm, the priority is given to the process having the longest burst time.



The Drawbacks of LJFN are:

  • This algorithm leads to the reduction of processing speed due to which there is a reduction in the efficiency and utilization of the system.
  • Due to this algorithm, for a given set of processes, the average waiting time and average turn-around time increase.
  • This algorithm leads to the convoy effect.
  • With this algorithm, there is a possibility that a short process may never get executed and the system keeps on executing the long processes.


Round Robin(RR)

Round robin is the oldest, simplest scheduling algorithm. The name of this algorithm comes from the round-robin principle, where each person gets an equal share of something in turn. It is mostly used for scheduling algorithms in multitasking. This algorithm method helps for starvation free execution of processes.



Characteristics of Round Robin:

  • Round robin is a hybrid model which is clock-driven
  • Time slice should be minimum, which is assigned for a specific task to be processed. However, it may vary for different processes.
  • It is a real time system which responds to the event within a specific time limit.


Priority Pre-emptive(PP)

In Preemptive Priority Scheduling, at the time of arrival of a process in the ready queue, its Priority is compared with the priority of the other processes present in the ready queue as well as with the one which is being executed by the CPU at that point of time. The One with the highest priority among all the available processes will be given the CPU next.

Once all the jobs get available in the ready queue, the algorithm will behave as non-preemptive priority scheduling, which means the job scheduled will run till the completion and no preemption will be done.



Characteristics of PP:

  • A CPU algorithm that schedules processes based on priority.
  • It used in Operating systems for performing batch processes.
  • If two jobs having the same priority are READY, it works on a FIRST COME, FIRST SERVED basis.
  • In priority scheduling, a number is assigned to each process that indicates its priority level.
  • Lower the number, higher is the priority.
  • In this type of scheduling algorithm, if a newer process arrives, that is having a higher priority than the currently running process, then the currently running process is preempted.


Priority Non Preemptive(PN)

In the Non Preemptive Priority scheduling, The Processes are scheduled according to the priority number assigned to them. Once the process gets scheduled, it will run till the completion. Generally, the lower the priority number, the higher is the priority of the process.



Characteristics of PN:

  • A CPU algorithm that schedules processes based on priority.
  • It used in Operating systems for performing batch processes.
  • If two jobs having the same priority are READY, it works on a FIRST COME, FIRST SERVED basis.
  • In priority scheduling, a number is assigned to each process that indicates its priority level.
  • Lower the number, higher is the priority.


Highest Response Ratio Next(HRRN)

As HRRN is a non-preemptive scheduling algorithm so in case if there is any process that is currently in execution with the CPU and during its execution, if any new process arrives in the memory with burst time smaller than the currently running process then at that time the currently running process will not be put in the ready queue & complete its execution without any interruption.

HRRN is basically the modification of Shortest Job Next(SJN) in order to reduce the problem of starvation.

In the HRRN scheduling algorithm, the CPU is assigned to the next process that has the highest response ratio and not to the process having less burst time.

Response Ratio = (W+S)/S
Where,
W=It indicates the Waiting Time.
S=It indicates the Service time that is Burst Time.




Algorithm of HRRN is:

  • In the HRNN scheduling algorithm, once a process is selected for execution will run until its completion.
  • The first step is to calculate the waiting time for all the processes. Waiting time simply means the sum of the time spent waiting in the ready queue by processes.
  • Processes get scheduled each time for execution in order to find the response ratio for each available process.
  • Then after the process having the highest response ratio is executed first by the processor.
  • In a case, if two processes have the same response ratio then the tie is broken using the FCFS scheduling algorithm.



Characteristics of HRRN are:

  • This algorithm not only favors shorter job but it also concern the waiting time of the longer jobs.
  • Its mode is non preemptive hence context switching is minimal in this algorithm.