当前位置:   article > 正文

高响应比作业调度

高响应比作业调度程序流程图
  1. #include <malloc.h>
  2. #include <cstdio>
  3. #include <cstring>
  4. #define N 10
  5. typedef struct table
  6. {
  7. char name[8]; // 作业名
  8. float in_well; // 进入输入井的时间
  9. float begin_run; // 开始运行时间
  10. float run_time; // 运行时间
  11. float end_run; // 结束运行时间
  12. float turnover_time;// 周转时间
  13. }jobtable;
  14. void init(jobtable job[], int n)
  15. {
  16. int i, j;
  17. printf("input %d job information\n", n);
  18. printf("in_well run_time name\n");
  19. for(i = 0; i < n; ++ i)
  20. {
  21. scanf("%f %f %s", &job[i].in_well, &job[i].run_time, job[i].name);
  22. job[i].begin_run = 0.0;
  23. job[i].end_run = 0.0;
  24. job[i].turnover_time = 0.0;
  25. }
  26. }
  27. void print(jobtable job[], int n)
  28. {
  29. int i;
  30. printf("name in_well run_time begin_run end_run turnover_time\n");
  31. for(i = 0; i < n; ++ i)
  32. {
  33. printf("%s\t%0.1f\t%0.1f\t", job[i].name, job[i].in_well, job[i].run_time);
  34. if(job[i].begin_run == 0.0 && job[i].end_run == 0.0 && job[i].turnover_time == 0.0)
  35. {
  36. printf(" \n");
  37. }
  38. else
  39. {
  40. printf("%9.1f%9.1f\t%0.1f\n", job[i].begin_run, job[i].end_run, job[i].turnover_time);
  41. }
  42. }
  43. }
  44. void swap(jobtable job[], int p, int q)
  45. {
  46. float temp1;
  47. char temp2[8];
  48. strcpy(temp2, job[p].name);
  49. strcpy(job[p].name, job[q].name);
  50. strcpy(job[q].name, temp2);
  51. temp1 = job[p].in_well;
  52. job[p].in_well = job[q].in_well;
  53. job[q].in_well = temp1;
  54. temp1 = job[p].run_time;
  55. job[p].run_time = job[q].run_time;
  56. job[q].run_time = temp1;
  57. }
  58. // 模拟当前作业表的调度过程
  59. float response_ratio(jobtable job[], int n)
  60. {
  61. int i, j, temp;
  62. float average_time, ratio1, ratio2;
  63. job[0].begin_run = job[0].in_well;
  64. job[0].end_run = job[0].begin_run + job[0].run_time;
  65. job[0].turnover_time = job[0].end_run - job[0].begin_run;
  66. average_time = job[0].turnover_time;
  67. for(i = 1; i < n; ++ i)
  68. {
  69. if(job[i].in_well <= job[i - 1].end_run)
  70. {
  71. j = i + 1;
  72. temp = i;
  73. ratio1 = 1 + (job[i - 1].end_run - job[i].in_well) * 1.0 / job[i].run_time;
  74. while(j < n && job[j].in_well <= job[i - 1].end_run)
  75. {
  76. ratio2 = 1 + (job[i - 1].end_run - job[j].in_well) * 1.0 / job[j].run_time;
  77. if(ratio2 > ratio1)
  78. {
  79. temp = j;
  80. }
  81. j ++;
  82. }
  83. if(temp != i)
  84. {
  85. swap(job, i, temp);
  86. }
  87. }
  88. job[i].begin_run = job[i - 1].end_run;
  89. job[i].end_run = job[i].begin_run + job[i].run_time;
  90. job[i].turnover_time = job[i].end_run - job[i].in_well;
  91. average_time = average_time + job[i].turnover_time;
  92. }
  93. return (average_time / n);
  94. }
  95. int main()
  96. {
  97. int n;
  98. float ave_turnover_time;
  99. jobtable job[N];
  100. printf("input job numbers\n");
  101. scanf("%d", &n);
  102. if(n <= N)
  103. {
  104. printf("按照进入输入井的先后顺序初始化作业表\n");
  105. init(job, n);
  106. printf("initial station\n");
  107. print(job, n);
  108. ave_turnover_time = response_ratio(job, n);
  109. printf("termination station\n");
  110. print(job, n);
  111. printf("ave_turnover_time is : %.1f\n", ave_turnover_time);
  112. }
  113. else
  114. {
  115. printf("error!\n");
  116. }
  117. return 0;
  118. }
  119. /*
  120. 0 120 a
  121. 30 60 b
  122. 90 15 c
  123. */

  

 

转载于:https://www.cnblogs.com/mjn1/p/10710313.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/964637
推荐阅读
相关标签
  

闽ICP备14008679号