Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main()
- {
- int bt[20],p[20],wt[20],tatime[20],pr[20],i,j,n,m,total,avg_wt,avg_time,temp;
- printf("Enter Total Number of Process:");
- scanf("%d",&n);
- printf("\nEnter Burst Time and Priority\n");
- for(i=0;i<n;i++)
- {
- printf("\nP[%d]\n",i+1);
- printf("Burst Time:");
- scanf("%d",&bt[i]);
- printf("Priority:");
- scanf("%d",&pr[i]);
- p[i]=i+1; //contains process number
- }
- //sorting burst time, priority and process number
- for (i=0; i<n; i++)
- {
- for(m=i+1; m<n; m++)
- {
- if(pr[i]>pr[m])
- {
- (temp=pr[i]), (pr[i] = pr[m]), (pr[m] = temp);
- (temp=bt[i]), (bt[i] = bt[m]), (bt[m] = temp);
- (temp= p[i]), (p[i] = p[m]), (p[m] = temp);
- }
- if (pr[i]==pr[m])
- {
- if(bt[i]>bt[m])
- {
- (temp=bt[i]), (bt[i] = bt[m]), (bt[m] = temp);
- (temp=pr[i]), (pr[i] = pr[m]), (pr[m] = temp);
- (temp= p[i]), (p[i] = p[m]), (p[m] = temp);
- }
- if(bt[i]==bt[m])
- {
- if (p[i]>p[m])
- {
- (temp= p[i]), (p[i] = p[m]), (p[m] = temp);
- (temp=bt[i]), (bt[i] = bt[m]), (bt[m] = temp);
- (temp=pr[i]), (pr[i] = pr[m]), (pr[m] = temp);
- }
- }
- }
- }
- }
- wt[0]=0; //waiting time for first process is zero
- //calculate waiting time
- for(i=1;i<n;i++)
- {
- wt[i]=0;
- for(j=0;j<i;j++)
- wt[i]+=bt[j];
- total+=wt[i];
- }
- avg_wt=total/n; //average waiting time
- total=0;
- printf("\nProcess\t \t Priority Burst Time \tWaiting Time\tTurnaround Time");
- for(i=0;i<n;i++)
- {
- tatime[i]=bt[i]+wt[i]; //calculate turnaround time
- total+=tatime[i];
- printf("\nP[%d]\t\t %d\t\t %d\t\t %d\t\t\t%d",p[i],pr[i],bt[i],wt[i],tatime[i]);
- }
- avg_time=total/n; //average turnaround time
- printf("\n\nAverage Waiting Time=%d",avg_wt);
- printf("\nAverage Turnaround Time=%d\n",avg_time);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment