Guest User

Untitled

a guest
Feb 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.77 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<iomanip>
  4. #include<algorithm>
  5. #include<deque>
  6. #include<vector>
  7. using namespace std;
  8.  
  9. struct Scheduling{
  10. int id,
  11. arrivetime,
  12. brusttime,
  13. priority,
  14. waitingtime,
  15. turnaroundtime;
  16.  
  17. Scheduling() : id(0), arrivetime(0), brusttime(0), priority(0), waitingtime(0), turnaroundtime(0){};
  18. };
  19.  
  20. class Method{
  21. private:
  22. deque<Scheduling> Process;
  23. deque<Scheduling> Process_clone;
  24. deque<Scheduling> Process_brust;
  25. vector<Scheduling> completion;
  26.  
  27. public:
  28. Method(){};
  29.  
  30. void Insert(Scheduling data);
  31. void Display_orig();
  32. void Display();
  33. void FCFS();
  34. void SJF_Non();
  35. void SRTF();
  36. void Priority_non();
  37. void RR(int quantum);
  38. void SWAP(deque<Scheduling>::iterator itr1, deque<Scheduling>::iterator itr2);
  39. };
  40.  
  41. void Method::SWAP(deque<Scheduling>::iterator itr1, deque<Scheduling>::iterator itr2){
  42. deque<Scheduling>::iterator temp = itr1;
  43. itr1 = itr2;
  44. itr2 = temp;
  45. }
  46.  
  47. void Method::Display(){
  48.  
  49. cout << setw(3) << "Process" << setw(15) << "Waiting Time" << setw(18) << "Turnaround Time" <<endl;
  50.  
  51. for (int i = 0; i < completion.size(); i++){
  52. cout << setw(3) << "P" << completion[i].id << setw(12) << completion[i].waitingtime << setw(15) << completion[i].turnaroundtime << endl;
  53. }
  54. cout << endl;
  55. }
  56.  
  57. void Method::Display_orig(){
  58.  
  59. cout << setw(3) << "Process" << setw(15) << "Arrival Time" << setw(15) << "Brust Time" << setw(15) << "Priority" << endl;
  60.  
  61. for (int i = 0; i < Process.size(); i++){
  62. cout << setw(3) <<"P" << Process[i].id << setw(12) <<Process[i].arrivetime << setw(15) << Process[i].brusttime << setw(17) << Process[i].priority << endl;
  63. }
  64. cout << endl;
  65. }
  66.  
  67. void Method::Insert(Scheduling data){
  68.  
  69. Process.push_back(data);
  70. Process_clone.push_back(data);
  71. Process_brust.push_back(data);
  72. }
  73.  
  74. bool comp_arrival(const Scheduling lhs, const Scheduling rhs){
  75. return lhs.arrivetime < rhs.arrivetime;
  76. }
  77.  
  78. bool comp_brust(const Scheduling lhs, const Scheduling rhs){
  79. return lhs.brusttime < rhs.brusttime;
  80. }
  81.  
  82. bool comp_priority(const Scheduling lhs, const Scheduling rhs){
  83. return lhs.priority < rhs.priority;
  84. }
  85.  
  86. void Method::FCFS(){
  87.  
  88. sort(Process.begin(), Process.end(), comp_arrival);
  89. int time = 0;
  90. deque<Scheduling>::iterator itr;
  91. for (itr = Process.begin(); itr != Process.end(); itr++){
  92. time += (*itr).brusttime;
  93. (*itr).waitingtime = time - (*itr).arrivetime - (*itr).brusttime;
  94. (*itr).turnaroundtime = time - (*itr).arrivetime;
  95.  
  96. completion.push_back((*itr));
  97. }
  98. cout << "FCFS Algorithm: " << endl;
  99.  
  100. Display();
  101.  
  102. for (itr = Process.begin(); itr != Process.end(); itr++){
  103. itr->waitingtime = 0;
  104. itr->turnaroundtime = 0;
  105. }
  106.  
  107. completion.clear();
  108. }
  109.  
  110. void Method::SJF_Non(){
  111.  
  112. sort(Process.begin(), Process.end(), comp_arrival);
  113.  
  114. deque<Scheduling>::iterator itr, itr_begin = Process.begin();
  115.  
  116.  
  117. deque<Scheduling> q;
  118. q.push_back((*itr_begin));
  119.  
  120. int time = itr_begin->brusttime;
  121. itr_begin->waitingtime += time - itr_begin->brusttime - itr_begin->arrivetime;
  122. itr_begin->turnaroundtime += time - itr_begin->arrivetime;
  123.  
  124. completion.push_back((*itr_begin));
  125.  
  126. bool visited[10];
  127.  
  128. for (int i = 0; i < 10; i++){
  129. visited[i] = false;
  130. }
  131.  
  132. while (q.size()){
  133.  
  134. if (q.front().arrivetime == Process.begin()->arrivetime){
  135. q.pop_front();
  136. }
  137.  
  138. for (itr = Process.begin() + 1; itr != Process.end(); itr++){
  139. // check the process whether in the schedule or not
  140. if (itr->arrivetime <= time && visited[itr->id] == false){
  141. q.push_back((*itr));
  142. visited[itr->id] = true;
  143. }
  144. }
  145.  
  146. sort (q.begin(), q.end(), comp_brust);
  147. deque<Scheduling>::iterator finish = q.begin();
  148.  
  149. time += finish->brusttime;
  150. finish->waitingtime += time - finish->brusttime - finish->arrivetime;
  151. finish->turnaroundtime += time - finish->arrivetime;
  152.  
  153. completion.push_back((*finish));
  154.  
  155. q.pop_front();
  156. }
  157.  
  158. cout << "SJF(Non-Preemptive) : " << endl;
  159.  
  160. Display();
  161.  
  162. for (itr = Process.begin(); itr != Process.end(); itr++){
  163. itr->waitingtime = 0;
  164. itr->turnaroundtime = 0;
  165. }
  166.  
  167. completion.clear();
  168. }
  169.  
  170. void Method::Priority_non(){
  171.  
  172. sort(Process.begin(), Process.end(), comp_arrival);
  173.  
  174. deque<Scheduling>::iterator itr;
  175.  
  176. deque<Scheduling> q;
  177. q.push_back((*Process.begin()));
  178.  
  179. int time = Process.begin()->brusttime;
  180. Process.begin()->waitingtime += time - Process.begin()->brusttime - Process.begin()->arrivetime;
  181. Process.begin()->turnaroundtime += time - Process.begin()->arrivetime;
  182.  
  183. completion.push_back((*Process.begin()));
  184.  
  185. bool visited[10]; // record the status of the arrival process
  186.  
  187. for (int i = 0; i < 10; i++){
  188. visited[i] = false;
  189. }
  190.  
  191. while (q.size()){
  192.  
  193. if (q.front().arrivetime == Process.begin()->arrivetime){
  194. q.pop_front();
  195. }
  196.  
  197. for (itr = Process.begin() + 1; itr != Process.end(); itr++){
  198. // check the process whether in the schedule or not
  199. if (itr->arrivetime <= time && visited[itr->id] == false){
  200. q.push_back((*itr));
  201. visited[itr->id] = true;
  202. }
  203. }
  204.  
  205. sort (q.begin(), q.end(), comp_priority);
  206. deque<Scheduling>::iterator finish = q.begin();
  207.  
  208. time += finish->brusttime;
  209. finish->waitingtime += time - finish->brusttime - finish->arrivetime;
  210. finish->turnaroundtime += time - finish->arrivetime;
  211.  
  212. completion.push_back((*finish));
  213.  
  214. q.pop_front();
  215. }
  216.  
  217. cout << "Priority : " << endl;
  218.  
  219. Display();
  220.  
  221. for (itr = Process.begin(); itr != Process.end(); itr++){
  222. itr->waitingtime = 0;
  223. itr->turnaroundtime = 0;
  224. }
  225.  
  226. completion.clear();
  227. }
  228.  
  229. void Method::SRTF(){
  230.  
  231. int time_total = 0, time_diff = 0;
  232. int brust_orig[10];
  233.  
  234. for (deque<Scheduling>::iterator itr_curr = Process.begin(); itr_curr != Process.end(); itr_curr++){
  235. brust_orig[itr_curr->id] = itr_curr->brusttime;
  236. }
  237.  
  238. sort(Process.begin(), Process.end(), comp_arrival);
  239.  
  240. deque<Scheduling>::iterator itr;
  241. deque<Scheduling>::iterator itr_curr;
  242. deque<Scheduling>::iterator itr_smallest;
  243. for (itr = Process.begin(); itr != Process.end(); itr++){
  244.  
  245. deque<Scheduling>::iterator temp = itr;
  246. deque<Scheduling>::iterator itr_next = ++temp;
  247. if (itr == Process.begin()){
  248. time_diff += itr_next->arrivetime - itr->arrivetime;
  249. time_total += time_diff;
  250. itr->brusttime -= time_diff;
  251. }
  252. else if (itr_next == Process.end()){ // the lastest arrival process has no time difference
  253. break;
  254. }
  255. else{
  256. sort(Process_brust.begin(), Process_brust.end(), comp_brust);
  257.  
  258. for (itr_curr = Process_brust.begin(); itr_curr != Process_brust.end(); itr_curr++){
  259. if (itr_curr->brusttime == 0){
  260. Process_brust.erase(itr_curr);
  261. continue;
  262. }
  263. else if (itr->brusttime > itr_curr->brusttime && itr_curr->arrivetime <= time_total){
  264. itr_smallest = itr_curr;
  265. break;
  266. }
  267. else{
  268. itr_smallest = itr;
  269. }
  270. }
  271. time_diff = 0;
  272. time_diff += itr_next->arrivetime - itr->arrivetime;
  273. time_total += time_diff;
  274. itr_smallest->brusttime -= time_diff;
  275.  
  276. // the same operation should do to another deque of process. Process <-> Process_brust
  277. if (itr_smallest == itr){
  278. for (itr_curr = Process_brust.begin(); itr_curr != Process_brust.end(); itr_curr++){
  279. int temptime = itr_smallest->brusttime;
  280. if (itr_curr->brusttime == temptime + time_diff)
  281. itr_curr->brusttime -= time_diff;
  282. }
  283. }
  284. else{
  285. for (itr_curr = Process.begin(); itr_curr != Process.end(); itr_curr++){
  286. int temptime = itr_smallest->brusttime;
  287. if (itr_curr->brusttime == temptime + time_diff)
  288. itr_curr->brusttime -= time_diff;
  289. }
  290. }
  291.  
  292. if (itr_smallest->brusttime == 0){ // check the process whether completed in advance or not
  293. itr_smallest->waitingtime += time_total - itr_smallest->arrivetime - brust_orig[itr_smallest->id];
  294. itr_smallest->turnaroundtime += time_total - itr_smallest->arrivetime;
  295. completion.push_back((*itr_smallest));
  296. }
  297. }
  298. }
  299. sort(Process_brust.begin(), Process_brust.end(), comp_brust);
  300.  
  301. for (deque<Scheduling>::iterator itr_curr = Process_brust.begin(); itr_curr != Process_brust.end(); itr_curr++){
  302. time_total += itr_curr->brusttime;
  303. itr_curr->waitingtime += time_total - itr_curr->arrivetime - brust_orig[itr_curr->id];
  304. itr_curr->turnaroundtime += time_total - itr_curr->arrivetime;
  305. completion.push_back((*itr_curr));
  306. }
  307.  
  308. cout << "Shortest Remaining Time First : " << endl;
  309.  
  310. Display();
  311.  
  312. for (itr = Process.begin(); itr != Process.end(); itr++){
  313. itr->waitingtime = 0;
  314. itr->turnaroundtime = 0;
  315. }
  316.  
  317. completion.clear();
  318. }
  319.  
  320. void Method::RR(int quantum){
  321.  
  322. sort(Process_clone.begin(), Process_clone.end(), comp_arrival);
  323. int time = 0;
  324. int brust_orig[10];
  325.  
  326. // duplicate the original data of brusttime
  327. for (deque<Scheduling>::iterator itr_curr = Process_clone.begin(); itr_curr != Process_clone.end(); itr_curr++){
  328. brust_orig[itr_curr->id] = itr_curr->brusttime;
  329. }
  330.  
  331. deque<Scheduling>::iterator itr;
  332. for (itr = Process_clone.begin(); itr != Process_clone.end() ; itr++){
  333. // process's brusttime runs out
  334. if (itr->brusttime <= quantum){
  335. time += itr->brusttime;
  336. itr->waitingtime += time - itr->arrivetime - brust_orig[itr->id];
  337. itr->turnaroundtime += time - itr->arrivetime;
  338. completion.push_back((*itr));
  339. }
  340. // continuous to run
  341. else{
  342. time += quantum;
  343. itr->brusttime -= quantum;
  344. Process_clone.push_back((*itr));
  345. }
  346. }
  347. cout << "RR : " << endl;
  348.  
  349. Display();
  350. }
  351.  
  352. int main(){
  353. FILE *fin;
  354. fin = fopen("scheduling.txt", "r");
  355. if (fin == NULL){
  356. printf ("File not found. \n");
  357. exit(EXIT_FAILURE);
  358. }
  359.  
  360. int sum = 0, num[50];
  361. while (feof(fin) == 0){
  362. fscanf(fin, "%d ", &num[sum]);
  363. sum++;
  364. }
  365.  
  366. Method work;
  367.  
  368. struct Scheduling data;
  369.  
  370. int c = 1, i;
  371. for (i = 0; i < sum; i++){
  372. if (!(i % 3)){
  373. data.id = c++;
  374. data.arrivetime = num[i];
  375. data.brusttime = num[i + 1];
  376. data.priority = num[i + 2];
  377. work.Insert(data);
  378. }
  379. }
  380.  
  381. work.Display_orig();
  382.  
  383. work.FCFS();
  384. work.SJF_Non();
  385. work.Priority_non();
  386. work.SRTF();
  387.  
  388. int quantum = 2;
  389.  
  390. work.RR(quantum);
  391.  
  392. fclose(fin);
  393. return 0;
  394. }
Add Comment
Please, Sign In to add comment