Priority Scheduling Program in C
Priority Scheduling Program in C
As computer programs become increasingly complex, correctly controlling system sources will become vital. Finite CPU time is one of the most important resources that must be controlled and should be allotted to systems-running strategies. A later-used set of guidelines for CPU scheduling in operating frameworks is priority scheduling. This tutorial will dive deeply into priority scheduling and provide a pattern software in C.
What is Priority Scheduling Algorithm in C?
Priority scheduling is a CPU scheduling mechanism in which systems are given priorities and the highest-priority system is chosen to be executed. In priority scheduling the scheduler selects the method from the ready queue with the greatest priority number and assigns the CPU to it. A system’s priority is commonly decided by its significance, the amount of CPU time it desires, or its deadline.
The algorithms prioritize the approaches that should be finished and schedule them hence. A higher-priority manner will get hold of CPU priority first, which can preserve until all techniques are completed. The algorithm that places the techniques inside the ready queue in the order decided by using their priority and chooses which has to visit the job queue for execution calls for both the job and the ready queue. Due to the process’s greater priority, the Priority Scheduling Algorithm transfers it from the ready queue to the work queue.
priority scheduling can be categorized into Preemptive and non-preemptive. The currently running process may be stopped using a higher priority process in preemptive priority scheduling much like in non-preemptive priority scheduling. nevertheless, once a system is given the CPU it will continue to run until it completes or is blocked.
Example:
CODE:
/*
* C program to implement priority scheduling
*/
#include <stdio.h>
//Function to swap two variables
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 );
// b is an array for burst time, p for priority, and an index for process id
int b[n],p[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”,&b[i],&p[i] );
index[i]=i+1;
}
for( int i=0;i<n;i++ )
{
int a=p[i],m=i;
//Finding out the highest priority element and placing it in its desired place
for( int j=i;j<n;j++ )
{
if( p[j] > a )
{
a=p[j];
m=j;
}
}
//Swapping processes
swap( &p[i], &p[m] );
swap( &b[i], &b[m] );
swap( &index[i],&index[m] );
}
// T stores the starting time of the process
int t=0;
//Printing scheduled process
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+b[i] );
t+=b[i];
}
printf( “\n” );
printf( “Process Id Burst Time Wait Time TurnAround Time\n” );
int wait_time=0;
for( int i=0;i<n;i++ )
{
printf( “P%d %d %d %d\n”,index[i],b[i],wait_time,wait_time + b[i] );
wait_time += b[i];
}
return 0;
}
Output:
The Output for the above program will be:
Program Explanation:
1. First, enter the total number of processes and store it in variable n.
2. Store the burst time and priority in variables b and p.
3. Finding the highest priority element and placing it in its desired place.
4. Sort the techniques based on priority.
5. Print the process with their time stamp (starting and ending times). Variable T stores the starting time of the process.
6. In the end, print each process’s waiting time and turnaround time. Waiting time is the time stamp in the ready queue, while turnaround time is the total time taken by the process( burst time + waiting time ).
Time Complexity: O( n*n )
Sorting takes time of the order of O( n*n ), So time complexity is of the order of O( n*n ).
Space Complexity: O( n )
Space is required to shop burst time, arrival time, and index, So space complexity is O( n ).
Run Time Testcases:
In this case, we enter “3” because of the variety of procedures, and the burst time and priority values are “p1: 10 2”, “p2: 5 0”, and “p3: 8 1”.
How is Prioritization Decided?
A process’s priority can be determined by considering many aspects, including 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 way of 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 divided into two halves, mentioned as-
1. Non-preemptive Priority Scheduling Algorithm
2. Preemptive Priority Scheduling Algorithm
Non-Preemptive Priority Scheduling Algorithm:
When a system with a higher priority comes into the queue while the process with the highest order continues to be executed, the execution of the persevering method isn’t stopped, and the approaching system with the higher priority must wait until the continued manner is completed.
Preemptive Priority Scheduling Algorithm:
Preemption is made possible when a process with a better priority is given to the CPU, even as the procedure with the significantly higher priority order continues to be finished. This occurs if and while a procedure with a higher priority enters the queue.
NOTE: In a particular circumstance where two strategies have equal priority, the first come, first serve ( FCFS ), a scheduling algorithm in and of itself, a tie-breaker is used to determine which procedure could be performed first.
Algorithm of Priority Scheduling program in C:
We now have a clue to create an algorithm for the priority scheduling software in C and put into effect the concern scheduling application in C after reviewing the theoretical notion and operating examples 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 a higher priority for execution.
- Determine the typical waiting and turnaround times.
- Print the ordered process execution along with the typical wait and turnaround times.
Analysis of Code — Priority Scheduling Program in C
The worst-case time complexity of the priority scheduling utility in C, which has sorting constructed in, is O( N² ).
No extra area may be wanted for operations because we’ve already supplied all of the relevant values, such as burst time and priority, so that the distance complexity can be O( 1 ).
The priority scheduling algorithm in C has the following characteristics:
- Each method is connected to a concern quantity and procedures are carried out in accordance with that quantity.
- The priority scheduling algorithm enables carrying out batch approaches.
- If two processes are within the priority, the primary system to reach the ready queue could be allowed to execute.
- In the case of preemptive priority scheduling, a process with a higher priority arrives at some point in the execution of a low-priority challenge, the low-priority undertaking is paused, and the new task is authorized to be completed.
- The priority that may be changed is called dynamic priority, and which cannot be changed is called static priority.
NOTE: The term ‘batch process’ describes the routinely and repeatedly run programmes that operating systems employ.
Program to Implement Priority Scheduling Algorithm:
The following code shows the implementation of the Priority Scheduling program in C:
CODE:
#include<stdio.h>
// structure representing a structure
struct importance_scheduling {
// name of the task
char task_name;
// time required for the execution
int burst_time;
// wait time of a task
int wait_time;
// total time of execution
int turn_around_time;
// importance of the task
int importance;
};
int main( ) {
// total no. of taskes
int no._of_task;
// total wait and turn_around time
int total = 0;
// temporary structure for swap
struct importance_scheduling temp_task;
// ASCII no.s are used to represent the name of the task
int ASCII_no. = 65;
// swapping place
int place;
//Avg wait time of the task
float avg_wait_time;
//Avg turn_around time of the task
float avg_turn_around_time;
printf( “Enter the total no. of Taskes: “ );
//Get the total no. of the task as input
scanf( “%d”, & no._of_task );
// initializing the array
struct importance_scheduling task[no._of_task];
printf( “\nPlease Enter the Burst Time and Importance of each task:\n” );
// get burst time and importance of all task
for ( int i = 0; i < no._of_task; i++ ) {
// assign names using ASCII no.
task[i].task_name = ( char ) ASCII_no.;
printf( “\nEnter the details of the task %c \n”, task[i].task_name );
printf( “Enter the burst time: “ );
scanf( “%d”, & task[i].burst_time );
printf( “Enter the importance: “ );
scanf( “%d”, & task[i].importance );
// increment the ASCII no. to get the next alphabet
ASCII_no.++;
}
// swap task according to high importance
for ( int i = 0; i < no._of_task; i++ ) {
place = i;
for ( int j = i + 1; j < no._of_task; j + + ) {
// check if importance is higher for swapping
if ( task[j].importance > task[place].importance )
place = j;
}
// swapping of lower importance task with the higher importance task
temp_task = task[i];
task[i] = task[place];
task[place] = temp_task;
}
// First task will not have to wait and hence has a wait time of 0
task[0].wait_time = 0;
for ( int i = 1; i < no._of_task; i++ ) {
task[i].wait_time = 0;
for ( int j = 0; j < i; j++ ) {
// calculate wait time
task[i].wait_time += task[j].burst_time;
}
// calculate total wait time
total += task[i].wait_time;
}
//Calculate the avg wait time
avg_wait_time = ( float ) total / ( float ) no._of_task;
// assigning total as 0 for next calculations
total = 0;
printf( “\n\nTask_name \t Burst Time \t Wait Time \t Turn_around Time\n” );
printf( “ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — \n” );
for ( int i = 0; i < no._of_task; i++ ) {
// calculating the turn_around time of the taskes
task[i].turn_around_time = task[i].burst_time + task[i].wait_time;
//Calculating the total turn_around time.
total += task[i].turn_around_time;
//Printing all the outputs
printf( “\t %c \t\t %d \t\t %d \t\t %d”, task[i].task_name, task[i].burst_time, task[i].wait_time, task[i].turn_around_time );
printf( “\n — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -\n” );
}
// calculating the avg turn_around time
avg_turn_around_time = ( float ) total / ( float ) no._of_task;
// avg wait time
printf( “\n\n Avg Wait Time : %f”, avg_wait_time );
// avg turn_around time
printf( “\n Avg Turn_around Time: %f\n”, avg_turn_around_time );
return 0;
}
In the above example, we expect each technique to arrive within the ready queue simultaneously ( 0 ).
· A method inside the priority scheduling software in c is represented with a structure called struct priority__scheduling.
· Waiting time represents the time the technique has to attend to go into a state of execution.
· The amount of time needed for a process to complete execution is known as turn round time.
· The technique is ordered primarily based on priority by swapping the lower priority system with a better priority technique using the temp_proces variable.
· The ready time is calculated using the procedure’s burst time and the priority strategies’ ready time. The ready time for the primary procedure is constantly zero.
· • The turnaround time is estimated by adding up the burst and ready times of the technique.
· The average turnaround time and average waiting time are calculated by dividing the total waiting and average time via various techniques.
· These average times may be used to estimate the efficiency of the algorithm.
Output:
The Output of the Program will be:
The Output depicts the equal order of execution as seen in the example section, C → A → B. We can infer that the rules are a perfect fit for the input strategies because the average waiting time in the previous example is significantly lower. The time complexity for the priority scheduling software in c for best, worst, and average time is,
· Θ( N ) for best-case time complexity
· Θ( N ) for Average case time complexity.
· Θ( N² ) for Worst case time complexity as here we’ve sorted according to the priority using the selection sort algorithm. The space complexity is Θ( 1 ).
N Represents number of processes
Benefits and Drawbacks of the C Priority Scheduling Programme:
1. Some of the Advantages of Priority Scheduling are:
· The priority scheduling set of rules provides a way to assign priorities to a procedure based totally on time of execution, memory requirements, etc.
· Essential strategies can be assigned better priorities to be assured of faster execution.
· The algorithm is described adequately for instances in which there’s a constrained number of processes.
· Priority-primarily based scheduling guarantees that excessive-priority strategies are performed first, which could lead to the crucial responsibilities quicker.
2. Some of the Disadvantages of Priority Scheduling are:
· Choosing the factor based on which priorities can be assigned is more challenging.
· The resources can’t be applied in parallel using this algorithm.
· Priority inversion: Priority inversion occurs when a low-priority process holds a valuable resource that a high-priority procedure requires. This can reason delays in the execution of high-priority procedures.
· Starvation: If the system is heavily loaded with high-priority procedures, low-priority techniques might also not be executed.
· Priority inversion avoidance techniques and priority inheritance or priority protocols can introduce extra complexity to the machine.
· Setting priorities may be difficult, especially when many processes have different priorities.
Conclusion:
Priority scheduling programs in C , provide a powerful way of optimizing mission execution in the operating system, ensuring efficient allocation of system resources and improving overall system performance.
This tutorial explored the priority scheduling, implementation strategies, and potential advantages of the priority scheduling application in C.
We started by understanding the idea of priority and its importance in determining the order of task execution. By assigning priorities to tasks based on their relative significance, priority scheduling programs allow us to allocate computing resources to reflect the specific desires of various tactics.
In this tutorial, we understood how priority scheduling works based totally on the higher priority value of the process in terms of execution. Pre-emptive and non-pre-emptive priority scheduling methods were investigated in detail. The priority scheduling software in C is carried out by swapping the process with lower priorities with the system having higher priorities.
On the basis of the typical waiting and turnaround times the algorithm’s effectiveness is determined. A low priority process could run the risk of running out of resources immediately. The algorithm gives a way to assign priority to essential processes for resources.