top of page


Public·6 members

How to Implement Preemptive Priority Scheduling Algorithm in C

How to Implement Preemptive Priority Scheduling Algorithm in C

Preemptive priority scheduling is a CPU scheduling algorithm that assigns a priority to each process and executes the process with the highest priority first. If a process with a higher priority arrives while another process is running, the running process is preempted and the higher priority process is executed. This algorithm can reduce the waiting time and response time of high priority processes, but it may cause starvation for low priority processes.

preemptive priority scheduling program in c download for windows 7

In this article, we will show you how to implement preemptive priority scheduling algorithm in C language. We will use a structure to represent a process, and an array to store the processes. We will also use some sorting functions to sort the processes by arrival time, burst time, and priority. We will calculate the completion time, turnaround time, waiting time, and response time of each process, and display them in a table. We will also draw a Gantt chart to show the execution order of the processes.

Step 1: Define the Process Structure

A process in the preemptive priority scheduling algorithm can be represented with a structure that contains the following fields:

  • pname: The name or ID of the process.

  • priority: The priority of the process. A lower value means a higher priority.

  • atime: The arrival time of the process.

  • btime: The burst time or CPU time required by the process.

  • restime: The response time of the process, which is the time from arrival to first execution.

  • ctime: The completion time of the process, which is the time when the process finishes execution.

  • wtime: The waiting time of the process, which is the time spent in the ready queue.

We can define the structure as follows:

struct node

char pname;

int priority;

int atime;

int btime;

int restime;

int ctime;

int wtime;


Step 2: Declare and Initialize an Array of Processes

We can declare an array of structures to store the processes. For example, we can use an array named a with size 1000. We can also declare another array named b to store the processes that are ready to execute, and another array named c to store the processes that are executed.

struct node a[1000], b[1000], c[1000];

We can initialize the array a by taking input from the user or hard-coding some values. For example, we can take input from the user as follows:

void insert(int n)

int i;







The function insert takes an integer parameter n, which is the number of processes, and reads their name, priority, arrival time, and burst time from the standard input. It also initializes their waiting time as -a[i].atime+1, which is equivalent to -1.

Step 3: Sort the Processes by Arrival Time

We need to sort the processes by their arrival time in ascending order, so that we can simulate their arrival in chronological order. We can use any sorting algorithm for this purpose, such as bubble sort, insertion sort, selection sort, merge sort, quick sort, etc. Alternatively, we can use some built-in sorting functions provided by C++ standard library, such as sort, qsort, etc.



Welcome to the group! You can connect with other members, ge...

Group Page: Groups_SingleGroup
bottom of page