Advertisement
Sri27119

non_preemptive

Nov 25th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct Process {
  4. int id;
  5. int burstTime;
  6. int priority;
  7. int arrivalTime;
  8. int waitingTime;
  9. int turnAroundTime;
  10. int completionTime;
  11. int isCompleted;
  12. };
  13.  
  14. int findNextProcess(struct Process proc[], int n, int currentTime) {
  15. int highestPriorityIdx = -1;
  16. for (int i = 0; i < n; i++) {
  17. if (proc[i].arrivalTime <= currentTime && proc[i].isCompleted == 0) {
  18. if (highestPriorityIdx == -1 || proc[i].priority > proc[highestPriorityIdx].priority) {
  19. highestPriorityIdx = i;
  20. }
  21. }
  22. }
  23. return highestPriorityIdx;
  24. }
  25.  
  26. void calculateCompletionTime(struct Process proc[], int n) {
  27. int currentTime = 0;
  28. int completedProcesses = 0;
  29.  
  30. while (completedProcesses < n) {
  31. int idx = findNextProcess(proc, n, currentTime);
  32.  
  33. if (idx == -1) {
  34. currentTime++;
  35. continue;
  36. }
  37.  
  38. currentTime += proc[idx].burstTime;
  39. proc[idx].completionTime = currentTime;
  40. proc[idx].isCompleted = 1;
  41. completedProcesses++;
  42. }
  43. }
  44.  
  45. void calculateTurnAroundTime(struct Process proc[], int n) {
  46. for (int i = 0; i < n; i++) {
  47. proc[i].turnAroundTime = proc[i].completionTime - proc[i].arrivalTime;
  48. }
  49. }
  50.  
  51. void calculateWaitingTime(struct Process proc[], int n) {
  52. for (int i = 0; i < n; i++) {
  53. proc[i].waitingTime = proc[i].turnAroundTime - proc[i].burstTime;
  54. }
  55. }
  56.  
  57. void displayTable(struct Process proc[], int n) {
  58. printf("\nProcess ID\tArrival Time\tPriority\tBurst Time\tCompletionTime\tTurnaroundTime\tWaitingTime\n");
  59. printf("---------------------------------------------------------------------------------------------------------\n");
  60. for (int i = 0; i < n; i++) {
  61. printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
  62. proc[i].id, proc[i].arrivalTime, proc[i].priority, proc[i].burstTime,
  63. proc[i].completionTime, proc[i].turnAroundTime, proc[i].waitingTime);
  64. }
  65. }
  66.  
  67. void displayavgTime(struct Process proc[], int n) {
  68. int totalWaitingTime = 0;
  69. int totalTurnAroundTime = 0;
  70.  
  71. for (int i = 0; i < n; i++) {
  72. totalWaitingTime += proc[i].waitingTime;
  73. totalTurnAroundTime += proc[i].turnAroundTime;
  74. }
  75.  
  76. printf("\nAverage Waiting Time: %.2f\n", (float)totalWaitingTime / n);
  77. printf("Average Turnaround Time: %.2f\n", (float)totalTurnAroundTime / n);
  78. }
  79.  
  80. int main() {
  81. int n;
  82.  
  83. printf("Enter the number of processes: ");
  84. scanf("%d", &n);
  85.  
  86. struct Process proc[n];
  87.  
  88. for (int i = 0; i < n; i++) {
  89. proc[i].id = i + 1;
  90. printf("Enter arrival time, burst time and priority for Process %d: ", proc[i].id);
  91. scanf("%d", &proc[i].arrivalTime);
  92. scanf("%d", &proc[i].burstTime);
  93. scanf("%d", &proc[i].priority);
  94. proc[i].isCompleted = 0;
  95. }
  96.  
  97. calculateCompletionTime(proc, n);
  98. calculateTurnAroundTime(proc, n);
  99. calculateWaitingTime(proc, n);
  100.  
  101. displayTable(proc, n);
  102.  
  103. displayavgTime(proc, n);
  104.  
  105. return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement