Important alert: (current site time 9/2/2014 4:56:01 PM EDT)
 

VB icon

FCFS Scheduling Algorithm

Email
Submitted on: 12/7/2007 8:48:19 AM
By: Sathya Priya 
Level: Intermediate
User Rating: Unrated
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++
Views: 48404
 
     This code implements the First come First Served Scheduling Algorithm. It gets the number of processes and the process name, burst time and arrival time for each user from the user. It then sorts the processes depending upon the arrival time. Calculates and displays the average turn around time and waiting time.Then it will display 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: FCFS Scheduling Algorithm
// Description:This code implements the First come First Served Scheduling Algorithm. It gets the number of processes and the process name, burst time and arrival time for each user from the user. It then sorts the processes depending upon the arrival time. Calculates and displays the average turn around time and waiting time.Then it will display the Gantt chart.
// By: Sathya Priya
//
// Inputs:1. Number of processes
2. Process name
3. Burst Time
4. Arrival Time
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=11821&lngWId=3//for details.//**************************************

/* FIRST COME FIRST SERVED SCHEDULING ALGORITHM*/
//PREPROCESSOR DIRECTIVES
#include<stdio.h>
#include<conio.h>
#include<string.h>
//GLOBAL VARIABLES - DECLARATION
int n,Bu[20],Twt,Ttt,A[10],Wt[10],w;
float Awt,Att;
char pname[20][20],c[20][20];
//FUNCTION DECLARATIONS
void Getdata();
void Gantt_chart();
void Calculate();
void fcfs();
//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\n\t\t\tGANTT CHART\n");
	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");
 }
//CALCULATING AVERAGE WAITING TIME AND AVERAGE TURN AROUND TIME
void Calculate()
{
	int i;
	//For the 1st process
	Wt[1]=0;
	for(i=2;i<=n;i++)
	{
		Wt[i]=Bu[i-1]+Wt[i-1];
	}
	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);
}
//FCFS Algorithm
void fcfs()
{
	int i,j,temp, temp1;
	Twt=0;
	Ttt=0;
	Printf(\n\n FIRST COME FIRST SERVED ALGORITHM\n\n);
	for(i=1;i<=n;i++)
	{
		for(j=i+1;j<=n;j++)
		{
			if(A[i]>A[j])
			{
				temp=Bu[i];
				temp1=A[i];
				Bu[i]=Bu[j];
				A[i]=A[j];
				Bu[j]=temp;
				A[j]=temp1;
				strcpy(c[i],pname[i]);
				strcpy(pname[i],pname[j]);
				strcpy(pname[j],c[i]);
			}
		}
	}
	Calculate();
	Gantt_chart();
}
void main()
{
	int ch;
	clrscr();
	Getdata();
	fcfs();
	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.