Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Priority Scheduling Program in C

Last Updated on May 25, 2023 by Prepbytes

This article delves into the world of priority scheduling programs in the C programming language, offering a comprehensive understanding of their inner workings and practical implementation. We will explore how priority scheduling algorithms prioritize tasks based on predefined criteria, allocating computing resources with a fine-tuned strategy that maximizes system efficiency.

By examining the fundamentals of priority scheduling, we aim to equip readers with the knowledge needed to develop and optimize their own priority scheduling programs. Whether you’re a seasoned programmer seeking to enhance your skill set or a curious enthusiast delving into the intricacies of operating systems, this article serves as an invaluable resource to navigate the realm of priority scheduling in C.

Priority Scheduling Algorithm in C

The algorithms prioritize the processes that must be carried out and schedule them accordingly. A higher priority process will receive CPU priority first, and this will continue until all of the processes are finished. The algorithm that places the processes in the ready queue in the order determined by their priority and chooses which ones to go to the job queue for execution requires both the job queue and the ready queue.
Due to the process’ greater priority, the Priority Scheduling Algorithm is in charge of transferring it from the ready queue to the work queue.

How is the Prioritization Decided?

A process’s priority can be determined by taking into consideration many aspects, such as its burst time and minimal memory requirements.

Several key words to understand when it comes to process scheduling:-

  • Arrival Time:- The time interval of the arrival of the process in the ready queue
  • Waiting Time:- The time interval for which the process needs to wait in the queue for its execution
  • Burst Time:- The time interval 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

If and when a process with a higher priority comes in the queue while the process with the highest order is still being executed, the execution of the continuing process is not stopped and the approaching process with the higher priority must wait until the ongoing process is finished.

Preemptive Priority Scheduling Algorithm

Preemption is made possible when a process with a higher priority is given the CPU while the process with the highest order is still being executed. This happens if and when a process with a higher priority enters the queue.

Note:- In the event of a specific circumstance where two processes have the same priority, the first come first serve (FCFS), which is a scheduling algorithm in and of itself, tie-breaker is used to determine which process would be performed first.

Dry Run Example of Priority Scheduling Algorithm

Let’s use the three processes A, B, and C from the table below as an example. The table includes the processes priorities, arrival time, and burst time. We use a non-preemptive priority scheduling technique to track each process as it runs.

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

As previously stated, Process A will receive the CPU because it is the only process running at the moment. The process will complete its work before another process receives the CPU because it is non-preemptive.

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

Processes B and C are in a waiting state on the fifth second, with B having already finished, but C will receive the CPU because of its higher priority.

Process Burst Time Arrival Time Priority
B 3 3 1

B will finish running on the 7th second, at which point the CPU will be transferred to the last remaining process, C, to continue running.

All of the processes will be completed by the algorithm at the 10th second.

Algorithm of Priority Scheduling Program in C

We now have the clue to create an algorithm for the priority scheduling program in C and implement the priority scheduling program in C after reviewing the theoretical notion and operational example of the priority scheduling algorithm.

  • Input the total number of processes that need to be run.
  • Give each process the burst time and priority that it needs to run.
  • Sort the process according to higher priority for execution
  • Determine the typical waiting and turnaround times.
  • Print the ordered process execution along with the typical wait time 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: The user is asked to provide input for a number of processes, as well as the priority and burst time for each process. The processes are sorted using selection sort according to their priority values, and their positions in the current arrays for burst time, priority values, and ordered execution are swapped using the swap function. To get the average waiting time and average turnaround time, the total waiting time and total turnaround time are calculated in additional code and then divided by the entire number of processes.

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

The worst-case temporal complexity of the priority scheduling program in C, which has sorting built in, is O(N^2).
As no more auxiliary space will be needed for operations because we have already provided all of the relevant values, such as burst time and priority, the space complexity will be O(1).

Conclusion
Priority scheduling programs in C offer a powerful means of optimizing task execution in operating systems, ensuring efficient allocation of system resources and enhancing overall system performance. Through this article, we have explored the intricacies of priority scheduling, shedding light on its fundamental concepts, implementation strategies, and potential benefits.

We began by understanding the concept of task priority and its significance in determining the order of task execution. By assigning priorities to tasks based on their relative importance, priority scheduling programs enable us to allocate computing resources in a manner that reflects the specific needs of different processes.

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

Q1: How are task priorities determined in priority scheduling programs?
Ans. Task priorities can be determined using various methods, depending on the specific requirements of the system. Common approaches include assigning fixed priorities to tasks during system initialization or allowing dynamic adjustment of priorities based on task characteristics, such as their deadlines, importance, or resource requirements.

Q2: What is a process control block (PCB) in the context of priority scheduling?
Ans. A process control block (PCB) is a data structure maintained by the operating system for each process or task. It contains essential information about the process, including its state, priority, program counter, register contents, and other relevant data. In the context of priority scheduling, PCBs play a crucial role in managing and tracking tasks, enabling the scheduler to make informed decisions regarding task execution and resource allocation.

Q3: What are the advantages of using priority scheduling programs?
Ans. Priority scheduling programs offer several advantages, including:

  • Responsiveness: Higher-priority tasks are executed promptly, ensuring quick response times for critical operations.
  • Deadline Compliance: Priority scheduling allows for the prioritization of tasks with impending deadlines, enabling the system to meet time constraints efficiently.
  • Resource Allocation: By assigning priorities, system resources can be allocated effectively, maximizing overall system efficiency.
  • Flexibility: Task priorities can be adjusted dynamically, allowing the system to adapt to changing requirements or conditions.
  • Fairness: Priority scheduling algorithms can be designed to ensure that lower-priority tasks receive an appropriate share of resources, preventing starvation.

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 *