Written by Sai Sandeep Thota on April 26, 2013 in C > Programming

C Program to Implement Priority Scheduling

GET ALERTS:

Get our Latest updates delivered to your mailbox!

Priority scheduling is similar to Shortest Job First scheduling. Except in priority scheduling we ask the user to give Priorities to every process that enters the queue. Based on the priority given to each process the processes are swapped and the Process which is having the highest priority is executed first.

For instance we have three processes: P1, P2 and P3 having burst time of 3, 5, 1 and priorities of 3, 1, 2. According to Shortest Job first the Process which is having the lowest burst time is executed first. Here in Priority Scheduling the executing purely depends on the Priority given by the user. Here in this example we are having Process P2 with priority 1, so it enters the process queue first and completes the execution then process P2 and then process P1 will complete their execution.

The nested for loop is used to swap the processes according to their priority and place them in the Process Queue.

C Program : Priority Scheduling

#include<conio.h>
#include<stdio.h>

void main()
{
	int burst[10],pid[10],priority[10],wt[10],total, n, i, j, temp;
	clrscr();
	printf("\nPriority Scheduling");
	printf("\nEnter Number of Processes:");
	scanf("%d",&n);
	printf("\nEnter %d Processes Details\n\n",n);
	for(i=0;i<n;i++)
	{
		printf("\nEnter Process %d ID:",i+1);
		scanf("%d",&pid[i]);
		printf("\nEnter Process %d Burst Time:",i+1);
		scanf("%d",&burst[i]);
		printf("\nEnter Process %d Priority:",i+1);
		scanf("%d",&priority[i]);
	}

	for(i=0;i<n-1;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(priority[i]<priority[j])
			{
				//Swapping Processes Based on Priority
				temp=priority[i];
				priority[i]=priority[j];
				priority[j]=temp;
				//Swapping Process ID Accordingly
				temp=pid[i];
				pid[i]=pid[j];
				pid[j]=temp;
				//Swapping Burst Time
				temp=burst[i];
				burst[i]=burst[j];
				burst[j]=temp;
			}

		}
	}
    //Waiting time for the First Process is zero.
	wt[0]=0;
	for(i=1;i<n;i++)
	{
		wt[i]=wt[i-1]+burst[i-1];
		total=total+wt[i];
	}
	printf("\nPRIORITY\tP_ID\tP_TIME\tWT_TIME\n");
	for(i=0;i<n;i++)
	{
		printf("%d\t%d\t%d\t%d\n",priority[i],pid[i],burst[i],wt[i]);
	}
	getch();
}

Output:
priority_scheduling_c_program

{ 2 comments… read them below or add one }

logeshwari January 29, 2014 at 10:46 am

Good job.This program is very useful to me.

Reply

shahbaaz September 3, 2014 at 12:49 pm

very good

Reply

Leave a Comment

Previous post:

Next post: