Priority Scheduling Program in C

In this article, we are going to study the famous scheduling algorithm in an operating system which is a priority scheduling algorithm. Firstly, we cover the mechanism of the priority scheduling algorithm by grasping the theoretical concepts and understanding a live example of the algorithm.
In the further section of the article, the code, algorithm and analysis of the program are taught.

Priority Scheduling Algorithm, along with the rest of the scheduling algorithm remains an integral part of the interview questions asked to freshers.

Priority Scheduling Algorithm

The algorithms schedule processes that need to be executed on the basis of priority. A Process with higher priority will be given the CPU first and this shall keep until the processes are completed. Job Queue and Ready Queue are required to perform the algorithm where the processes are placed according to their prioritised order in the ready queue and selected to be placed in the job queue for execution.

The Priority Scheduling Algorithm is responsible for moving the process from the ready queue into the job queue on the basis of the process having a higher priority.

How is the prioritization decided?

The priority of a process can be decided by keeping certain factors in mind such as the burst time of the process, lesser memory requirements of the process etc.

A few of the important terminologies to know as a part of process scheduling:-

  • Arrival Time:- The time of the arrival of the process in the ready queue
  • Waiting Time:- The interval for which the process needs to wait in the queue for its execution
  • Burst Time:- The time required by the process for completion.
  • Turnaround Time:- The summation of waiting time in the queue and burst time.

Types of Priority Scheduling Algorithm

Priority Scheduling Algorithm can be bifurcated into two halves, mentioned as-

  1. Non-preemptive Priority Scheduling Algorithm
  2. Preemptive Priority Scheduling Algorithm

Non-preemptive Priority Scheduling Algorithm

During the execution of a process with the highest order, if and if a process with a higher priority arrives in the queue, the execution of the ongoing process is not stopped and the arriving process, with higher priority, has to wait until the ongoing process concludes.

Preemptive Priority Scheduling Algorithm

During the execution of a process with the highest order, if and if a process with a higher priority arrives in the queue, the execution stops with the CPU provided to the process with a higher priority, thus enabling preemption as a result.

Note:- On the occasion of a special case, when two processes have the same priority then the tie-breaker for the earliest process to be executed is decided on the basis of a first come first serve (FCFS) which is a scheduling algorithm in itself.

Dry Run Example of Priority Scheduling Algorithm

Let us take an example with three processes A, B and C with mentioned priority, arrival time and burst time in the table below. We trace one by one which process takes place using a non-preemptive priority scheduling algorithm.

Process Burst Time Arrival Time Priority
A 5 0 3
B 3 3 1
C 2 5 2

As mentioned, Process A will get the CPU being the only process at the point in time. Being non-preemptive, the process will finish its execution before another process gets the CPU.

Process Burst Time Arrival Time Priority
C 2 5 2
B 3 3 1

On the 5th second, Process B and Process C are in waiting state with B already elapsed time, but C will get the CPU due to the reason that it has a higher priority.

Process Burst Time Arrival Time Priority
B 3 3 1

On the 7th second, B will finish execution and CPU will be passed to the only process left i.e C to complete execution.

The algorithm will bind up performing all the processes at the 10th second.

Algorithm of Priority Scheduling Program in C

Now that we are done discussing the theoretical concept and working example of Priority Scheduling Algorithm, we have the hint to come up with an algorithm for the priority scheduling program in c and implement the priority scheduling program in c.

  • Input the total count of processes to be executed
  • Input the burst time and priority for each process to be executed
  • Sort the process depending on the execution by higher priority
  • Calculate the Average Waiting and Average Turnaround Time
  • Print the ordered execution of process and the average waiting and turnaround time

Program Code of Priority Scheduling in C

Having knowledge of priority scheduling, we are good to go with the priority scheduling program in C.

#include <stdio.h>
 
void swap(int *a,int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
int main()
{
    int n;
    printf("Enter Number of Processes: ");
    scanf("%d",&n);

    int burst[n],priority[n],index[n];
    for(int i=0;i<n;i++)
    {
        printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
        scanf("%d %d",&burst[i],&priority[i]);
        index[i]=i+1;
    }
    for(int i=0;i<n;i++)
    {
        int temp=priority[i],m=i;
 
        for(int j=i;j<n;j++)
        {
            if(priority[j] > temp)
            {
                temp=priority[j];
                m=j;
            }
        }
 
        swap(&priority[i], &priority[m]);
        swap(&burst[i], &burst[m]);
        swap(&index[i],&index[m]);
    }
 
    int t=0;
 
    printf("Order of process Execution is\n");
    for(int i=0;i<n;i++)
    {
        printf("P%d is executed from %d to %d\n",index[i],t,t+burst[i]);
        t+=burst[i];
    }
    printf("\n");
    printf("Process Id\tBurst Time\tWait Time\n");
    int wait_time=0;
    int total_wait_time = 0;
    for(int i=0;i<n;i++)
    {
        printf("P%d\t\t%d\t\t%d\n",index[i],burst[i],wait_time);
        total_wait_time += wait_time;
        wait_time += burst[i];
    }
    
    float avg_wait_time = (float) total_wait_time / n;
    printf("Average waiting time is %f\n", avg_wait_time);
    
    int total_Turn_Around = 0;
    for(int i=0; i < n; i++){
        total_Turn_Around += burst[i];
    }
    float avg_Turn_Around = (float) total_Turn_Around / n;
    printf("Average TurnAround Time is %f",avg_Turn_Around);
    return 0;
}

Explanation: input for a number of processes is taken from the user followed by the input for the priority and burst time for each process. Selection Sort is used to sort the process depending on their priority values with the swap function used to swap their position in the existing arrays for burst time, priority values and ordered execution. In further code, total waiting time and total turnaround time is calculated, only to be divided by the total number of process to find the average waiting time and average turnaround time.

Output

Enter Number of Processes: 2
Enter Burst Time and Priority Value for Process 1: 5 3
Enter Burst Time and Priority Value for Process 2: 4 2

Order of process Execution is
P1 is executed from 0 to 5
P2 is executed from 5 to 9

Process Id  Burst Time          Wait Time
P1                      5                       0
P2                      4                       5

Average waiting time is 2.500000
Average TurnAround Time is 4.500000

Analysis of Code – Priority Scheduling Program in C

As sorting has been performed in the priority scheduling program in C, the time complexity will rise to O(N^2) in the worst case.

The space complexity is going to be O(1) as no extra auxiliary space will be required for operations given that we are already providing all the necessary values like burst time and priority beforehand.

Conclusion
In this article on priority scheduling in c, we understood how priority scheduling works on the basis of the higher priority value of the process when it comes to execution. We studied the two different types of priority scheduling algorithms, preemptive and non-preemptive. The algorithm, code and cost of the program were discussed in this article as well.

Hope you liked this article on the Priority scheduling program in C. We expect to see you soon with another insightful article.

Frequently Asked Questions

1. What is the difference between dynamic priority and static priority?
If the priority can be altered, it refers to dynamic priority else if the priority remains fixed, it is a static priority.

2. What is the worst-case time complexity of the priority scheduling algorithm?
The worst-case time complexity is O(N^2) given the fact that selection sort is performed in the program to sort on the basis of the descending value of priority.

Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C
Linear Search Program in C
While Loop Program in C
C Program to Swap Two Numbers
Calculator Program in C Language
Simple Interest Program in C
Compound Interest Program in C

Leave a Reply

Your email address will not be published. Required fields are marked *