1. Home
Operating System

OS Tutorial: Learn Operating Systems Basics

Learn Operating System fundamentals: concepts, processes, memory management, and more. Start your journey to mastering OS with our comprehensive tutorial.

  • 47
  • 7 Hours
right-top-arrow

Tutorial Playlist

47 Lessons
30

Priority Scheduling in OS

Updated on 19/07/2024458 Views

Assume the role of a chef for a moment. There you are – working in an extremely busy kitchen. You have orders pouring in from all around you – and each of different levels of importance.

Some orders from crucial customers are extremely important and call for prioritization. 

How would you make certain that these high-priority orders are served first without neglecting or overlooking any? 

Well, however you do it, in the world of operating systems, this is done using what is known as priority scheduling in OS. In this tutorial, I will walk you through all the nuances of priority scheduling in OS, along with priority scheduling algorithm in OS, priority scheduling example, and more! So, let’s begin. 

What is Priority Scheduling in OS?

Priority scheduling in OS, a method that operating systems use for deciding the order in which processes get run, is somewhat similar to giving some processes a more important status or priority than others. It's like having a "VIP lane" in the queue where high-priority processes can jump ahead and be treated with preference.

In priority scheduling, every process gets a priority value. This can be either fixed priority or dynamic priority that changes with time. The operating system then chooses the process having highest importance to execute next for getting access to CPU and other resources of the system.

Priority Scheduling Algorithm in OS

The priority scheduling algorithm in OS manages the order of execution for processes according to their priorities. It is a basic rule: the process with the highest priority will be run first. When more than one process possesses identical priority, they are usually scheduled using a first-come, first-serve (FCFS) method within this level of priority.

Here's a step-by-step breakdown of the priority scheduling algorithm in OS:

  1. Assign priorities to each process based on their importance or urgency.
  1. Order the processes in the ready queue by their priorities, with the process having the highest priority at the front.
  1. Select the process with the highest priority and allocate the CPU to it.
  1. When a fresh method comes with a greater priority than the ongoing process, we have to stop the current procedure and move to the one with higher-priority (in preemptive priority scheduling).
  1. Continue executing the process until it is completed, or a higher-priority process arrives.
  1. Repeat steps 3-5 until all processes are completed.

The operating system makes certain that high-priority processes get special treatment and are executed ahead of lower-priority processes by using these types of priority scheduling algorithm in OS. 

Preemptive Priority Scheduling Algorithm

Preemptive priority scheduling algorithm works like a variant of the priority scheduling algorithm, where a process that is currently running can be interrupted and substituted by another process with higher priority. It's similar to when a VIP guest comes in at a restaurant—they are given a seat right away, even if it means asking someone else to wait for their place.

In preemptive priority scheduling, the operating system always checks the ready queue for any new processes with higher priority. If a process of higher priority arrives while another process with lower priority is running, the operating system interrupts this current process. It saves its state and begins executing the new process that has arrived with higher importance.

Here's a preemptive priority scheduling example:

Imagine we have three processes: P1 (priority 3), P2 (priority 1) and P3 (priority 2). In the beginning, process P1 begins executing. Now, after a little time has passed, process P2 comes in with more importance. The operating system takes away control from P1, saves its condition, and then begins executing P2. After some time, P3 comes with a higher priority than P1 but is lower compared to that of P2. It is put in the ready queue, and when P2 finishes the execution cycle, then we start processing it. Finally, P1 resumes its execution after P3 completes.

Firstly, preemptive priority scheduling ensures that high-priority processes are given instant attention. This occurs regardless of whether they are running or not and can interrupt the execution of processes with lower priority.

If you wish to learn more about the details of priority scheduling and other process scheduling algorithms, I suggest you explore upGrad’s offerings in the domain of computer science. These courses include everything from OS, programming, database management, and more! 

Priority Scheduling Program in OS

Now, I’ll explain priority scheduling algorithm with example. For incorporating priority scheduling into an operating system, we can create a program to handle the ready queue. This program will pick out the process possessing the greatest priority for execution. Here is a basic priority scheduling algorithm example in OS:

function PriorityScheduling(processes):

while not isEmpty(processes):

highestPriorityProcess = findHighestPriorityProcess(processes)

execute(highestPriorityProcess)

remove(highestPriorityProcess, processes)

end while

end function


function findHighestPriorityProcess(processes):

highestPriorityProcess = processes[0]

for each process in processes:

if process.priority > highestPriorityProcess.priority:

highestPriorityProcess = process

end if

end for

return highestPriorityProcess

end function

In the pseudocode, the PriorityScheduling function keeps on finding a process with maximum priority using findHighestPriorityProcess function, executing and taking it out from the list of processes until all are finished.

The function findHighestPriorityProcess runs a loop through the list of processes and gives back the process having the highest priority.

Certainly, this is a basic form, and real versions could have more characteristics such as preemption, dynamic priority changes and dealing with many processes having equal priorities.

For a deep look into the inner workings of priority scheduling in OS and other scheduling algorithms, you may find upGrad's courses worth considering. The courses are designed to give you practical knowledge on how to implement different scheduling algorithms and comprehend their actions in actual situations.

Requirements and Benefits of Priority Based Scheduling In OS

Priority based scheduling algorithm offers several benefits and meets certain requirements in operating systems:

  1. Importance-based execution: Priority scheduling offers the advantage of executing processes in a sequence according to their importance or urgency. This means that crucial processes can be assigned greater priorities, guaranteeing they are served initially and assisting in reducing delays while enhancing system reaction time.
  1. Flexibility: Priority scheduling gives flexibility in prioritizing processes. We can assign priorities depending on different reasons, like the kind of process, the resources needed for it, or even criteria set by users themselves. The operating system can adjust according to varying workloads and improve the overall performance of the system.
  1. Preemption support: In a preemptive priority scheduling system, the operating system can interrupt processes that are lower priority to make way for ones with higher priority. This method guarantees immediate attention for high-priority processes and prevents them from being blocked by lower-priority processes, thus enhancing the responsiveness of the whole system.
  1. Resource utilization: Priority scheduling is beneficial for utilizing system resources effectively. When processes are arranged according to their importance, the operating system can assign resources in an organized manner, reducing resource loss and enhancing throughput.
  1. Real-time support: Priority scheduling is very important for real-time systems. This ensures that processes with high priority are given to tasks needing immediate action, helping them complete within the required time limits.

But, there are also a few setbacks to this method. It might cause starvation of lower-priority processes when higher-priority processes keep coming in, not allowing the lower ones to get any CPU time. Moreover, giving correct priorities to processes can be difficult and needs deep thought so as to prevent priority inversion and guarantee fair scheduling.

Concluding Remarks

Priority scheduling in OS is a major concept that underlies process execution order according to given priorities. This technique, by giving more importance to high-priority processes, makes sure vital tasks are handled first: it improves system response time and meets needs for execution based on importance.

Learning about priority scheduling is necessary for someone who enjoys working with operating systems to comprehend how processes are handled in real-world systems. It serves as a base for designing and enhancing operating systems that can manage different types of workloads appropriately and fulfill the requirements of present-day computing.

The courses from upGrad, covering memory management, file systems, and synchronization, along with other interesting subjects like computer science, software engineering, and more, are recommended to those who wish to explore the world of operating systems. These complete learning pathways will assist you in becoming a genuine expert in this field.

Keep on discovering, keep learning, and above all else, continue putting your love for operating systems at the forefront. Perhaps someday, you will design a new significant scheduling algorithm that transforms how computing is done.

Good luck with your scheduling, and may your processes always end up at the top of the priority queue!

FAQs

  1. What is the priority level in OS? 

The priority level in OS represents the process's importance or urgency. It decides the sequence of processes that run, prioritizing high-priority processes over low-priority ones.

  1. What is preemptive and non-preemptive priority? 

Preemptive priority scheduling gives permission for an ongoing process to get interrupted and substituted with a more important one, making sure crucial tasks are handled right away. Non-preemptive priority scheduling, in contrast, permits a process to finish its execution before switching to the next highest-priority one. This remains true even when a higher-priority process arrives during this period of time.

  1. What are the 3 types of scheduling queues? 

The three different types of scheduling queues are:

  • Ready Queue: Contains processes that are ready to execute and waiting for CPU allocation.
  • Device Queue: Contains processes that are waiting for I/O operations to complete.
  • Job Queue: It has all the processes in the system that are prepared and not ready for I/O.
  1. What is priority-driven scheduling? 

Priority-driven scheduling is another term for priority scheduling. It is a scheduling method that organizes processes by their given priorities, with those with more priority getting better treatment than lower-priority processes.

  1. What does it mean by non-preemptive priority scheduling in OS? 

Non-preemptive priority scheduling is a type of prioritized scheduling in which the currently running process can complete its task before the scheduler chooses the next process with the highest priority. If a process of lower priority is running and one with higher priority comes in, the scheduler will wait for the current process to finish before it swaps to the incoming high-priority one.

  1. What are the three priority levels? 

The three most used priority levels are:

  • High Priority: Processes with high priority are given the highest preference for execution.
  • Processes that have a medium priority run after high-priority processes but before those that possess low priority.
  • Low Priority: Processes with low priority are executed last, after high and medium-priority processes.
  1. What is the reason for using priority scheduling? 

Priority scheduling ensures that processes with greater significance or emergencies are executed before processes with lower priority. It allows the operating system to focus on important tasks, enhance system quickness in response, and fulfill importance-related execution needs.

  1. What is the main issue with priority scheduling? 

The major problem with priority scheduling is that it can cause starvation. When processes with higher priority keep coming in, those processes with lower priority might be delayed indefinitely. This can cause starvation, which means that these less important processes do not get enough time on the CPU or resources to finish their execution.

  1. What is priority scheduling with round robin?

This is a hybrid scheduling algorithm that uses both priority scheduling and the round-robin method. In this method, processes are arranged according to their importance level. If two or more processes have equal priority, we use the round-robin algorithm to arrange them. This means that every process receives a fixed amount of time quantum for execution before moving on to the next process with identical priority.

  1. What are priority scheduling advantages and disadvantages? 

Advantages of priority scheduling include:

  • Importance-based execution
  • Flexibility in assigning priorities
  • Preemption support for critical tasks
  • Efficient resource utilization
  • Real-time system support

Disadvantages of priority scheduling include:

  • Possibility of starvation for lower-priority processes
  • Difficulty in assigning appropriate priorities
  • Potential for priority inversion if not handled properly
  • Increased overhead due to priority-based scheduling decisions.
Amit Chandra

Amit Chandra

Amit Chandra, PMP, SCPM, is a program and product management professional with over 15 years of experience in publishing, EDA and Insurance domai…Read More

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...