Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <queue>
  4. #include<string>
  5. #include<limits>
  6. #include<map>
  7. #include<algorithm>
  8. #include<math.h>
  9. using namespace std;
  10.  
  11. struct process{
  12. string name;
  13. double run_time;
  14. int arrival_time;
  15. int processed_time;
  16. int deque_flag;
  17. double start_time;
  18. double second_start_time;//added//if runtime greater than quantum//processed on two iterations
  19. double finish_time;
  20.  
  21. double stopped_time;//indicate at which process stopped
  22. bool isFinished;//process done or not
  23. bool was_proccesed;
  24. bool was_queued;
  25. process()//default constructor
  26. {
  27. deque_flag = 0;
  28. processed_time = 0;
  29. isFinished = false;
  30. was_proccesed = false;
  31. was_queued = false;
  32. }
  33.  
  34. };
  35. //To sort arrival times
  36. void BubbleSort(process * pro_arr, int len)
  37. {
  38. int i, j, flag = 1; // set flag to 1 to start first pass
  39. process temp; // holding variable
  40. for (i = 1; (i <= len) && flag; i++)
  41. {
  42. flag = 0;
  43. for (j = 0; j < (len - 1); j++)
  44. {
  45. if (pro_arr[j + 1].arrival_time < pro_arr[j].arrival_time)
  46. {
  47. temp = pro_arr[j]; // swap elements
  48. pro_arr[j] = pro_arr[j + 1];
  49. pro_arr[j + 1] = temp;
  50. flag = 1; // indicates that a swap occurred.
  51. }
  52. }
  53. }
  54.  
  55. }
  56.  
  57.  
  58. int main(){
  59.  
  60. string inputFileName;
  61. while (true){
  62. cout << "enter File name : ";
  63. cin >> inputFileName;
  64. ifstream in(inputFileName);
  65. if (!in){
  66. cout << "File not found" << endl;
  67. exit(1); //exit the programe
  68. }
  69. string outFile_name;
  70. outFile_name = inputFileName.substr(0, inputFileName.length() - 4) + "_output" + inputFileName.substr(inputFileName.length() - 4, 4); //set out file name
  71.  
  72. string out_log_file_name;
  73. out_log_file_name = inputFileName.substr(0, inputFileName.length() - 4) + "_log" + inputFileName.substr(inputFileName.length() - 4, 4);//set log file name
  74. ofstream myfile;
  75. myfile.open(outFile_name);
  76. myfile << "process_id " << "run_time " << "Arrival_time " << "start_time " << "finish_time " << "TA_time " << "weighted_TA_time" << endl;
  77. ofstream my_log_file;
  78. my_log_file.open(out_log_file_name);
  79. cout << " the output is stored at text file " << outFile_name << "\n and the log file is stored at text file " << out_log_file_name << endl;
  80.  
  81.  
  82. //temp is to get rid of unnessacery input strings
  83. string temp;
  84. in >> temp;
  85. int quantum, switch_time;
  86. in >> quantum >> temp >> switch_time;
  87. in >> temp >> temp >> temp;
  88.  
  89. int iterator = 0;
  90.  
  91. process * pro_arr = new process[1000]; //save all coming process
  92. string temp_name; //tEMP Name of the process
  93. do {
  94. in >> temp_name;
  95. if (iterator != 0 && temp_name == pro_arr[iterator - 1].name) //this is to handle the input
  96. break;
  97. else
  98. {
  99. //save all process info to the array
  100. pro_arr[iterator].name = temp_name;
  101. in >> pro_arr[iterator].run_time >> pro_arr[iterator].arrival_time;
  102. iterator++;
  103. }
  104. } while (!in.eof());
  105. //sort arrivals time
  106. BubbleSort(pro_arr, iterator);
  107.  
  108.  
  109. queue<process> my_queue; //queue to handle round robin algorithim
  110. queue<process> my_printed_queue;
  111. double time = 0; //current cpu time
  112.  
  113.  
  114. bool finish = true; //all process are queued and finished
  115. string last_runned; //the last runned process name
  116.  
  117. map<string, process> out_process; //to get output in the order of thier input where the string is the process name
  118. //Start round roubin
  119. while (true)
  120. {
  121. //loop throw the input array
  122. for (int i = 0; i < iterator; i++){
  123. if (!pro_arr[i].was_queued){
  124. finish = false;//we are not done with all process yet
  125. }
  126. if (pro_arr[i].arrival_time <= time && !pro_arr[i].was_proccesed&&!pro_arr[i].was_queued){
  127. pro_arr[i].was_queued = true;
  128. my_queue.push(pro_arr[i]);//put the process in the queue
  129. }
  130.  
  131.  
  132. }
  133.  
  134. if (finish&&my_queue.empty())
  135. break; //all process are done
  136. if (!my_queue.empty()){
  137.  
  138. //to make sure that the top of the queue is not the last runned process
  139. if (my_queue.front().was_proccesed&&my_queue.front().name == last_runned){
  140. process temp = my_queue.front();
  141. my_queue.pop();
  142. time += (switch_time);
  143. time++;
  144. my_queue.push(temp);
  145. }
  146. //outting the queue members//
  147. if (my_queue.size()>0)//not to be printed if there is no queue.
  148. {
  149. my_log_file << "\n \n Queue: ";
  150. }
  151. while (!(my_queue.empty()))
  152. {
  153. //cout<<my_queue.front().name;
  154. my_log_file << my_queue.front().name ;
  155. process hopa = my_queue.front();
  156. my_printed_queue.push(hopa);//saving elements of my my_queue
  157. my_queue.pop();
  158. if (!my_queue.empty())
  159. my_log_file << ",";
  160. }
  161. //int deque_flag=0;
  162. //cout << "\n" << endl;
  163. while (!my_printed_queue.empty())//restore my queue
  164. {
  165. process hopa = my_printed_queue.front();
  166. my_queue.push(hopa);
  167. my_printed_queue.pop();
  168.  
  169.  
  170. }
  171.  
  172. //START SIMULATING
  173.  
  174. if (!my_queue.front().was_proccesed){
  175. my_queue.front().start_time = time;
  176. my_queue.front().was_proccesed = true;
  177. }
  178. else{ //start switching
  179. time += (switch_time);
  180. time++;
  181.  
  182. }
  183.  
  184. last_runned = my_queue.front().name;
  185.  
  186. if (my_queue.front().run_time - my_queue.front().processed_time <= quantum){
  187.  
  188. time += (my_queue.front().run_time - my_queue.front().processed_time);
  189. my_queue.front().finish_time = time;
  190. my_queue.front().second_start_time = time - (my_queue.front().run_time - my_queue.front().processed_time);//current time - remained time from first process iterations..reamining-->(93-50)
  191. //added//
  192. my_queue.front().isFinished = true;//to flag that the process is done
  193.  
  194. out_process[my_queue.front().name] = my_queue.front();
  195. my_queue.front().deque_flag = 1;//if we deque here we cant print element that is finished being processed from first iteration//so we deque after printing
  196. //else my_queue.pop();
  197.  
  198.  
  199. }
  200. else{ //move time to the next quantam
  201. time += quantum;
  202. my_queue.front().processed_time += (quantum);
  203. my_queue.front().stopped_time = time;
  204. }
  205. time++; //move time one step;
  206.  
  207. }
  208.  
  209.  
  210. else{
  211. double temp_time = 10000000;
  212. for (int i = 0; i < iterator; i++){
  213.  
  214. if (!pro_arr[i].was_proccesed&&!pro_arr[i].was_queued){
  215. temp_time = pro_arr[i].arrival_time;
  216. break;
  217. }
  218. }
  219. time += (quantum + 1);
  220. if (time>temp_time)
  221. time = temp_time;
  222. }
  223. ////outting log file//
  224. if (!my_queue.size() == 0)//if there is no eleemtns dont enter printing region// to avoid runtime error//
  225. {
  226.  
  227. if (my_queue.front().isFinished&&my_queue.front().run_time>quantum)//at the second iteration//...//means process takes two iterations to be done,then at the secind iteration we use secon_start_time
  228. {
  229. my_log_file << "\n\n process switching : started at " << my_queue.front().second_start_time - switch_time-1 << ", \tfinished at " << my_queue.front().second_start_time - 1;//switching
  230. my_log_file << "\n \n Executing process " << my_queue.front().name << " :";
  231. my_log_file << " started at " << my_queue.front().second_start_time << ",\t" << "finished at " << my_queue.front().finish_time;
  232. }
  233. if (my_queue.front().isFinished&&my_queue.front().run_time<quantum)//process toke one iteration only
  234. {
  235. my_log_file << "\n \n Executing process " << my_queue.front().name << " :";
  236. my_log_file << " started at " << my_queue.front().start_time << ", \t" << "finished at " << my_queue.front().finish_time;
  237. }
  238. if (!my_queue.front().isFinished)//process not finished yet
  239. {
  240. my_log_file << "\n \n Executing process " << my_queue.front().name << " :";
  241. my_log_file << " started at " << my_queue.front().start_time << ", \tstopped at " << my_queue.front().stopped_time << ",\t" << my_queue.front().run_time - my_queue.front().processed_time << " remaining";
  242. my_log_file << "\n\n process switching : started at " << my_queue.front().stopped_time + 1 << ", \tfinished at " << my_queue.front().stopped_time + switch_time+1;//switching
  243. }
  244.  
  245.  
  246. if (my_queue.front().deque_flag == 1){ my_queue.pop(); } //
  247. finish = true;
  248. }
  249. }//end of while
  250.  
  251.  
  252. double total_waiting_time = 0;
  253. double total_weighted_turn_around = 0;
  254. double * wegihted_arr = new double[iterator];
  255. //start outing to the out file
  256. for (int i = 0; i < iterator; i++){
  257. process temp = out_process[pro_arr[i].name];
  258. double weighted_turn_around_time = (temp.finish_time - temp.arrival_time) / temp.run_time;
  259. wegihted_arr[i] = weighted_turn_around_time;
  260. total_weighted_turn_around += weighted_turn_around_time;
  261. total_waiting_time += (temp.start_time - temp.arrival_time);
  262. myfile << temp.name << " " << temp.run_time << " " << temp.arrival_time << " " << temp.start_time << " " << temp.finish_time << " " << temp.finish_time - temp.arrival_time << " " << weighted_turn_around_time << endl;
  263. }
  264.  
  265. double avg_weighted_turn_around_time = total_weighted_turn_around / (iterator);
  266.  
  267. myfile << "Average Weighted TA time: " << avg_weighted_turn_around_time << endl;
  268.  
  269. //calculate standard div
  270. double total = 0;
  271. for (int i = 0; i < iterator; i++){
  272.  
  273. total += pow(avg_weighted_turn_around_time - wegihted_arr[i], 2);
  274.  
  275. }
  276.  
  277. myfile << "Standard deviation Weighted TA time: " << sqrt(total / iterator) << endl;
  278. myfile << "Average waiting time :" << total_waiting_time / iterator << endl;
  279.  
  280. in.close();
  281. myfile.close();
  282. my_log_file.close();
  283. }
  284. return 0;
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement