Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. #include <windows.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <process.h>
  5. #include <stdlib.h>
  6. #include <iostream>
  7. #include <string>
  8. #include <sstream>
  9. #include <time.h>
  10. #include <queue>
  11. #include <vector>
  12. #include <fstream>
  13.  
  14. using namespace std;
  15.  
  16.  
  17. struct ThreadInfo
  18. {
  19. ThreadInfo()
  20. {
  21. isStarted = false;
  22. runPermission = false;
  23. isFinished = false;
  24. priority = 0;
  25. age = 0;
  26. }
  27. int id;
  28. //when thread should be started
  29. int startTime;
  30. //How long thread should work
  31. int duration;
  32. //Scheduler set this boolean to simulate giving time shared to each thread
  33. int priority;
  34. //priority of current thread
  35. int age;
  36. //age of the thread
  37. bool runPermission;
  38. //Indicate if the thread is started
  39. bool isStarted;
  40. //Indicate if the thread is finished
  41. bool isFinished;
  42. };
  43.  
  44. struct QueueInfo
  45. {
  46. int timeSlice;
  47. //Time slice of the queue
  48. int maxAge;
  49. //maximum age of the thread before moving to next queue
  50. };
  51.  
  52. static QueueInfo* qinfo;
  53.  
  54. ofstream out;
  55.  
  56. unsigned __stdcall threadWord(void* a)
  57. {
  58. ThreadInfo* info = (ThreadInfo*)a;
  59.  
  60. //Print out start of this thread
  61. stringstream ss1(stringstream::in | stringstream::out);
  62. ss1 << "Clock: " << clock() << ", Thread " << info->id << ": Started." << endl;
  63. cout << ss1.str();
  64. out << ss1.str();
  65.  
  66. long isStarted = clock();
  67. long executionDuration = 0;
  68.  
  69. //Continue until total execution reaches the duration of the thread
  70. while(executionDuration < info->duration)
  71. {
  72. //Wait until get time slice from the scheduler
  73. while(!info->runPermission){}
  74.  
  75. //Calculate current cycle execution time
  76. long executionStart = clock();
  77. long cycleDuration = 0;
  78. //Continue the calculation until the time shared expires or thread finishes
  79. while(info->runPermission && executionDuration + cycleDuration < info->duration){
  80. cycleDuration = clock() - executionStart;
  81. }
  82. executionDuration += cycleDuration;
  83. info->age=clock()-info->startTime;
  84.  
  85. //Print out the execution time in each time slice
  86. stringstream ss(stringstream::in | stringstream::out);
  87. ss << "Clock: " << clock() << ", Thread " << info->id << ": Execution: " << executionDuration<<" Age: "<<info->age<<" Priority: "<<info->priority<<endl;
  88. cout << ss.str();
  89. out << ss.str();
  90. }
  91. long end = clock();
  92.  
  93.  
  94. //Print out end of this thread
  95. stringstream ss2(stringstream::in | stringstream::out);
  96. ss2 << "Clock: " << clock() << ", Thread " << info->id << ": Finished. Duration=" << end - isStarted << endl;
  97. cout << ss2.str();
  98. out << ss2.str();
  99.  
  100. //Set the thread as finished
  101. info->isFinished = true;
  102. return 0;
  103. }
  104.  
  105. void priority(ThreadInfo* obj)
  106. {
  107. int maxAge=qinfo[obj->priority].maxAge; //assigns maxAge the value of that queue's max age using the thread's priority number
  108. if((obj->age)>maxAge) //if the thread's age is greater than the max age, increase the priority by 1
  109. {
  110. obj->priority++;
  111. }
  112. }
  113.  
  114. //Round-robin Scheduler
  115. unsigned __stdcall scheduler_RR(void* a)
  116. {
  117. //Input thread infos
  118. queue<ThreadInfo*>* waitingThreads = (queue<ThreadInfo*>*)a;
  119.  
  120. while(!waitingThreads->empty())
  121. {
  122. //Get first element of the queue
  123. ThreadInfo *curThreadInfo = waitingThreads->front();
  124. waitingThreads->pop();
  125. //Check if start time has been reached
  126. if(curThreadInfo->startTime > clock())
  127. {
  128. //push back this thread and go for next thread
  129. waitingThreads->push(curThreadInfo);
  130. continue;
  131. }
  132.  
  133. //Check if the thread has already been started.
  134. //int start_time=clock()-curThreadInfo->startTime;
  135.  
  136. priority(curThreadInfo);
  137.  
  138. if(!curThreadInfo->isStarted)
  139. {
  140. curThreadInfo->isStarted = true;
  141. //start thread
  142. _beginthreadex(NULL, 0, threadWord, curThreadInfo, 0, 0);
  143. }
  144.  
  145.  
  146. //Give time share to the current thread
  147. curThreadInfo->runPermission = true;
  148. //Sleep(qinfo[curThreadInfo->priority].timeSlice);
  149.  
  150. if(curThreadInfo->priority==2) //occurs when time slice is -1 (lets the thread finish)
  151. {
  152. while(!curThreadInfo->isFinished){} //loop runs until thread has finished
  153. }
  154.  
  155. else
  156. Sleep(qinfo[curThreadInfo->priority].timeSlice); //time slice is the queue array value using the thread priority 0,1 or 2
  157. //Take the time share back from the current thread
  158. curThreadInfo->runPermission = false;
  159.  
  160. //If the thread is finished
  161. if(!curThreadInfo->isFinished)
  162. waitingThreads->push(curThreadInfo);
  163. }
  164.  
  165. return 0;
  166. }
  167.  
  168.  
  169.  
  170.  
  171. //Read input thread infos and return them as a queue of pointers
  172. queue<ThreadInfo*>* readThreadInfos(string fileName)
  173. {
  174. ifstream in(fileName);
  175.  
  176. queue<ThreadInfo*>* threadInfos = new queue<ThreadInfo*>();
  177. int idTemp = 0;
  178. while(in.good())
  179. {
  180. //Create each threadInfo instance
  181. ThreadInfo* info = new ThreadInfo();
  182. info->id = idTemp;
  183. in >> info->startTime;
  184. in >> info->duration;
  185. info->startTime *= 1000;
  186. info->duration *= 1000;
  187. threadInfos->push(info);
  188. idTemp++;
  189. }
  190. return threadInfos;
  191. }
  192.  
  193. void main()
  194. {
  195. //output file
  196. out = ofstream("c:\\output.txt");
  197.  
  198. //reading thread infos (start time and duration of each thread)
  199. queue<ThreadInfo*>* threadInfos = readThreadInfos("c:\\RR-input.txt");
  200.  
  201. ifstream myFile("config.txt");
  202.  
  203. int queueNum=0;
  204. myFile >> queueNum;
  205. qinfo = new QueueInfo[queueNum];
  206.  
  207.  
  208. for(int i=0; !myFile.eof(); i++)
  209. {
  210. if(i==0){}
  211. else
  212. {
  213. myFile >> qinfo[i-1].timeSlice;
  214. qinfo[i-1].timeSlice=qinfo[i-1].timeSlice*1000;
  215. myFile >> qinfo[i-1].maxAge;
  216. qinfo[i-1].maxAge=qinfo[i-1].maxAge*1000;
  217. }
  218. }
  219.  
  220.  
  221. //Start scheduler thread
  222. _beginthreadex(NULL, 0, scheduler_RR, threadInfos, 0, 0);
  223.  
  224. getchar();
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement