Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. //
  2. // FCFS.c
  3. // First Come First Serve Scheduling Algorithm
  4. //
  5. // Created by Anthony Andrada on 9/27/16.
  6. //
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12.  
  13. int size = 50;
  14. typedef int bool;
  15. #define true 1
  16. #define false 0
  17.  
  18. struct Process
  19. {
  20. int arrival_time;
  21. int service_time;
  22. int priority;
  23. };
  24.  
  25. int compare(const void * e1, const void * e2)
  26. {
  27. struct Process *x = (struct Process *)e1;
  28. struct Process *y = (struct Process *)e2;
  29. if (x->arrival_time < y->arrival_time){
  30. return -1;
  31. }
  32. else if (x->arrival_time > y->arrival_time){
  33. return 1;
  34. }
  35. else return 0;
  36. }
  37.  
  38. void sort (struct Process x[])
  39. {
  40. qsort (x, size, sizeof(struct Process), compare);
  41. }
  42.  
  43. void print (struct Process n[])
  44. {
  45. for (int i = 0; i < size; i++)
  46. {
  47. int x = n[i].arrival_time;
  48. int y = n[i].service_time;
  49. int z = n[i].priority;
  50. printf("Job: %d, AT: %d, ST: %d, P: %d\r\n",i, x, y, z);
  51. }
  52. }
  53.  
  54. void schedule(struct Process x[])
  55. {
  56. struct Process* ptr = x;
  57. int job = 0;
  58. int start = 0;
  59. int finish = 0;
  60. bool isScheduled = false;
  61. float turnaround = 0;
  62. float wait = 0;
  63. float response = 0;
  64. for (int q = 0; q < 110; q++)
  65. {
  66. if (isScheduled == false && q > 99) break;
  67. printf("Quantum: %d. ", q);
  68. if (isScheduled == true && q == (start + ptr->service_time))
  69. {
  70. turnaround += (q - ptr->arrival_time);
  71. ptr++;
  72. printf("Finished Job %d. ", job);
  73. job++;
  74. isScheduled = false;
  75. if (q >= 99)
  76. {
  77. printf("\r\n");
  78. break;
  79. }
  80. finish = q;
  81. }
  82. else if (isScheduled == true)
  83. {
  84. printf("Working Job %d.", job);
  85. }
  86. if (isScheduled == false && q >= ptr->arrival_time)
  87. {
  88. response += (q - finish);
  89. wait += (q - ptr->arrival_time);
  90. printf("Starting Job %d. ", job);
  91. isScheduled = true;
  92. start = q;
  93. }
  94. printf("\r\n");
  95. }
  96. printf("\r\n");
  97. printf("Average turnaround time: %.2f", turnaround / job);
  98. printf("\r\n");
  99. printf("Average wait time: %.2f", wait / job);
  100. printf("\r\n");
  101. printf("Average response time: %.2f", response / job);
  102. printf("\r\n");
  103. printf("Thoroughput : %f", (float)job / 100);
  104. printf("\r\n");
  105. }
  106.  
  107. int main()
  108. {
  109. int seed = time(NULL);
  110. srand(seed);
  111. struct Process queue[size];
  112. int t;
  113. for (int i = 0; i < size; i++)
  114. {
  115. queue[i].arrival_time = rand() % 100;
  116. t = rand() % 11;
  117. if (t == 0) t +=1;
  118. queue[i].service_time = t;
  119. t = rand() % 5;
  120. if (t == 0) t += 1;
  121. queue[i].priority = t;
  122. }
  123. sort(queue);
  124. printf("\r\n");
  125. print(queue);
  126. printf("\r\n");
  127. schedule(queue);
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement