scheduling.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. NAME : RUSHIKESH M FANSE
  3. DIV : H
  4. ROLL NO : 16
  5. ASSIGNMENT : DIFFERENT JOB SCHEDULNG
  6. */
  7. #include<iostream>
  8. #include<cstdio>
  9. #include<fstream>
  10. #include<list>
  11. #include<algorithm>
  12. #define MAX 10
  13. using namespace std;
  14. ifstream fin;
  15. ofstream fout;
  16. int time_quantum;
  17. class process
  18. {
  19. public:
  20. int p_id;
  21. int p_at;
  22. int p_bt;
  23. int p_rt;
  24. int p_tt;
  25. int p_wt;
  26. int p_pri;
  27. process(int pid,int pat,int pbt,int ppri)
  28. {
  29. p_id=pid;
  30. p_at=pat;
  31. p_bt=pbt;
  32. p_rt=pbt;
  33. p_pri=ppri;
  34. p_tt=0;
  35. p_wt=0;
  36. }
  37. };
  38. int process_intel[MAX][MAX];
  39. string str;
  40. list<process> remaining_process;
  41. int row,approach;
  42. //initialize old current time and new current time
  43. //current time can be updated as minimum of arrival time
  44. int old_current_time,new_current_time;
  45. bool burstime(const process &a, const process &b)
  46. {
  47. return a.p_bt < b.p_bt;
  48. }
  49. bool priority(const process &a, const process &b)
  50. {
  51. return a.p_pri < b.p_pri;
  52. }
  53. process select_next(int approach)
  54. {
  55. //different approaches to follow on users entry
  56. process min_burst_pro(0,0,0,0);
  57. int min;
  58. switch(approach)
  59. {
  60. case 1:
  61. {
  62. //FCFS APPROACH
  63. return remaining_process.front();
  64. }
  65. break;
  66. case 2:
  67. {
  68. //SHORTEST JOB FIRST APPROACH PREMPTIVE
  69. }
  70. case 3:
  71. {
  72. //SHORTEST JOB FIRST APPROACH NON PREMPTIVE
  73. remaining_process.sort(burstime);
  74. return remaining_process.front();
  75. }
  76. break;
  77. case 4:
  78. //PRIORITY PREMPTIVE
  79. case 5:
  80. {
  81. //PRORITY NON PREMPTIVE
  82. remaining_process.sort(priority);
  83. return remaining_process.front();
  84. }
  85. break;
  86. case 6:
  87. //ROUND ROBIN
  88. time_quantum=2;
  89. return remaining_process.front();
  90. break;
  91. }
  92. }
  93. void add_into_remaining_process(int old_current_time,int new_current_time)
  94. {
  95. for(int i=0;i<row;i++)
  96. {
  97. if(process_intel[i][1]>=old_current_time&&process_intel[i][1]<=new_current_time)
  98. {
  99. process p(process_intel[i][0],process_intel[i][1],process_intel[i][2],process_intel[i][3]);
  100. remaining_process.push_back(p);
  101. }
  102. }
  103. }
  104. void removeprocess(int process_to_be_removed)
  105. {
  106. list<process>::iterator it1;
  107. it1=remaining_process.begin();
  108. //advance(it1,process_to_be_removed-1);
  109. advance(it1,0);
  110. remaining_process.erase(it1);
  111. }
  112. void update_the_process_value(int p_id,int p_at,int p_bt,int p_rt,int p_tt,int p_wt,int p_pri)
  113. {
  114. for(list<process>::iterator i=remaining_process.begin(),e=remaining_process.end();i!=e;i++)
  115. {
  116. if(i->p_id == p_id)
  117. {
  118. i->p_id=p_id;
  119. i->p_at=p_at;
  120. i->p_bt=p_bt;
  121. i->p_rt=p_rt;
  122. i->p_tt=p_tt;
  123. i->p_wt=p_wt;
  124. i->p_pri=p_pri;
  125. }
  126. }
  127. }
  128. int main()
  129. {
  130. int flag;
  131. row=0;
  132. approach=1;
  133. char ch;
  134. //declare a matrix and read a file and store the problem in matrix
  135. fin.open("inputsc.txt");
  136. fout.open("output_scheduling.txt");
  137. while(!(fin.eof()))
  138. {
  139. getline(fin,str);
  140. if(!(str.empty()))
  141. {
  142. process_intel[row][0]=(int)str[0]-48;
  143. process_intel[row][1]=(int)str[2]-48;
  144. process_intel[row][2]=(int)str[4]-48;
  145. process_intel[row][3]=(int)str[6]-48;
  146. row++;
  147. }
  148. }
  149. while(approach<=6)
  150. {
  151. old_current_time=0;
  152. new_current_time=0;
  153. add_into_remaining_process(old_current_time,new_current_time);
  154. if(approach==1)
  155. cout<<"FCFS\n";
  156. else if(approach==2)
  157. cout<<"SHORTEST JOB PREMPTIVE\n";
  158. else if(approach==3)
  159. cout<<"SHORTEST JOB FIRST NON PREMPTIVE\n";
  160. else if(approach==4)
  161. cout<<"PRIORITY PREMPTIVE\n";
  162. else if(approach==5)
  163. cout<<"PRIORITY NON PREMPTIVE\n";
  164. else if(approach==6)
  165. cout<<"ROUND ROBIN \n";
  166. //get the remaning process at arrival time zero
  167. while(!(remaining_process.empty()))
  168. {
  169. if(approach==6)//if(the approach used is round robin)
  170. {
  171. process p=select_next(approach);
  172. // if(p.rt<=q)
  173. // {
  174. // ct+=p.rt;
  175. // dequeue newly arrived process
  176. // }
  177. if(p.p_rt<=time_quantum)
  178. {
  179. old_current_time=new_current_time;
  180. new_current_time+=p.p_rt;
  181. p.p_rt-=p.p_rt;
  182. p.p_tt=new_current_time-p.p_at;
  183. p.p_wt=p.p_tt-p.p_bt;
  184. printf("process id:%d arrival time: %d burst time:%d turn around time:%d waiting time:%d\n",p.p_id,p.p_at,p.p_bt,p.p_tt,p.p_wt);
  185. flag=0;
  186. removeprocess(p.p_id);
  187. }
  188. else
  189. {
  190. flag=1;
  191. p.p_rt-=time_quantum;
  192. old_current_time=new_current_time;
  193. new_current_time+=time_quantum;
  194. update_the_process_value(p.p_id,p.p_at,p.p_bt,p.p_rt,p.p_tt,p.p_wt,p.p_pri);
  195. removeprocess(p.p_id);
  196. remaining_process.push_back(p);
  197. }
  198. if(flag==0)
  199. add_into_remaining_process(old_current_time,new_current_time);
  200. else if(flag==1)
  201. add_into_remaining_process(new_current_time,new_current_time);
  202. // else
  203. // {
  204. // ct+=q;
  205. // if(any new process has arrived)
  206. // {
  207. // inqueue it
  208. // }
  209. // }
  210. }
  211. else
  212. {
  213. //select next process to execute;
  214. process p=select_next(approach);
  215. //execute that process
  216. if((approach==2||approach==4)){//APPROACH IS PREMPTIVE
  217. // here increment the new current time one by one
  218. // execute it
  219. // if finished remove it from current process
  220. if(p.p_rt==0)
  221. {
  222. p.p_tt=new_current_time-p.p_at;
  223. p.p_wt=p.p_tt-p.p_bt;
  224. printf("process id:%d arrival time: %d burst time:%d turn around time:%d waiting time:%d\n",p.p_id,p.p_at,p.p_bt,p.p_tt,p.p_wt);
  225. //old_current_time=new_current_time+1;
  226. removeprocess(p.p_id);
  227. }
  228. else
  229. {
  230. new_current_time+=1;
  231. p.p_rt--;
  232. update_the_process_value(p.p_id,p.p_at,p.p_bt,p.p_rt,p.p_tt,p.p_wt,p.p_pri);
  233. add_into_remaining_process(new_current_time,new_current_time);
  234. }
  235. // check any new process has com at new time as there is no interval if yes the add else continue
  236. }
  237. else if((approach==1||approach==3||approach==5))//APPROACH IS NON PREMPTIVE
  238. {
  239. // execute it completely remove it from the remaning process
  240. old_current_time=new_current_time+1;
  241. new_current_time+=p.p_bt;
  242. p.p_rt-=p.p_rt;
  243. p.p_tt=new_current_time-p.p_at;
  244. p.p_wt=p.p_tt-p.p_bt;
  245. //print the status of process after completion
  246. printf("process id:%d arrival time: %d burst time:%d turn around time:%d waiting time:%d\n",p.p_id,p.p_at,p.p_bt,p.p_tt,p.p_wt);
  247. // check for new process between old and new current time add it to queue of remaining process
  248. add_into_remaining_process(old_current_time,new_current_time);
  249. removeprocess(p.p_id);
  250. }
  251. }
  252. }
  253. approach+=1;
  254. }
  255. return 0;
  256. }
  257. /*
  258. OUTPUT:
  259. rishi@rishi-PC:~$ ./a.out
  260. FCFS
  261. process id:1 arrival time: 0 burst time:1 turn around time:1 waiting time:0
  262. process id:2 arrival time: 1 burst time:9 turn around time:9 waiting time:0
  263. process id:3 arrival time: 1 burst time:8 turn around time:17 waiting time:9
  264. process id:4 arrival time: 1 burst time:5 turn around time:22 waiting time:17
  265. SHORTEST JOB PREMPTIVE
  266. process id:1 arrival time: 0 burst time:1 turn around time:1 waiting time:0
  267. process id:4 arrival time: 1 burst time:5 turn around time:5 waiting time:0
  268. process id:3 arrival time: 1 burst time:8 turn around time:13 waiting time:5
  269. process id:2 arrival time: 1 burst time:9 turn around time:22 waiting time:13
  270. SHORTEST JOB FIRST NON PREMPTIVE
  271. process id:1 arrival time: 0 burst time:1 turn around time:1 waiting time:0
  272. process id:4 arrival time: 1 burst time:5 turn around time:5 waiting time:0
  273. process id:3 arrival time: 1 burst time:8 turn around time:13 waiting time:5
  274. process id:2 arrival time: 1 burst time:9 turn around time:22 waiting time:13
  275. PRIORITY PREMPTIVE
  276. process id:3 arrival time: 1 burst time:8 turn around time:8 waiting time:0
  277. process id:1 arrival time: 0 burst time:1 turn around time:9 waiting time:8
  278. process id:2 arrival time: 1 burst time:9 turn around time:17 waiting time:8
  279. process id:4 arrival time: 1 burst time:5 turn around time:22 waiting time:17
  280. PRIORITY NON PREMPTIVE
  281. process id:1 arrival time: 0 burst time:1 turn around time:1 waiting time:0
  282. process id:3 arrival time: 1 burst time:8 turn around time:8 waiting time:0
  283. process id:2 arrival time: 1 burst time:9 turn around time:17 waiting time:8
  284. process id:4 arrival time: 1 burst time:5 turn around time:22 waiting time:17
  285. ROUND ROBIN
  286. process id:1 arrival time: 0 burst time:1 turn around time:1 waiting time:0
  287. process id:4 arrival time: 1 burst time:5 turn around time:17 waiting time:12
  288. process id:3 arrival time: 1 burst time:8 turn around time:21 waiting time:13
  289. process id:2 arrival time: 1 burst time:9 turn around time:22 waiting time:13
  290. */