Written by Sai Sandeep Thota on January 22, 2013 in C > Programming

C Program to Implement Shortest Job First (SJF)

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In the earlier post, we have discussed about FCFS. We’ll discuss how to implement Shortest Job First in C Programming, It is a method of load balancing or scheduling algorithm used to schedule a process by giving priority to its Burst time.. For instance, if there are three processes P1, P2 and P3 and having burst times of 4, 2, 10 respectively. P2 will be given the highest priority as it is having the shortest Burst time then P1 will be processed and then P3.

Here we will be using two more variables for swapping the Processes. temp is used for swapping the processes according to their burst time in ascending order and ptemp is used to swap the process name or id accordingly.

You may also consider reading:

Shortest Job First C Program

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

void main()
{
	int i, j, n, process[10], total=0, wtime[10], ptime[10], temp, ptemp;
	float avg=0;
	clrscr();
	printf("\nEnter number of Processes:");
	scanf("%d", &n);
	for(i=0;i<n;i++)
	{
		printf("\nEnter Process %d ID:",i+1);
		scanf("%d", &process[i]);
		printf("\nEnter Process %d Time:",i+1);
		scanf("%d",&ptime[i]);
	}

	for(i=0;i<n-1;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(ptime[i]>ptime[j])
			{
				temp = ptime[i];
				ptime[i] = ptime[j];
				ptime[j] = temp;
				ptemp = process[i];
				process[i] = process[j];
				process[j] = ptemp;
			}
		}
	}
		wtime[0]=0;
	for(i=1;i<n;i++)
	{
		wtime[i]=wtime[i-1]+ptime[i-1];
		total=total+wtime[i];
	}
	avg=(float)total/n;
	printf("\nP_ID\t P_TIME\t W_TIME\n");
	for(i=0;i<n;i++)
		printf("%d\t %d\t %d\n",process[i],ptime[i],wtime[i]);
	printf("\nTotal Waiting Time: %d \nAverage Waiting Time: %f", total, avg);
	getch();
}

Download Source Code

Output:

Shortest Job First C Program

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: