fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct Process
  4. {
  5. int pid;
  6. int bt;
  7. int priority;
  8. };
  9. bool comparison(Process a, Process b)
  10. {
  11. return (a.priority > b.priority);
  12. }
  13. void findWaitingTime(Process proc[], int n,
  14. int wt[])
  15. {
  16. wt[0] = 0;
  17. for (int i = 1; i < n ; i++ )
  18. wt[i] = proc[i-1].bt + wt[i-1] ;
  19. }
  20. void findTurnAroundTime( Process proc[], int n,
  21. int wt[], int tat[])
  22. {
  23. for (int i = 0; i < n ; i++)
  24. tat[i] = proc[i].bt + wt[i];
  25. }
  26. void findavgTime(Process proc[], int n)
  27. {
  28. int wt[n], tat[n], total_wt = 0, total_tat = 0;
  29. findWaitingTime(proc, n, wt);
  30. findTurnAroundTime(proc, n, wt, tat);
  31. for (int i=0; i<n; i++)
  32. {
  33. total_wt = total_wt + wt[i];
  34. total_tat = total_tat + tat[i];
  35. cout << "Processes " << proc[i].pid << "\t"
  36. << " Burst time "<< proc[i].bt << "\t " << " Waiting time "<< wt[i]
  37. << "\t " << " Turn around time"<< tat[i] <<endl;
  38. }
  39.  
  40. cout << "\nAverage waiting time = "
  41. << (float)total_wt / (float)n;
  42. cout << "\nAverage turn around time = "
  43. << (float)total_tat / (float)n;
  44. }
  45.  
  46. void priorityScheduling(Process proc[], int n)
  47. {
  48. sort(proc, proc + n, comparison);
  49. for (int i = 0 ; i < n; i++)
  50. cout << proc[i].pid <<" " ;
  51.  
  52. findavgTime(proc, n);
  53. }
  54.  
  55. int main()
  56. {
  57. Process proc[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};
  58. int n = sizeof proc / sizeof proc[0];
  59. priorityScheduling(proc, n);
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 4208KB
stdin
Standard input is empty
stdout
1 3 2 Processes 1	 Burst time  10	     Waiting time  0	   Turn around time10
Processes 3	 Burst time  8	     Waiting time  10	   Turn around time18
Processes 2	 Burst time  5	     Waiting time  18	   Turn around time23

Average waiting time = 9.33333
Average turn around time = 17