Priority scheduling functions as a CPU scheduling technique that involves assigning priorities to processes, selecting and executing the process with the highest priority. Within this algorithm, the scheduler picks the process with the greatest priority value from the ready queue and assigns the CPU to it. Typically, a process's priority is based on factors such as significance, required CPU time, or deadline.
Priority scheduling is categorized into two forms: preemptive and non-preemptive. Under preemptive priority scheduling, the running process can be interrupted by a higher-priority process, whereas in non-preemptive priority scheduling, a process will execute without interruption until completion or blocking.
Example:
Let's examine the code of the Priority Scheduling algorithm in the C programming language:
#include<stdio.h>
#include<stdlib.h>
struct process {
int process_id;
int burst_time;
int priority;
int waiting_time;
int turnaround_time;
};
void find_waiting_time(struct process[], int, int[]);
void find_turnaround_time(struct process[], int, int[], int[]);
void find_average_time(struct process[], int);
void priority_scheduling(struct process[], int);
int main()
{
int n, i;
struct process proc[10];
printf("Enter the number of processes: ");
scanf("%d", &n);
for(i = 0; i< n; i++)
{
printf("\nEnter the process ID: ");
scanf("%d", &proc[i].process_id);
printf("Enter the burst time: ");
scanf("%d", &proc[i].burst_time);
printf("Enter the priority: ");
scanf("%d", &proc[i].priority);
}
priority_scheduling(proc, n);
return 0;
}
void find_waiting_time(struct process proc[], int n, int wt[])
{
int i;
wt[0] = 0;
for(i = 1; i< n; i++)
{
wt[i] = proc[i - 1].burst_time + wt[i - 1];
}
}
void find_turnaround_time(struct process proc[], int n, int wt[], int tat[])
{
int i;
for(i = 0; i< n; i++)
{
tat[i] = proc[i].burst_time + wt[i];
}
}
void find_average_time(struct process proc[], int n)
{
int wt[10], tat[10], total_wt = 0, total_tat = 0, i;
find_waiting_time(proc, n, wt);
find_turnaround_time(proc, n, wt, tat);
printf("\nProcess ID\tBurst Time\tPriority\tWaiting Time\tTurnaround Time");
for(i = 0; i< n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", proc[i].process_id, proc[i].burst_time, proc[i].priority, wt[i], tat[i]);
}
printf("\n\nAverage Waiting Time = %f", (float)total_wtotal_wt/n);
printf("\nAverage Turnaround Time = %f\n", (float)total_tat/n);
}
void priority_scheduling(struct process proc[], int n)
{
int i, j, pos;
struct process temp;
for(i = 0; i< n; i++)
{
pos = i;
for(j = i + 1; j < n; j++)
{
if(proc[j].priority< proc[pos].priority)
pos = j;
}
temp = proc[i];
proc[i] = proc[pos];
proc[pos] = temp;
}
find_average_time(proc, n);
}
Output:
Enter the number of processes: 3
Enter the process ID: 1 Enter the burst time: 5 Enter the priority: 2
Enter the process ID: 2 Enter the burst time: 2 Enter the priority: 1
Enter the process ID: 3 Enter the burst time: 4 Enter the priority: 3
Process ID Burst Time Priority Waiting Time Turnaround Time 2 2 1 0 2 1 5 2 2 7 3 4 3 7 11
Average Waiting Time = 3.000000 Average Turnaround Time = 6.666667
Explanation:
In this software, we initially prompt the user to input the quantity of operations, operation ID, execution time, and precedence for each operation. The provided data is then saved in a structure array named proc.
Next, we invoke the priority scheduling function, which arranges the processes according to their priority using a selection sort technique. Subsequently, the arranged processes are forwarded to the calculateaveragetime function, which computes the waiting time and turnaround time for each process and displays the outcomes alongside the process ID, burst time, and priority.
In the result, it's evident that task 2 takes precedence and is given CPU priority initially. Following the completion of task 2, task 1 is next in line for CPU allocation, followed by task 3 according to their respective priorities.
Conclusion:
Priority scheduling stands as a popular algorithm employed for CPU scheduling within Operating Systems. It involves assigning priorities to various processes and selecting the one with the highest priority to run next. The following article includes a C program example for priority scheduling. This program organizes processes by priority, then computes the waiting time and turnaround time for each of them.