/* Objective: To implement FCFS Algorithm Author: Rahul kumar */ #include <stdio.h> #define M 100 int main() { int n,i,j,temp,AT[M],CT[M],ET[M],WT[M],TAT[M],PID[M]; double avg_TAT,avg_WT,throughput; printf("Enter the total No.of Process: "); scanf("%d",&n); if(n>M) { printf("Please Enter the Smaller No."); } else if(n<1) { printf("This No. of process isn't exist or Negative No."); } else { for(i=0;i< n;i++) { printf("Enter the detail of the process P%d\n",i+1); printf("Arrival Time: "); scanf("%d",&AT[i]); printf("Execution Time: "); scanf("%d",&ET[i]); PID[i]=i+1; } } //sorting for(i=0;i< n-1;i++) { for(j=0;j< n-i-1;j++) { if(AT[j]>AT[j+1]) { //swapping for arrival time temp=AT[j+1]; AT[j+1]=AT[j]; AT[j]=temp; //swapping for execution time temp=ET[j+1]; ET[j+1]=ET[j]; ET[j]=temp; //swapping for product id temp=PID[j+1]; PID[j+1]=PID[j]; PID[j]=temp; } } } printf("\nProcess\tArrivalTime\tExecutionTime\tCompletionTime\tTurnAroundTime\tWaitingTime\n"); CT[0]=AT[0]+ET[0]; TAT[0]=CT[0]-AT[0]; WT[0]=TAT[0]-ET[0]; printf("P%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",PID[0],AT[0],ET[0],CT[0],TAT[0],WT[0]); avg_TAT=TAT[0]; avg_WT=WT[0]; for(i=1;i< n;i++) { CT[i]=CT[i-1]+ET[i]; TAT[i]=CT[i]-AT[i]; WT[i]=TAT[i]-ET[i]; avg_TAT= avg_TAT+TAT[i]; avg_WT= avg_WT+WT[i]; printf("P%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",i+1,AT[i],ET[i],CT[i],TAT[i],WT[i]); } avg_TAT=(double)avg_TAT/n; avg_WT=(double)avg_WT/n; throughput=(double)n/(CT[n-1]-AT[0]); printf("\nAvg_TAT=%.2f",avg_TAT); printf("\nAvg_WT=%.2f",avg_WT); printf("\nThroughput=%.2f",throughput); return 1; }
Output:
Enter the total No.of Process: 5 Enter the detail of the process P1 Arrival Time: 3 Execution Time: 4 Enter the detail of the process P2 Arrival Time: 5 Execution Time: 3 Enter the detail of the process P3 Arrival Time: 4 Execution Time: 5 Enter the detail of the process P4 Arrival Time: 3 Execution Time: 2 Enter the detail of the process P5 Arrival Time: 1 Execution Time: 5 Process ArrivalTime ExecutionTime CompletionTime TurnAroundTime WaitingTime P5 1 5 6 5 0 P2 3 4 10 7 3 P3 3 2 12 9 7 P4 4 5 17 13 8 P5 5 3 20 15 12 Avg_TAT=9.80 Avg_WT=6.00 Throughput=0.26