{"id":8730,"date":"2025-07-31T16:43:52","date_gmt":"2025-07-31T16:43:51","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=8730"},"modified":"2025-07-31T16:43:52","modified_gmt":"2025-07-31T16:43:51","slug":"scheduling-algorithms-fcfs-sjf-rr-priority","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/scheduling-algorithms-fcfs-sjf-rr-priority\/","title":{"rendered":"Scheduling Algorithms (FCFS, SJF, RR, Priority)"},"content":{"rendered":"<h1>Understanding Scheduling Algorithms: FCFS, SJF, RR, and Priority<\/h1>\n<p>As a developer, understanding process scheduling is crucial to optimizing performance and resource utilization in operating systems. Scheduling algorithms play a vital role in determining the order in which processes are executed on a CPU. In this article, we will delve into four fundamental scheduling algorithms: First-Come, First-Served (FCFS), Shortest Job First (SJF), Round Robin (RR), and Priority Scheduling. Each algorithm has its unique advantages, drawbacks, and use cases. Let\u2019s explore each one in detail.<\/p>\n<h2>1. First-Come, First-Served (FCFS)<\/h2>\n<p>The First-Come, First-Served scheduling algorithm is the simplest type of CPU scheduling algorithm. In FCFS, the process that arrives first in the ready queue gets executed first. This approach resembles a queue at a grocery store, where the first customer is served first.<\/p>\n<h3>Characteristics of FCFS<\/h3>\n<ul>\n<li><strong>Non-Preemptive:<\/strong> Once a process is allocated the CPU, it runs to completion without interruption.<\/li>\n<li><strong>Simplicity:<\/strong> Easy to implement and understand.<\/li>\n<li><strong>Fairness:<\/strong> All processes get a chance to be executed in the order of arrival.<\/li>\n<\/ul>\n<h3>Drawbacks of FCFS<\/h3>\n<ul>\n<li><strong>Convoy Effect:<\/strong> Short processes can get stuck waiting behind long ones, which leads to poor average wait times.<\/li>\n<li><strong>Throughput:<\/strong> Not optimal for time-sharing systems where response time is critical.<\/li>\n<\/ul>\n<h3>Example of FCFS<\/h3>\n<pre>\nProcess    Arrival Time    Burst Time\nP1             0                4\nP2             1                3\nP3             2                2\n<\/pre>\n<p>In this example, the processes would be executed in the order of P1, P2, P3, leading to a total waiting time of:<\/p>\n<pre>\nWaiting Time for P1 = 0\nWaiting Time for P2 = 4 (from P1) \nWaiting Time for P3 = 7 (4 from P1 + 3 from P2)\n<\/pre>\n<p>The average waiting time can be calculated as follows:<\/p>\n<pre>\nAverage Waiting Time = (0 + 4 + 7) \/ 3 = 3.67\n<\/pre>\n<h2>2. Shortest Job First (SJF)<\/h2>\n<p>Shortest Job First is a scheduling algorithm that selects the process with the smallest execution time. This non-preemptive algorithm minimizes the average waiting time for processes and can significantly improve system efficiency.<\/p>\n<h3>Characteristics of SJF<\/h3>\n<ul>\n<li><strong>Optimal for Average Waiting Time:<\/strong> SJF minimizes the average waiting time compared to any other scheduling method.<\/li>\n<li><strong>Non-Preemptive:<\/strong> Once a process starts executing, it runs to completion.<\/li>\n<\/ul>\n<h3>Drawbacks of SJF<\/h3>\n<ul>\n<li><strong>Starvation:<\/strong> Longer processes may never get executed if shorter processes continue to arrive.<\/li>\n<li><strong>Difficulty in Estimation:<\/strong> It can be challenging to know the exact burst time of processes in advance.<\/li>\n<\/ul>\n<h3>Example of SJF<\/h3>\n<pre>\nProcess    Arrival Time    Burst Time\nP1             0                8\nP2             1                4\nP3             2                9\nP4             3                3\n<\/pre>\n<p>In this case, the order of execution would be P2, P4, P1, then P3:<\/p>\n<p>The waiting times are calculated as:<\/p>\n<pre>\nWaiting Time for P2 = 0\nWaiting Time for P4 = 3 (time until P2 finishes)\nWaiting Time for P1 = 7 (3 + 4 for P2 and P4)\nWaiting Time for P3 = 16 (7 + 9 for P1)\n<\/pre>\n<p>Calculating the average waiting time gives us:<\/p>\n<pre>\nAverage Waiting Time = (0 + 3 + 7 + 16) \/ 4 = 6.5\n<\/pre>\n<h2>3. Round Robin (RR)<\/h2>\n<p>Round Robin is one of the fairest scheduling algorithms, designed primarily for time-sharing systems. It treats all processes equally and allocates a fixed time slice, known as a quantum, to each process.<\/p>\n<h3>Characteristics of RR<\/h3>\n<ul>\n<li><strong>Preemptive:<\/strong> If a process doesn\u2019t finish execution within its time slice, it is moved to the back of the queue.<\/li>\n<li><strong>Fair Process Sharing:<\/strong> All processes receive an equal opportunity to execute over time.<\/li>\n<\/ul>\n<h3>Drawbacks of RR<\/h3>\n<ul>\n<li><strong>Time Slice Value:<\/strong> Choosing a quantum that&#8217;s too high can lead to increased waiting time, while a lower quantum can lead to context-switching overhead.<\/li>\n<li><strong>Not Optimal for Short Processes:<\/strong> Longer waiting might lead to inefficiency for systems requiring quick responses.<\/li>\n<\/ul>\n<h3>Example of RR<\/h3>\n<pre>\nProcess    Arrival Time    Burst Time    \nP1               0              4             \nP2               1              5             \nP3               2              6             \nQuantum: 2\n<\/pre>\n<p>The execution order would be: P1, P2, P3, P1 (again), P2, P3, leading to varied waiting times for each process.<\/p>\n<p>Average waiting time calculation can be tedious here due to the multiple contexts. For instance:<\/p>\n<pre>\nWaiting Time for P1 = 2 + 4 = 6 (after P1, P2, and P3 complete their first slice)\nWaiting Time for P2 = 2 + 2 = 4 (after its first slice)\nWaiting Time for P3 = 2 (after P3 finishes first slice)\n<\/pre>\n<p>Average waiting time can then be computed as:<\/p>\n<pre>\nAverage Waiting Time = (6 + 4 + 2) \/ 3 = 4\n<\/pre>\n<h2>4. Priority Scheduling<\/h2>\n<p>Priority Scheduling is another crucial algorithm where every process is assigned a priority. The CPU is allocated to the process with the highest priority (the lowest number is typically the highest priority).<\/p>\n<h3>Characteristics of Priority Scheduling<\/h3>\n<ul>\n<li><strong>Preemptive or Non-Preemptive:<\/strong> Can be either depending on the system design.<\/li>\n<li><strong>Efficient for critical processes:<\/strong> Very useful for real-time systems.<\/li>\n<\/ul>\n<h3>Drawbacks of Priority Scheduling<\/h3>\n<ul>\n<li><strong>Starvation:<\/strong> Lower priority processes may wait indefinitely if higher priority processes keep coming.<\/li>\n<li><strong>Preemption Overhead:<\/strong> Can lead to constant context switching if done poorly.<\/li>\n<\/ul>\n<h3>Example of Priority Scheduling<\/h3>\n<pre>\nProcess    Arrival Time    Burst Time    Priority\nP1             0                4               2\nP2             1                2               1\nP3             2                1               3\n<\/pre>\n<p>In this scenario, with P2 having the highest priority, the execution order would be P2, P1, P3:<\/p>\n<p>Calculated waiting times:<\/p>\n<pre>\nWaiting Time for P1 = 1 (time until P2 finishes execution)\nWaiting Time for P2 = 0 (starts immediately)\nWaiting Time for P3 = 3 (after P1 and P2 complete)\n<\/pre>\n<p>Thus, the average waiting time can be computed as follows:<\/p>\n<pre>\nAverage Waiting Time = (1 + 0 + 3) \/ 3 = 1.33\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>In summary, understanding the varying scheduling algorithms\u2014FCFS, SJF, RR, and Priority\u2014gives developers the insight to optimize process execution based on specific needs, whether it&#8217;s fairness, efficiency, or responsiveness. Each algorithm has its unique pros and cons, and developers must choose the right one depending on the specific application requirements and system constraints. Having a solid grasp of these algorithms will undoubtedly enhance your development skills and system performance.<\/p>\n<p>As you continue to explore deeper into operating systems and processes, always remember that each scheduling algorithm can drastically impact performance, resource utilization, and overall user experience in software applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding Scheduling Algorithms: FCFS, SJF, RR, and Priority As a developer, understanding process scheduling is crucial to optimizing performance and resource utilization in operating systems. Scheduling algorithms play a vital role in determining the order in which processes are executed on a CPU. In this article, we will delve into four fundamental scheduling algorithms: First-Come,<\/p>\n","protected":false},"author":185,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1143],"tags":[1178,1177,1179,1176],"class_list":["post-8730","post","type-post","status-publish","format-standard","category-cpu-scheduling","tag-algorithms","tag-cpu","tag-process-execution","tag-scheduling"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/185"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=8730"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8730\/revisions"}],"predecessor-version":[{"id":8761,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8730\/revisions\/8761"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=8730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=8730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=8730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}