DEV Community

Cover image for Scheduling Algorithms in Operating Systems - Part 4 - Priority Scheduling Algorithm
Anna
Anna

Posted on

Scheduling Algorithms in Operating Systems - Part 4 - Priority Scheduling Algorithm

You can skip the intro if you have been through the Part - 1 or Part - 2 or Part - 3 of the series

Like humans, the operating system needs to plan its activities. These activities are the various processes that need to be executed by the OS. The OS needs to schedule all its processes properly to ensure that they get executed without any problems and in minimal time.

For this purpose, there are many scheduling algorithms like the First Come First Serve (FCFS) algorithm, Shortest Job First (SJF) algorithm, etc. We will be discussing these algorithms in detail but before that, we need to understand some terms.

  1. Arrival Time: The time at which the process arrives in the processor for execution.

  2. Waiting Time: The time for which a process has to wait before it starts executing. A process may have to wait if some other process is being executed (in a uniprocessor environment) or when the resources required by the process are not available, etc. It is calculated as :

    Waiting time = Completion Time- Arrival Time- Service Time

    1. Service Time/ Burst time: The time required by the process to complete its execution.
    2. Turnaround Time: The interval between the time at which the process starts executing and the arrival time of the process. It is calculated using the formula: > Turnaround Time = Service Time + Waiting Time

Now, let us have a look at the Priority Scheduling algorithm :

Priority Scheduling Algorithm:

In this algorithm, every process has a specific priority and is executed according to the given priority.

  1. Non-Preemptive Priority Scheduling Algorithm:

In this algorithm, the process of highest priority is completely executed first and then the process with the next highest priority is executed.

Let's take an example:

In the above example, we have 5 processes A, B,C, D,E

Processes A and B enter the processor first. Out of those two processes, process B has a higher priority and hence, process B will be executed first.

Meanwhile, the other processes are added to the waiting queue.

Now, after B completes its execution, the waiting queue is checked. Process D is the process in the waiting queue with the highest priority. Hence, it is executed next.

Similarly, now from the queue, process E has the highest priority and is therefore executed.

Again, considering the priority, process C is executed….

followed by the process A.

Now, let us calculate the average waiting time and turnaround time.

Advantages:

The processes of higher priority get preference and are made to execute earlier.

Disadvantages:

The processes with lower priority may suffer starvation.

  1. Preemptive Scheduling Algorithm:

In this algorithm, if a process with a priority higher than that of the process being executed enters, the processor is preempted and that process starts executing.

For example,

Here at 0ms processes A and B enter the system. B being the process with the highest priority starts executing first….

..but as process D enters the processor since D has a higher priority, it starts executing next and B is added to the queue.

Now after D completes executing, (D gets completely executed as no other process with higher priority enters in between) B being the process with the highest priority starts executing next.

Next, the process E starts executing

Then, process C starts executing

And finally, process A starts executing

Now, let us calculate the average waiting time and turnaround time

The advantage of this algorithm is that the processes with higher priority do not have to wait, hence they are not starved.

In the next post, let us have a look at the Highest Response Ratio Next Algorithm.

Hope this helps you!

Have a nice day 😊!!

Top comments (0)