fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. struct Process {
  5. char name[10];
  6. int burst_time;
  7. int priority;
  8. int waiting_time;
  9. int turnaround_time;
  10. };
  11.  
  12. void sortProcesses(struct Process p[], int n) {
  13. struct Process temp;
  14. for (int i = 0; i < n-1; i++) {
  15. for (int j = i+1; j < n; j++) {
  16. if (p[i].priority > p[j].priority) { // Sorting based on Priority
  17. temp = p[i];
  18. p[i] = p[j];
  19. p[j] = temp;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. void calculateTimes(struct Process p[], int n) {
  26. p[0].waiting_time = 0;
  27. p[0].turnaround_time = p[0].burst_time;
  28.  
  29. for (int i = 1; i < n; i++) {
  30. p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time;
  31. p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
  32. }
  33. }
  34.  
  35. void display(struct Process p[], int n) {
  36. int total_waiting_time = 0, total_turnaround_time = 0;
  37.  
  38. printf("\nProcess Burst Time Priority Waiting Time Turnaround Time\n");
  39. for (int i = 0; i < n; i++) {
  40. printf("%s %d %d %d %d\n", p[i].name, p[i].burst_time, p[i].priority, p[i].waiting_time, p[i].turnaround_time);
  41. total_waiting_time += p[i].waiting_time;
  42. total_turnaround_time += p[i].turnaround_time;
  43. }
  44.  
  45. printf("\nTotal Waiting Time: %d", total_waiting_time);
  46. printf("\nAverage Waiting Time: %.2f", (float)total_waiting_time / n);
  47. printf("\nTotal Turnaround Time: %d", total_turnaround_time);
  48. printf("\nAverage Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
  49. }
  50.  
  51. int main() {
  52. int n;
  53. printf("Enter the number of processes: ");
  54. scanf("%d", &n);
  55.  
  56. struct Process p[n];
  57.  
  58. for (int i = 0; i < n; i++) {
  59. printf("\nEnter Process Name: ");
  60. scanf("%s", p[i].name);
  61. printf("Enter Burst Time: ");
  62. scanf("%d", &p[i].burst_time);
  63. printf("Enter Priority: ");
  64. scanf("%d", &p[i].priority);
  65. }
  66.  
  67. sortProcesses(p, n);
  68. calculateTimes(p, n);
  69. display(p, n);
  70.  
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0s 5268KB
stdin
Standard input is empty
stdout
Enter the number of processes: 
Process	Burst Time	Priority	Waiting Time	Turnaround Time

Total Waiting Time: 0
Average Waiting Time: -nan
Total Turnaround Time: 0
Average Turnaround Time: -nan