/*--------------------------------------------------------------------------------
Title: Write a program to implement Shortest Job First (Non-Preemptive) scheduling algorithm.
---------------------------------------------------------------------------------*/
#include<stdio.h>
#include<string.h>
int n;
struct process
{
int arr[30];
char prc[30];
int at,bt,pt;
// int flags,l,id;
}p[30];
struct tprocess
{
char prc[30];
int at,bt;
}tp1[30];
void main()
{
char ta[10];
int wt[10],tat[10];
int temp,temp1=0,flg=0,flag=0,cnt;
int i,t,j;
float awt,sum=0,x,awt1;
printf("\\n SJF (Non-Preempitve) \\n");
printf("\\n Enter Number of Process : ");
scanf("%d",&n);
flag=n;
for(i=0; i<n; i++)
{
printf("\\n Enter Process Name : ");
scanf("%s",p[i].prc);
printf("\\n Enter Arrival Time : ");
scanf("%d",&p[i].at);
printf("\\n Enter Burst Time : ");
scanf("%d",&p[i].bt);
}
// display data in table format
printf("\\n Process Name \\t Arrival Time \\t Burst Time \\n");
for(i=0; i<n; i++)
{
printf("\\n %s \\t\\t %d \\t\\t %d ",p[i].prc,p[i].at,p[i].bt);
}
// sorting logic based on BT
for(i=0; i<n; i++)
{
for(j=i; j<n; j++)
{
if(p[i].bt>p[j].bt)
{
temp = p[i].bt;
t = p[i].at;
strcpy(ta,p[i].prc);
p[i].bt = p[j].bt;
p[i].at = p[j].at;
strcpy(p[i].prc,p[j].prc);
p[j].bt = temp;
p[j].at = t;
strcpy(p[j].prc,ta);
}
}
}
// print the sorted table
printf("\\n\\n\\t\\t Sorted Sructure \\n");
printf("\\n Process Name \\t Arrival Time \\t Burst Time \\n");
for(i=0; i<n; i++)
{
printf("\\n %s \\t\\t %d \\t\\t %d ",p[i].prc,p[i].at,p[i].bt);
}
printf("\\n\\n Gaunt Chart : ");
// set WT and TAT as \'zero\' , its compulsory
for(i=0; i<n; i++)
{
wt[i] = 0;
tat[i]=0;
}
t=0;
cnt=0;
printf("0");
again: if(p[flg].at<=temp1)
{
wt[cnt] = temp1 - p[flg].at;
cnt++;
temp1 = temp1 + p[flg].bt;
tat[t] = temp1 - p[flg].at;
t++;
printf(" %s-->%d ",p[flg].prc,temp1);
goto delfrmstruct;
}
else
{
flg++;
goto again;
}
delfrmstruct: for(i=flg; i<flag; i++)
{
p[i] = p[i+1];
}
flag--;
if(flag==0)
{
awt1=0;
x=temp1;
awt1= n/x; // calculate throughput : dividing total no. of process by last execution time
goto ext; // goto exit and then calculation of Avg WT & Avg. TAT and throughput
}
else
{
flg=0;
goto again; // else goto label \'again\' for next calculation
}
ext: sum = 0;
for(i=0; i<n; i++)
{
sum = sum + wt[i];
}
awt = 0 ;
awt = sum/n;
printf("\\n\\n Wating Time : %f ",sum);
printf("\\n Average Waiting Time : %f",awt);
sum = awt = 0 ;
for(i=0; i<n; i++)
{
sum=sum + tat[i];
}
awt=sum/n;
printf("\\n\\n Turn around Time : %f ",sum);
printf("\\n Average Turn around Time : %f",awt);
printf("\\n\\n Throughput : %f ",awt1);
}
//end of the program
/*
//------------------
// OUTPUT
//------------------
gescoe@gescoe-Vostro-230:~/Desktop/TE$ gcc a6_SJF_NonP.c
gescoe@gescoe-Vostro-230:~/Desktop/TE$ ./a.out
SJF (Non-Preempitve)
Enter Number of Process : 4
Enter Process Name : p1
Enter Arrival Time : 0
Enter Burst Time : 8
Enter Process Name : p2
Enter Arrival Time : 0
Enter Burst Time : 5
Enter Process Name : p3
Enter Arrival Time : 1
Enter Burst Time : 2
Enter Process Name : p4
Enter Arrival Time : 2
Enter Burst Time : 7
Process Name Arrival Time Burst Time
p1 0 8
p2 0 5
p3 1 2
p4 2 7
Sorted Structure
Process Name Arrival Time Burst Time
p3 1 2
p2 0 5
p4 2 7
p1 0 8
Gaunt Chart : 0 p2-->5 p3-->7 p4-->14 p1-->22
Wating Time : 23.000000
Average Waiting Time : 5.750000
Turn around Time : 45.000000
Average Turn around Time : 11.250000
Throughput : 0.181818
// EXIT
*/