Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. --------------------------------------------------
  2. disk_gen (tester program)
  3. --------------------------------------------------
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7.  
  8. int main()
  9. {
  10. FILE *fp = fopen("requests", "w");
  11.  
  12. unsigned long long time = 0;
  13.  
  14. while (1)
  15. {
  16. for (int i = 0; i < rand() % 100; i++)
  17. {
  18. time += rand() % 11;
  19. fprintf(fp, "%24llu %14d %9d\n", time, rand() % (1 << 20), rand() % 100);
  20. fflush(fp);
  21. }
  22. sleep(2);
  23. }
  24.  
  25. fclose(fp);
  26. }
  27.  
  28.  
  29.  
  30. --------------------------------------------------
  31. disk
  32. --------------------------------------------------
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35.  
  36. typedef struct
  37. {
  38. unsigned int loc : 30;
  39. unsigned int proc : 30;
  40. } rec;
  41.  
  42. typedef union
  43. {
  44. rec x;
  45. long y;
  46. } rec_un;
  47.  
  48. FILE *inf;
  49. FILE *outf;
  50.  
  51. long q[10000];
  52. int qi = 0;
  53.  
  54. void enq(long n);
  55. long deq(int pos);
  56. void load(unsigned long long time);
  57. int smallest();
  58. int largest();
  59. unsigned long long process(unsigned long long time);
  60.  
  61. int main()
  62. {
  63. inf = fopen("requests", "r");
  64. outf = fopen("results", "w");
  65.  
  66. unsigned long long time = 0;
  67. while (1)
  68. {
  69. load(time);
  70. time = process(time);
  71. }
  72.  
  73. fclose(outf);
  74. fclose(inf);
  75. }
  76.  
  77. void enq(long n)
  78. {
  79. q[qi++] = n;
  80. }
  81.  
  82. long deq(int pos)
  83. {
  84. long r = q[pos];
  85. q[pos] = q[--qi];
  86. return r;
  87. }
  88.  
  89. void load(unsigned long long time)
  90. {
  91. static unsigned long long t = 0;
  92. static rec_un x = {0};
  93. int loc;
  94. int proc;
  95.  
  96. if (t > time)
  97. {
  98. time = t;
  99. }
  100. while (t <= time)
  101. {
  102. if (x.y != 0)
  103. {
  104. enq(x.y);
  105. }
  106. while (fscanf(inf, "%24llu %14d %9d\n", &t, &loc, &proc) <= 0)
  107. {
  108. fseek(inf, -1, SEEK_CUR);
  109. }
  110. x.x.loc = loc;
  111. x.x.proc = proc;
  112. }
  113. }
  114.  
  115. int smallest()
  116. {
  117. int si = 0;
  118.  
  119. for (int i = 1; i < qi; i++)
  120. {
  121. if (q[i] < q[si])
  122. {
  123. si = i;
  124. }
  125. }
  126. return si;
  127. }
  128.  
  129. int largest()
  130. {
  131. int li = 0;
  132.  
  133. for (int i = 1; i < qi; i++)
  134. {
  135. if (q[i] > q[li])
  136. {
  137. li = i;
  138. }
  139. }
  140. return li;
  141. }
  142.  
  143. unsigned long long process(unsigned long long time)
  144. {
  145. static int dir = 0;
  146.  
  147. if (dir)
  148. {
  149. while (qi)
  150. {
  151. rec_un y;
  152. y.y = deq(smallest());
  153. time += 5;
  154. fprintf(outf, "%24llu %9d\n", time, y.x.proc);
  155. }
  156. }
  157. else
  158. {
  159. while (qi)
  160. {
  161. rec_un y;
  162. y.y = deq(largest());
  163. time += 5;
  164. fprintf(outf, "%24llu %9d\n", time, y.x.proc);
  165. }
  166. }
  167. fflush(outf);
  168. dir ^= 1;
  169. return time;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement