Important alert: (current site time 10/24/2014 8:15:08 PM EDT)
 

VB icon

Shortest Job First (Non Preemptive) Scheduling

Email
Submitted on: 12/7/2007 8:49:01 AM
By: Sathya Priya 
Level: Intermediate
User Rating: Unrated
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++
Views: 67656
 
     This is the coding for Shortest Job First Scheduling Algorithm. It gets the number of processes and the process name,burst time and arrival time from the user. It then calculates the average waiting and average turn around time and displays the gantt chart.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Shortest Job First (Non Preemptive) Scheduling
// Description:This is the coding for Shortest Job First Scheduling Algorithm. It gets the number of processes and the process name,burst time and arrival time from the user. It then calculates the average waiting and average turn around time and displays the gantt chart.
// By: Sathya Priya
//
// Inputs:Number of process, and process name,burst time and arrival time for each process name.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=11825&lngWId=3//for details.//**************************************

// 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();
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.