Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. @@ -31,7 +31,7 @@ main() {
  2. }
  3.  
  4. void read_processes(process list[], int* q, int* numOfProcesses) {
  5. FILE *in = fopen("jobs.txt", "r");
  6. FILE *in = fopen("jobs_s18_team3.txt", "r");
  7. char ch;
  8.  
  9. while(fscanf(in, "%c", &ch)) {
  10. @ -52,6 +52,7 @@ Programmer: Johanna Lim, 11726008
  11. This function takes in the list of processes and writes the simulated execution order as well as the AWT in a file.
  12. */
  13. void print_fcfs(process list[], int numOfProcesses) {
  14. FILE *out = fopen("jobs_s18_team3.txt", "a");
  15. int i, j, indexCtr;
  16. int timeFinished[MAX_NUM_OF_PROCESSES];
  17. int waiting[MAX_NUM_OF_PROCESSES];
  18. @ -61,7 +62,7 @@ void print_fcfs(process list[], int numOfProcesses) {
  19. for (i = 0; i < numOfProcesses; i++) {
  20. processes[i] = list[i];
  21. }
  22. fprintf(stdout, "*FCFS*\n");
  23. fprintf(out, "\n*FCFS*\n");
  24. // Sort the processes according to time of arrival
  25. for (i = 0; i < numOfProcesses-1; i++){
  26. for (j = 0; j < numOfProcesses-i-1; j++){
  27. @ -75,7 +76,7 @@ void print_fcfs(process list[], int numOfProcesses) {
  28. // Write the simulated execution order to the file
  29. for (i = 0; i < numOfProcesses; i++){
  30. for (j = 0; j < processes[i].remaining ; j++){
  31. fprintf(stdout, "%c", processes[i].p_id);
  32. fprintf(out, "%c", processes[i].p_id);
  33. }
  34. }
  35. // Compute for the AWT
  36. @ -85,10 +86,12 @@ void print_fcfs(process list[], int numOfProcesses) {
  37. timeFinished[i] = timeFinished[i-1] + processes[i].remaining;
  38. waiting[i] = timeFinished[i-1] - processes[i].arrival;
  39. }
  40. fprintf(stdout, "\nAWT = %.2f\n\n", compute_avg(waiting, numOfProcesses));
  41. fprintf(out, "\nAWT = %.2f\n\n", compute_avg(waiting, numOfProcesses));
  42. fclose(out);
  43. }
  44.  
  45. void print_sjf(process list[], int numOfProcesses) {
  46. FILE *out = fopen("jobs_s18_team3.txt", "a");
  47. int time = 0;
  48. int i, j;
  49. int waiting[MAX_NUM_OF_PROCESSES] = {0};
  50. @ -97,7 +100,7 @@ void print_sjf(process list[], int numOfProcesses) {
  51. for (i = 0; i < numOfProcesses; i++) {
  52. processes[i] = list[i];
  53. }
  54. fprintf(stdout, "*SJF*\n");
  55. fprintf(out, "*SJF*\n");
  56. while (1) {
  57. char nextIndex = -1;
  58. int shortest = 1e9;
  59. @ -114,16 +117,17 @@ void print_sjf(process list[], int numOfProcesses) {
  60. if (finished) break;
  61. processes[nextIndex].remaining = 0;
  62. for (i = 0; i < shortest; i++) {
  63. fprintf(stdout, "%c", processes[nextIndex].p_id);
  64. fprintf(out, "%c", processes[nextIndex].p_id);
  65. }
  66. waiting[nextIndex] += time - processes[nextIndex].arrival;
  67. time += shortest;
  68. }
  69. fprintf(stdout, "\nAWT = %.2f\n\n", compute_avg(waiting, numOfProcesses));
  70.  
  71. fprintf(out, "\nAWT = %.2f\n\n", compute_avg(waiting, numOfProcesses));
  72. fclose(out);
  73. }
  74.  
  75. void print_srtf(process list[], int numOfProcesses) {
  76. FILE *out = fopen("jobs_s18_team3.txt", "a");
  77. int time = 0;
  78. int i, j;
  79. process processes[MAX_NUM_OF_PROCESSES];
  80. @ -131,7 +135,7 @@ void print_srtf(process list[], int numOfProcesses) {
  81. processes[i] = list[i];
  82. }
  83. int waiting[MAX_NUM_OF_PROCESSES] = {0};
  84. fprintf(stdout, "*SRTF*\n");
  85. fprintf(out, "*SRTF*\n");
  86. while (1) {
  87. int nextIndex = -1;
  88. int shortest = 1e9;
  89. @ -147,13 +151,14 @@ void print_srtf(process list[], int numOfProcesses) {
  90. }
  91.  
  92. if (finished) break;
  93. fprintf(stdout, "%c", processes[nextIndex].p_id);
  94. fprintf(out, "%c", processes[nextIndex].p_id);
  95. processes[nextIndex].remaining--;
  96. waiting[nextIndex] += time - processes[nextIndex].arrival;
  97. time++;
  98. processes[nextIndex].arrival = time;
  99. }
  100. fprintf(stdout, "\nAWT = %.2f\n\n", compute_avg(waiting, numOfProcesses));
  101. fprintf(out, "\nAWT = %.2f\n\n", compute_avg(waiting, numOfProcesses));
  102. fclose(out);
  103. }
  104.  
  105. void print_rr(process list[], int q, int numOfProcesses) {
  106. @ -183,7 +188,7 @@ void print_rr(process list[], int q, int numOfProcesses) {
  107. }
  108. }
  109. }
  110. fprintf(stdout, "*RR*\n");
  111. fprintf(out, "*RR*\n");
  112. i=0;
  113. holder = i;
  114. counter = q;
  115. @ -199,7 +204,7 @@ void print_rr(process list[], int q, int numOfProcesses) {
  116.  
  117. if(dispatched){
  118. if(retain == false){
  119. //fprintf(stdout, "%d-%d, ", time, processes[i].arrival); // prints the avg waiting calculations
  120. //fprintf(out, "%d-%d, ", time, processes[i].arrival); // prints the avg waiting calculations
  121. waiting[i] += time - processes[i].arrival;
  122. }
  123. }
  124. @ -208,7 +213,7 @@ void print_rr(process list[], int q, int numOfProcesses) {
  125. processes[i].remaining--;
  126. time++;
  127. counter--;
  128. fprintf(stdout, "%c", processes[i].p_id); // prints the dispatched process
  129. fprintf(out, "%c", processes[i].p_id); // prints the dispatched process
  130. //the process has ended or q is finished
  131. if(counter == 0 || processes[i].remaining == 0){
  132. holder = i;//keeps track of previous process
  133. @ -248,7 +253,8 @@ void print_rr(process list[], int q, int numOfProcesses) {
  134. }
  135.  
  136. }
  137. fprintf(stdout, "\nAWT = %.2f\n\n", compute_avg(waiting, numOfProcesses));
  138. fprintf(out, "\nAWT = %.2f\n\n", compute_avg(waiting, numOfProcesses));
  139. fclose(out);
  140. }
  141.  
  142. double compute_avg(int arr[], int n) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement