Advertisement
Guest User

Afaskfaskf

a guest
Sep 18th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. // Marti LEVEN 13 Sept 2014
  2. //
  3. #include<stdio.h>
  4. #include<cstdlib>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<vector>
  8. #include<math.h>
  9.  
  10. using namespace std;
  11.  
  12. #define DISK_RPM 5400
  13. #define DISK_SECTORS 63
  14. #define SECTORS_PER_SEC (DISK_RPM/60.0*DISK_SECTORS)
  15.  
  16. int main(int argc, char *argv[]) {
  17.  
  18. // Check that we have the correct number of arguments - a event file and number
  19. if ( argc < 2 ) {
  20. fprintf(stderr, "Incorrect number of arguments\n");
  21. return EXIT_FAILURE;
  22. }
  23.  
  24. // Open the schedule file
  25. FILE *schd_file = fopen(argv[1],"r");
  26. // Assume the event file has lines of less than 132 chars
  27. char line[133];
  28.  
  29.  
  30. // Count the number of lines in the schedule file == number of events
  31. int num = 0;
  32. //while(fgetc(schd_file) != EOF){
  33. while( !feof(schd_file) ) {
  34. fgets(line,128,schd_file);
  35. num++;
  36. }
  37. printf(" Read %i lines in the event file\n", num );
  38.  
  39. // Rewind the input file
  40. rewind( schd_file );
  41.  
  42. // Generate some vectors to store the schedule data
  43. int n = num +3; // allow a margin for error
  44. int *time = new int[n]; // allocates an array of 'n' adjacent integers (for time, pid and d_sect)
  45. int *extype = new int[n]; // allocates an array of 'n' different chars (for event type)
  46. int *pid = new int[n];
  47. int *d_sect = new int[n];
  48. char event_type[24];
  49. int t, e, p, d;
  50.  
  51. // Read thru the event file storing the data
  52. int i = 0;
  53. int num_fields = 0;
  54.  
  55. // printf(" Read thru the event file\n");
  56. fgets(line,128,schd_file);
  57. while( !feof(schd_file) ) {
  58. //printf("\n %s \n",line);
  59. num_fields = sscanf(line,"%i %s %i %i", &t, event_type, &p, &d);
  60. if (num_fields == 4) {
  61. time[i] = t;
  62. e = 0;
  63. if ( strncmp(event_type, "spawn", 5 ) == 0) {
  64. e = 1;
  65. }
  66. if ( strncmp(event_type, "io", 2 ) == 0 ) {
  67. e = 2;
  68. }
  69. if ( strncmp(event_type, "exit", 4 ) == 0 ) {
  70. e = 3;
  71. }
  72. extype[i] = e;
  73. pid[i] = p;
  74. d_sect[i] = d;
  75. printf("\n %6i %4i %8i %8i \n", time[i], extype[i], pid[i], d_sect[i] );
  76. i++;
  77. }
  78. fgets(line,128,schd_file);
  79. }
  80. // printf("\n\n%6i \n", extype[i-6]);
  81. int *abtime = new int[n];
  82. int k = 0;
  83. while(k < i) {
  84. switch(extype[k]){
  85. case 1:
  86. abtime[k] = time[k];
  87. k++;
  88. break;
  89. case 2:
  90. abtime[k] = time[k] + abtime[k-1];
  91. k++;
  92. break;
  93. case 3:
  94. abtime[k] = time[k] + abtime[k-1];
  95. k++;
  96. break;
  97. default:
  98. continue;
  99. }
  100. }
  101.  
  102. //create a microsecond absolute time vector
  103. int *micro_time = new int[n];
  104. for(int b=0; b < i; b++){
  105. micro_time[b] = (abtime[b] *1000);
  106. printf("\n %i \n", micro_time[b]);
  107. }
  108.  
  109. int current_time = micro_time[0];
  110. int time_blocked = 0;
  111. int check_overloop = 0;
  112. int task_num = 0;
  113.  
  114. float disk_sector = 0;
  115. //float disk_incr = 0.00567;
  116. // takes 11.11 ms for the disk to rotate,leeway gives 12 ms and include every event
  117.  
  118.  
  119. while(current_time < micro_time[k-1] ) {
  120. if( current_time >= micro_time[task_num] && extype[task_num] == 2 && (int)(disk_sector) == d_sect[task_num]) {
  121. // the current IO task is completed successfully
  122. time_blocked++;
  123. disk_sector = ((int)((current_time/1000000.0) * SECTORS_PER_SEC)) % DISK_SECTORS;
  124. //disk_sector = fmod((disk_sector + disk_incr), 62);
  125. check_overloop++;
  126.  
  127.  
  128.  
  129. } else if(current_time >= micro_time[task_num] && extype[task_num] == 2 && int(disk_sector) != d_sect[task_num]) {
  130. // the current IO task is blocked and the disk must rotate to reach the required disk sector
  131. current_time++;
  132. //disk_sector = fmod((disk_sector + disk_incr), 62);
  133. task_num++;
  134. disk_sector = ((int)((current_time/1000000.0) * SECTORS_PER_SEC)) % DISK_SECTORS;
  135. check_overloop++;
  136.  
  137.  
  138.  
  139. } else if(current_time >= micro_time[task_num] && extype[task_num] != 2) {
  140. // the current spawn/exit task is completed successfully
  141. task_num++;
  142. current_time++;
  143. check_overloop++;
  144. disk_sector = ((int)((current_time/1000000.0) * SECTORS_PER_SEC)) % DISK_SECTORS;
  145. //disk_sector = fmod((disk_sector + disk_incr), 62);
  146.  
  147.  
  148.  
  149. } else if(current_time < micro_time[task_num]){
  150. // the processor is waiting for the next new task
  151. current_time++;
  152. check_overloop++;
  153. disk_sector = ((int)((current_time/1000000.0) * SECTORS_PER_SEC)) % DISK_SECTORS;
  154. //disk_sector = fmod((disk_sector + disk_incr),62);
  155.  
  156.  
  157.  
  158.  
  159. } else {
  160. // something is wrong!
  161. printf("\n there is some sort of error! \n");
  162. }
  163. }
  164.  
  165.  
  166. /*
  167.  
  168. while( current_time < micro_time[k-1] ) {
  169. //printf("\n %i %i \n", current_time, micro_time[task_num]);
  170. if( current_time >= micro_time[task_num] ) {
  171. printf("\n ex type is %i \n", extype[task_num]);
  172. //printf("\n %i %i \n", current_time, micro_time[task_num]);
  173. if( extype[task_num] == 2 ) {
  174. printf("\n disk sectors %i %i \n", int(disk_sector), d_sect[task_num]);
  175. if( round(disk_sector) == d_sect[task_num] ) {
  176. printf("\n disk sector 2 %f \n", disk_sector);
  177. disk_sector = fmod((disk_sector + disk_incr), 62);
  178. time_blocked++;
  179. check_overloop++;
  180. } else {
  181. current_time++;
  182. disk_sector = fmod((disk_sector + disk_incr), 62);
  183. //printf("\n %f time %i \n", disk_sector, current_time);
  184. task_num++;
  185. check_overloop++;
  186. }
  187.  
  188. } else {
  189. //printf("\n sector %f time %i \n", disk_sector, current_time);
  190. task_num++;
  191. //printf("\n %i %i \n", current_time, micro_time[task_num]);
  192. current_time++;
  193. check_overloop++;
  194. disk_sector = fmod((disk_sector + disk_incr), 62);
  195. }
  196.  
  197. } else {
  198. //printf("\n %i %i \n", current_time, micro_time[task_num]);
  199. current_time++;
  200. check_overloop++;
  201. disk_sector = fmod((disk_sector + disk_incr),62);
  202. }
  203. }
  204. */
  205. int total_time = abtime[k-1] - abtime[0] + time_blocked;
  206. printf("\n %i %i \n", abtime[k-1], abtime[0]);
  207. printf("\n %i %i \n", total_time, time_blocked);
  208. printf("\n %i \n", check_overloop);
  209. return EXIT_SUCCESS;
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement