Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- struct process_struct
- {
- int burst_time,org_bt;
- int waiting_time;
- int turnaroundtime;
- };
- void main()
- {
- int n,i,j,total_burst_time=0,shortest_job,total_WT=0,total_TAT=0;
- float avg_WT,avg_TAT;
- printf("\nSJF Scheduling Algorithm Simulation\n");
- printf("\n\nPlease Enter the number of Processes: ");
- scanf("%d",&n);
- struct process_struct *process=(struct process_struct*)malloc(sizeof(struct process_struct)*n);
- printf("\nPlease Enter the Burst Time (BT) for the processes....\n");
- for(i=0;i<n;i++)
- {
- printf("\nProcess %d BT: ",i);
- scanf("%d",&process[i].burst_time);
- process[i].org_bt=process[i].burst_time;
- total_burst_time+=process[i].burst_time;
- process[i].waiting_time=0;
- process[i].turnaroundtime=0;
- }
- shortest_job=0;
- for(i=0;i<total_burst_time;)
- {
- for(j=0;j<n;j++)
- {
- if(process[j].burst_time<process[shortest_job].burst_time&&process[j].burst_time!=0)
- shortest_job=j;
- }
- i=i+process[shortest_job].burst_time;
- for(j=0;j<n;j++)
- {
- if(j!=shortest_job&&process[j].burst_time!=0)
- process[j].waiting_time+=process[shortest_job].burst_time;
- }
- process[shortest_job].burst_time=0;
- for(j=0;j<n;j++)
- if(process[j].burst_time!=0)
- {
- shortest_job=j;
- break;
- }
- }
- for(i=0;i<n;i++)
- {
- process[i].turnaroundtime=process[i].org_bt+process[i].waiting_time;
- total_WT+=process[i].waiting_time;
- total_TAT+=process[i].turnaroundtime;
- }
- avg_TAT=total_TAT/(float)n;
- avg_WT=total_WT/(float)n;
- printf("\n\nProcess ID\tBurst Time\tWaiting Time\tTurn Around Time\n---------------------------------------------------------------\n");
- for(i=0;i<n;i++)
- {
- printf("\n%d\t\t%d\t\t%d\t\t%d",i,process[i].org_bt,process[i].waiting_time,process[i].turnaroundtime);
- }
- printf("\n\nThe Average Waiting Time = %.2f",avg_WT);
- printf("\n\nThe Average Turn Around Time = %.2f",avg_TAT);
- }
Add Comment
Please, Sign In to add comment