Javatpoint Logo
Javatpoint Logo

Priority scheduling program in C

As computer programs become increasingly complex, the ability to efficiently manage system resources becomes crucial. One of the most important resources that must be managed is CPU time, which is finite and must be allocated to different processes running on a system. Priority scheduling is a widely used algorithm for CPU scheduling in operating systems. In this blog post, we'll take a deep dive into priority scheduling and provide a sample program in C.

What is Priority Scheduling?

Priority scheduling is a CPU scheduling algorithm where processes are assigned priorities, and the process with the highest priority is selected for execution. In priority scheduling, the scheduler chooses the process with the highest priority number from the ready queue and allocates the CPU to that process. The priority of a process is usually determined by its importance, the amount of CPU time it needs, or its deadline.

Priority scheduling can be divided into two types: preemptive and non-preemptive. In preemptive priority scheduling, the currently running process may be interrupted by a higher priority process, while in non-preemptive priority scheduling, once a process is assigned the CPU, it will continue to execute until it finishes or is blocked.

Example:

Let's look at the code of the Priority Scheduling program in C:

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 program, we first ask the user to enter the number of processes, process ID, burst time, and priority for each process. The input is stored in an array of structures called proc.

Next, we call the priority scheduling function, which sorts the processes based on their priority using a selection sort algorithm. After that, the sorted processes are passed to the find_average_time function, which calculates the waiting time and turnaround time for each process and prints the results along with the process ID, burst time, and priority.

In the output, we can see that process 2 has the highest priority and is assigned the CPU first. After process 2 is completed, process 1 and then process 3 are assigned the CPU based on their priority.

Conclusion:

Priority scheduling is a widely used algorithm for CPU scheduling in OS. It assigns priorities to different processes and chooses the process with the highest priority for execution. In this blog post, we provided a sample program in C for priority scheduling that sorts the processes based on their priority and calculates the waiting time and turnaround time for each process.


Next TopicPseudo code in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA