C Program for Shortest Job First Scheduling Algorithm

C Program for Shortest Job First Scheduling Algorithm

3521
14
SHARE

Hey friends, today we gonna discuss how to write a C program for Shortest job first scheduling algorithm or Non Preemptive . The code you gonna see below takes number of processes and the process name,burst time and arrival time as input from  the user and then calculates the and then displays the Gantt chart for the same.

C Program for Shortest Job First Scheduling Algorithm

 

C Program for Shortest Job First Scheduling Algorithm :

// PROGRAM FOR SJF - NON PREEMPTIVE SCHEDULING ALGORITHM
//PREPROCESSOR DIRECTIVES
#include<stdio.h>
#include<conio.h>
#include<string.h>
//GLOBAL VARIABLES - DECLARATION
int Twt,Ttt,A[20],Wt[20],n,Bu[20];
float Att,Awt;
char pname[20][20];
//FUNCTION DECLARATIONS
void Getdata();
void Gantt_chart();
void Sjf();
//GETTING THE NUMBER OF PROCESSES AND THE BURST TIME AND ARRIVAL TIME FOR EACH PROCESS
void Getdata()
{
	int i;
	printf("\n Enter the number of processes: ");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		fflush(stdin);
		printf("\n\n Enter the process name: ");
		scanf("%s",&pname[i]);
		printf("\n Enter The BurstTime for Process %s =	",pname[i]);
		scanf("%d",&Bu[i]);
		printf("\n Enter the Arrival Time for Process %s =	",pname[i]);
		scanf("%d",&A[i]);
	}
}
//DISPLAYING THE GANTT CHART
void Gantt_chart()
{
	int i;
	printf("\n\nGANTT CHART");
	printf("\n--------------------------------------------------------------------\n");
	for(i=1;i<=n;i++)
		printf("|\t%s\t",pname[i]);
	printf("|\t\n");
	printf("\n-----------------------------------------------------------\n");
	printf("\n");
	for(i=1;i<=n;i++)
		printf("%d\t\t",Wt[i]);
	printf("%d",Wt[n]+Bu[n]);
	printf("\n--------------------------------------------------------------------\n");
	printf("\n");
 }
//Shortest job First Algorithm with NonPreemption
void Sjf()
{
	int w,t,i,B[10],Tt=0,temp,j;
	char S[10],c[20][20];
	int temp1;
	printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM \n\n");
	Twt=Ttt=0;
	w=0;
	for(i=1;i<=n;i++)
	{
		B[i]=Bu[i];
		S[i]='T';
		Tt=Tt+B[i];
	}
	for(i=1;i<=n;i++)
	{
		for(j=3;j<=n;j++)
		{
			if(B[j-1]>B[j])
			{
				temp=B[j-1];
				temp1=A[j-1];
				B[j-1]=B[j];
				A[j-1]=A[j];
				B[j]=temp;
				A[j]=temp1;
				strcpy(c[j-1],pname[j-1]);
				strcpy(pname[j-1],pname[j]);
				strcpy(pname[j],c[j-1]);
			}
		}
	}
	//For the 1st process
	Wt[1]=0;
	w=w+B[1];
	t=w;
	S[1]='F';
	while(w<Tt)
	{
		i=2;
		while(i<=n)
		{
			if(S[i]=='T'&&A[i]<=t)
			{
				Wt[i]=w;
				S[i]='F';
				w=w+B[i];
				t=w;
				i=2;
			}
			else
				i++;
		}
	}
	//CALCULATING AVERAGE WAITING TIME AND AVERAGE TURN AROUND TIME
	for(i=1;i<=n;i++)
	{
		Twt=Twt+(Wt[i]-A[i]);
		Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
	}
	Att=(float)Ttt/n;
	Awt=(float)Twt/n;
	printf("\n\n Average Turn around time=%3.2f ms ",Att);
	printf("\n\n AverageWaiting Time=%3.2f ms",Awt);
	Gantt_chart();
}
void main()
{
	clrscr();
	Getdata();
	Sjf();
	getch();
}

 

We hope you all have enjoyed the detailed code. If you face any issues with the code ask us in the form of comments.

 

14 COMMENTS

  1. hi my name is anna I already saw ur code and u are great. can u help me for non – preemptive and preemptive code in c++ in details . thank u

  2. But the output is incorrect. Example I have 5 processes from P1 to P5 with the ff details:

    P AT BT
    1 2 5
    2 3 3
    3 4 2
    4 7 6
    5 9 8

    Supposed to be the total TAT must be 10 for NSJF and ave. WT must be = 5

  3. Its again the wrong program giving wrong output

    Checked for the below values
    Input
    4
    1
    5
    1
    2
    3
    2
    3
    4
    1
    4
    2
    3

    Output:

    Average Turn around time=7.25 ms

    AverageWaiting Time=3.75 ms

    GANTT CHART
    ——————————————————————–
    | 1 | 4 | 2 | 3 |

    ———————————————————–

    0 5 7 10 12

  4. Try this code-
    #include
    #include
    using namespace std;

    void display(int ar_time[],int bt_time[],int num,int wait[],int turn[],double avg_wait,double avg_turn);
    void ganttChart(int arrB[],int arrA[],int num)
    {
    int sum=0,wait[100],ta_time[100];
    double avg_wait=0,avg_turn=0;
    sum=arrA[0];
    for(int i=0;i<num;i++)
    {
    wait[i]=sum-arrA[i];
    avg_wait+=wait[i];
    sum+=arrB[i];
    ta_time[i]=sum-arrA[i];
    avg_turn+=ta_time[i];
    }
    display(arrA,arrB,num,wait,ta_time,avg_wait/num,avg_turn/num);

    }

    int enterValue(int ar_time[],int bt_time[])
    {
    int num;
    cout<>num;
    for(int i=0;i<num;i++)
    {
    cout<<"Enter the arrival time for process "<<i+1<>ar_time[i];
    cout<<"Enter the burst time for process "<<i+1<>bt_time[i];
    }
    return num;
    }

    void swapValue(int ar_time[],int bt_time[],int num)
    {
    int sum=0,a,i,j,k,b,temp;
    sum=ar_time[0];
    for(i=0;i<num;i++)
    {
    a=i;
    for(j=i;j=ar_time[j])
    {
    a=j;
    continue;
    }
    }
    for(k=i;k<num;k++)
    {
    for(b=i;bbt_time[b+1])
    {
    temp=bt_time[b];
    bt_time[b]=bt_time[b+1];
    bt_time[b+1]=temp;
    temp=ar_time[b];
    ar_time[b]=ar_time[b+1];
    ar_time[b+1]=temp;
    }
    }
    }
    sum+=bt_time[i];
    }
    ganttChart(bt_time,ar_time,num);
    }
    void display(int ar_time[],int bt_time[],int num,int wait[],int turn[],double avg_wait,double avg_turn)
    {
    cout<<"Arrival Time\tBurst Time\tWaiting Time\tTurn Around Time\n";
    for(int i=0;i<num;i++)
    {
    cout<<setw(11)<<ar_time[i]<<setw(13)<<bt_time[i]<<setw(15)<<wait[i]<<setw(29)<<turn[i]<<endl;
    }
    }
    int main()
    {
    int num,temp,ar_time[100],bt_time[100],wt_time[100],ta_time[100];
    num=enterValue(ar_time,bt_time);
    swapValue(ar_time,bt_time,num);
    }

LEAVE A REPLY