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
42

Preemptive Priority Scheduling Algorithm

Updated on 19/07/2024465 Views

Imagine yourself as a chef in a very busy kitchen, managing many different dishes that need attention at different times. Now, an important order from a VIP customer has arrived, and it needs to be worked on right away. 

What would you do? You give the VIP order attention first and delay other dishes until it has finished. 

This is what we mean by preemptive priority scheduling, and this is essentially what a preemptive priority scheduling algorithm does in operating systems! 

I am excited to tell you more about preemptive priority scheduling in this tutorial. So, let’s begin! 

What Is a Preemptive Priority Scheduling Algorithm?

In the culinary world of operating systems, we can compare the preemptive priority scheduling algorithm to a severe head chef who always guarantees that the most important dishes (processes) are served first, regardless of whether other tasks are being prepared. It's a method for arranging a time when every process gets one priority, and the processing unit (CPU) is given to the process having the highest priority, with the ability to stop the ongoing process if needed.

The preemptive priority scheduling algorithm, a method based on priority, brings in the idea of preemption. Preemption implies that when a process with higher priority comes, it can interrupt the lower-priority process currently running and take over control of the CPU. This method guarantees that essential processes are dealt with instantly and not delayed for an indefinite time period.

Preemptive Priority Scheduling vs Non-Preemptive Priority Scheduling

Now, to comprehend the preemptive priority scheduling algorithm, we can contrast it with non-preemptive priority scheduling. 

In non-preemptive priority scheduling, when a process begins running it carries on until finished or willingly gives up the CPU. It does not matter if another process with higher importance arrives during this time period. This behavior is similar to a chef who does not wish to stop preparing one meal for another, even if the latter order is more urgent.

However, with preemptive priority scheduling, the operating system can step in and take away the currently running process if a process of higher priority comes. It's similar to how a head chef might quickly shift gears and prioritize the VIP order, putting other dishes on hold.

Preemptive Priority Scheduling Example

To understand how preemptive priority scheduling functions, let us examine an example. Suppose we have three processes: P1 (priority 3), P2 (priority 1), and P3 (priority 2). The following sequence shows the arrangement:

  1. P1 starts executing as it has the highest priority.
  2. While P1 is executing, P2 arrives with priority 1.
  3. The preemptive priority scheduling algorithm immediately preempts P1 and starts executing P2.
  4. While P2 is executing, P3 arrives with priority 2.
  5. Since P3 has a lower priority than the currently executing P2, P3 is added to the ready queue, waiting for its turn.
  6. Once P2 completes, P3 starts executing as it has the highest priority among the remaining processes.
  7. Finally, P1 resumes its execution after P3 completes.

This example shows how the preemptive priority scheduling algorithm makes sure processes with higher priority are given more importance and can interrupt lower-priority process execution.

Non Preemptive Priority Scheduling Example

For a non preemptive priority scheduling case, we can use the same three processes: P1 (priority 3), P2 (priority 1), and P3 (priority 2). This is how the scheduling would go:

  1. P1 starts executing as it has the highest priority.
  2. While P1 is executing, P2 arrives with priority 1.
  3. Since the scheduling is non-preemptive, P2 is added to the ready queue, waiting for P1 to complete.
  4. While P1 is still executing, P3 arrives with priority 2.
  5. P3 is also added to the ready queue behind P2.
  6. Once P1 completes, P2 starts executing as it has the highest priority among the remaining processes.
  7. After P2 completes, P3 starts executing.

In this non preemptive scheduling situation, even if processes P2 and P3 have higher priorities, they must wait for process P1 to finish before starting execution.

Important Preemptive Priority Scheduling Questions

When you apply or study the algorithm of preemptive priority scheduling, it's crucial to think about these questions:

  1. How are priorities assigned to processes?
  2. Can priorities change dynamically during execution?
  3. How are ties resolved when multiple processes have the same priority?
  4. What mechanisms are in place to prevent priority inversion and starvation?
  5. How does the algorithm handle resource allocation and synchronization?

Answering these preemptive priority scheduling questions helps in designing an effective and robust preemptive priority scheduling system.

If the above questions make you curious, you should check out upGrad’s courses on computer science and engineering. Covering all important domains, including operating systems, these courses are designed to take you up from the very basics to a level where you comfortably understand all the important concepts and are in a position to apply your knowledge to real-world problems. Check out the courses and enroll yourself soon! 

Concluding Remarks

The preemptive priority scheduling algorithm is a strong method used in operating systems to make sure that tasks with high priority are given instant attention and run without delay. This technique is beneficial because it permits preemption, which allows the system to promptly respond to critical jobs and efficiently use CPU time.

In this tutorial, we have studied the idea of preemptive priority scheduling, compared it with non-preemptive scheduling and talked about its benefits as well as drawbacks. We also saw some instances to show how the algorithm functions in real life.

For someone who wants to become an expert in operating systems, knowing scheduling algorithms such as preemptive priority scheduling is very important. It helps with the basics of creating effective and quick systems that can handle different workloads and match what is needed for today's computing.

If you're excited about operating systems and want to learn more in areas like memory management, file systems, and synchronization, I suggest looking at the different courses from upGrad. They cover many topics related to operating systems and beyond, from computer science to software engineering. You can choose a complete learning journey with upGrad that will guide you toward becoming an expert in all things related to operating systems.

Continue to explore, continue to learn, and, above all else, keep accepting the art of process scheduling. Perhaps in the future, you could be the one who creates the next innovative scheduling algorithm that changes everything in operating systems around the world!

Happy scheduling, and may all your operations consistently make it to the front of the priority queue!

FAQs

  1. What is preemptive priority scheduling?

Preemptive priority scheduling is a kind of scheduling algorithm that gives a priority to every process. The CPU is given to the process with the highest priority. If there comes a higher-priority process while one with lower priority is working, we stop the lower-priority task (preempt it), so that it can start executing straight away.

  1. What is priority scheduling with preemption programs in C?

Programs in C that apply priority scheduling with preemption mean the usage of C programming language to create programs that demonstrate the functioning of a preemptive priority scheduling algorithm. This includes making a program that shows how the algorithm works, providing priorities for processes, and dealing with preemption when processes of higher priority arrive.

  1. What is an example of preemptive scheduling?

An instance of preemptive scheduling can be seen when there is a process with high priority, like a real-time system interrupt, that comes while another process of lower priority is running. The algorithm for preemptive scheduling will at once preclude the lower-priority operation and assign the CPU time towards this high-priority task. This guarantees rapid treatment for the interrupt.

  1. What is the use of preemptive scheduling?

Preemptive scheduling ensures that high-priority processes get immediate attention and are executed promptly. This helps the operating system to react quickly, use CPU effectively, and give better response time for users. Preemptive scheduling is very helpful in real-time systems and settings where some processes have strict timing limits.

  1. What are the types of preemptive scheduling?

Preemptive scheduling algorithms have different types, such as Round Robin with Preemption, Shortest Remaining Time First (SRTF), Preemptive Priority Scheduling, and Earliest Deadline First (EDF). These methods are distinct in how they decide which process to preempt and assign the CPU time to. This decision is made according to factors like time quantum, remaining execution time, priorities, or deadlines.

  1. What are the disadvantages of preemptive scheduling?

Some drawbacks of preemptive scheduling are context-switching overhead, priority inversion, starvation, and more complexity. Preemption needs to save and restore process states, which adds some overhead. When a high-priority process gets blocked because it's waiting for a resource held by a low-priority one, this condition is called priority inversion. Processes with lower priority could experience starvation if there is a constant arrival of high-priority processes. The introduction of preemption in scheduling algorithms can be more complicated than non-preemptive algorithms because it requires synchronization and dealing with preemption issues.

  1. Is SJF preemptive?

The Shortest Job First (SJF) scheduling algorithm can be preemptive or non-preemptive. In a preemptive setting, which is also referred to as Shortest Remaining Time First (SRTF), an incoming process with less remaining execution time can interrupt the current one. In a non-preemptive setting, once a process starts running, it will keep going even if another shorter process comes in.

  1. How do you solve preemptive priority scheduling?

To solve the preemptive priority scheduling, you must do these steps: give priorities to each process according to their importance or urgency; sort all processes in the ready queue by their priority; give CPU to the process having the highest priority; if a new process comes with more high priority than running one then it should be replaced and when any lower-priority process completes, or a higher-priority arrives, continue executing the previously preempted one. Repeat this procedure until all processes are finished.

  1. What are the advantages of preemptive scheduling techniques?

The benefits of preemptive scheduling methods are quicker response, better use of resources, flexibility and prevention of starvation. Processes with high priority get immediate attention so that important tasks can be handled promptly. The time of CPU is efficiently used by processes with high priority which reduces the idle periods. The system can adjust priorities in a flexible manner according to changing requirements, permitting it to adapt. Preemption enables lower-priority processes to execute when high-priority processes are not running, which stops infinite starvation.

  1. Is FIFO preemptive?

No, the First-In-First-Out (FIFO) scheduling algorithm, which is also called First-Come-First-Served (FCFS), does not stop processes. In this algorithm, processes are executed in the sequence they arrive at, and when a process starts running it goes on until finished without getting interrupted by other processes. FIFO doesn't involve any concept of priority for processes or allow preemption based on priority.

Rohan Vats

Rohan Vats

Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

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...