C program for shortest job first scheduling algorithm.

Posted by Mangesh on June 13, 2018

/* shortest job first scheduling program in c */

Description :

In shortest job first scheduling algorithm we first select the job with smallest burst time. Selected job is scheduled first, and after the completion of the scheduled job next job is selected from the remaining jobs. This process is carried out till all process are completed.

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
    int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
    float avg_wt,avg_tat;
    clrscr();
    printf("\n\nEnter number of process: ");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
	printf("Enter burst time for process %d: ",i+1);
	scanf("%d",&bt[i]);
	p[i]=i+1;
    }

    for(i=0;i<n;i++)
    {
	pos=i;
	for(j=i+1;j<n;j++)
	{
	    if(bt[j]<bt[pos])
		pos=j;
	}

	temp=bt[i];
	bt[i]=bt[pos];
	bt[pos]=temp;

	temp=p[i];
	p[i]=p[pos];
	p[pos]=temp;
    }

    wt[0]=0;
    for(i=1;i<n;i++)
    {
	wt[i]=0;
	for(j=0;j<i;j++)
	    wt[i]+=bt[j];

	total+=wt[i];
    }

    avg_wt=(float)total/n;
    total=0;

    printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
    for(i=0;i<n;i++)
    {
	tat[i]=bt[i]+wt[i];
	total+=tat[i];
	printf("\nprocess %d\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
    }

    avg_tat=(float)total/n;
    printf("\n\nAverage Waiting Time=%f",avg_wt);
    printf("\nAverage Turnaround Time=%f\n",avg_tat);
    getch();
}

Output :

c program for shortest job first Executed and Tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program for first come first serve scheduling.
2 C program for shortest job first scheduling algorithm.
3 C program for shortest remaining time first scheduling algorithm.
4 C program for round robin scheduling algorithm.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.