#include<stdio.h>
#include<conio.h>
typedef struct process{
char pname[20];
int at,bt,wt,ta,end;
}processes;
int main(){
processes P[10];
processes z[10];
processes et[10];
processes wt1[10];
processes ta1[10];
int a,b,n,x=0,st,y,w;
float aveta,avewt,sumta=0,sumwt=0;
printf("Process Scheduling Algorithm:\n");
printf("SHORTEST JOB FIRST\n");
printf("--------------------------------------\n");
printf("Enter No. of Process: ");
scanf("%d",&n);
printf("\n");
for(a=0;a<n;a++){
printf("Enter Process Name P[%d]: ",a+1);
scanf("%s",&P[a].pname);
}
printf("\n");
for(a=0;a<n;a++){
printf("Enter Arrival Time for P[%d]: ",a+1);
scanf("%d",&P[a].at);
}
for(a=0; a<n; a++)
{
if(st > P[a].at)
{
st = P[a].at;
}
}
printf("\n");
for(a=0;a<n;a++){
printf("Enter Burst Time for P[%d]: ",a+1);
scanf("%d",&P[a].bt);
}
printf("\n\n ========================================================================\n");
printf("||\tPROCESS \t||\t ARRIVAL TIME \t||\t BURST TIME \t||\n");
printf("||----------------------------------------------------------------------||\n");
for(a=0;a<n;a++){
printf("||\t P[%s] \t\t||\t %d \t\t||\t %d \t\t||\n",P[a].pname,P[a].at,P[a].bt);
}
printf(" ========================================================================\n");
for(a=0;a<n;a++){
z[a]=P[a];
et[a]=P[a];
}
processes t;
for(a=1;a<n;a++)
for(b=0;b<n-a;b++){
if(z[b].at > z[b+1].at){
t = z[b];
z[b] = z[b+1];
z[b+1] = t;
}
}
for(a=2;a<n;a++)
for(b=1;b<n-a+1;b++){
if(z[b].bt > z[b+1].bt){
t = z[b];
z[b] = z[b+1];
z[b+1] = t;
}
}
for(a=0;a<n;a++){
x+=z[a].bt;
}
printf("\n GANTT CHART: \n");
if(st>0){
for(a=0;a<n;a++){
if(st!=0){
x=st+z[a].bt;
printf("0\tIDLE\t%d\tP[%s]\t%d\t",st,z[a].pname,x);
st=0;
y=x;
et[a].end=x;
ta1[a].ta=et[a].end-z[a].at;
wt1[a].wt=ta1[a].ta-z[a].bt;
}
else{
y=y+z[a].bt;
w=w+z[a].bt;
w=y;
printf("P[%s]\t%d\t",z[a].pname,y);
y=w;
et[a].end=y;
ta1[a].ta=et[a].end-z[a].at;
wt1[a].wt=ta1[a].ta-z[a].bt;
}
}
}
else{
printf("0\t");
for(a=0;a<n;a++){
y=y+z[a].bt;
w=w+z[a].bt;
w=y;
printf("P[%s]\t%d\t",z[a].pname,y);
y=w;
et[a].end=y;
ta1[a].ta=et[a].end-z[a].at;
wt1[a].wt=ta1[a].ta-z[a].bt;
}
}
printf("\n\n ========================================================================\n");
printf("|| PROCESS \t|| END TIME \t|| TURN AROUND T.\t|| WAITING TIME\t||\n");
printf("||----------------------------------------------------------------------||\n");
for(a=0;a<n;a++){
printf("|| P[%s] \t|| %d \t|| %d \t\t|| %d \t\t|| \n",z[a].pname,et[a].end,ta1[a].ta,wt1[a].wt);
sumta+=ta1[a].ta;
sumwt+=wt1[a].wt;
}
printf(" =========================================================================\n");
aveta=sumta/n;
avewt=sumwt/n;
printf("\n Average Turn Around Time: %.2f",aveta);
printf("\n Average Waiting Time: %.2f",avewt);
getch();
return 0;
}
allen_1 0 Newbie Poster
Reverend Jim 4,968 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
JasonHippy 739 Practically a Master Poster
allen_1 0 Newbie Poster
Sahil Singh 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.