Advertisement
Sri27119

preemptive

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