Skip to content

Instantly share code, notes, and snippets.

@AyatKhraisat
Created December 1, 2015 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AyatKhraisat/2b9d58787bee76695d69 to your computer and use it in GitHub Desktop.
Save AyatKhraisat/2b9d58787bee76695d69 to your computer and use it in GitHub Desktop.
#include<stdlib.h>
#include<stdio.h>
typedef struct process
{
int id,at,bt,st,ft,pr;
float wt,tat;
}process;
process p[10],p1[10],temp;
int accept();
void turnwait(int n);
void display(int n);
void ganttfcfs(int n);
int main()
{
int i,n,ts,ch,j,x;
p[0].tat=0;
p[0].wt=0;
n=accept();
ganttfcfs(n);
turnwait(n);
display(n);
return 0;
}
int accept() //functions for accepting inputs from the user
{
int i,n;
printf("\nEnter the no. of process: ");
scanf("%d",&n);
if(n==0)
{
printf("\nInvalid no. of process");
exit(1);
}
for(i=1;i<=n;i++)
{
printf("\nEnter the arrival time for process P%d: ",i);
scanf("%d",&p[i].at);
p[i].id=i;
}
for(i=1;i<=n;i++)
{
printf("\nEnter the burst time for process P%d: ",i);
scanf("%d",&p[i].bt);
}
printf("\nInputs given by the user are:\n");
printf("==============================================================");
printf("\nProcess\tAT\tBT");
for(i=1;i<=n;i++)
printf("\nP%d\t%d\t%d",p[i].id,p[i].at,p[i].bt);
printf("\n============================================================");
for(i=1;i<=n;i++) // copying of one array into another array(dummy area)
p1[i]=p[i];
return n;
}
/*FUNCTION FOR DISPLAYING GANTT CHART FOR FIRST COME FIRST SERVE SCHEDULING*/
void ganttfcfs(int n)
{
int i;
p[1].st=p[1].at;
for(i=2;i<=n;i++)
p[i].st=p[i-1].st+p[i-1].bt;
p[1].ft=p[1].bt;
for(i=2;i<=n;i++)
p[i].ft=p[i-1].ft+p[i].bt;
printf("\nGant Chart is as follows:\n\n");
printf("%d->P%d->%d",p[1].st,p[1].id,p[1].ft);
for(i=2;i<=n;i++)
printf("->P%d->%d",p[i].id,p[i].ft);
printf("\n");
}
/* FUNCTION FOR CALCULATING TURN AROUND TIME OR WAIT TIME */
void turnwait(int n)
{
int i;
for(i=1;i<=n;i++)
{
p[i].tat=p[i].ft-p[i].at;
p[i].wt=p[i].tat-p[i].bt;
p[0].tat=p[0].tat+p[i].tat;
p[0].wt=p[0].wt+p[i].wt;
}
p[0].tat=p[0].tat/n;
p[0].wt=p[0].wt/n;
}
/* FUNCTION FOR DISPLAYING THE TABLE */
void display(int n)
{
int i;
printf("\n\n-------------------TABLE----------------------------------\n");
printf("\nProcess\tAT\tBT\tFT\tTAT\t\tWT");
for(i=1;i<=n;i++)
printf("\nP%d\t%d\t%d\t%d\t%f\t%f",p[i].id,p[i].at,p[i].bt,p[i].ft,p[i].tat,p[i].wt);
printf("\n\n-----------------------------------------------------------");
printf("\nAverage Turn Around Time: %f",p[0].tat);
printf("\nAverage Waiting Time: %f",p[0].wt);
int x;
scanf("%d",&x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment