Xavistian

Arqui-Scheduling algos

Nov 5th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.91 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////FIFO
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. #define limite 5
  7. void insertar(char datos[],int tiempo[],int l){
  8. for(int i = 0; i < (l); i++){
  9. cout << "inserte el tiempo en el proceso [" << datos[i] << "]: ";
  10. cin >> tiempo[i];
  11. system("cls");
  12. }
  13. }
  14. void fifo(char datos[],int tiempo[], int l){
  15. int tiempoTotal = 0;
  16. float tiempoReturn = 0.0f;
  17. insertar(datos,tiempo, l);
  18. for(int j = 0;j < l; j++){
  19. tiempoTotal += tiempo[j];
  20. tiempoReturn += tiempoTotal;
  21. cout <<"\n""tiempo de retorno de["<<datos[j]<<"]: "<<tiempoTotal<<"\t";
  22. }
  23. tiempoReturn = tiempoReturn / l;
  24. cout<<"\nEl tiempo de las entradas son: "<<tiempoReturn;
  25. }
  26. int main(){
  27. cout<<"\t\t\tSimulacion de FIFO en C++"<<endl;
  28. cout<<"\t\t_______________________________________\n"<<endl;
  29. char datos[limite] = {'a','b','c','d','e'};
  30. int tiempo[limite];
  31. fifo(datos,tiempo,limite);
  32. cin.get();
  33. cin.get();
  34. return 0;
  35. }
  36.  
  37. ////////////////////////////////////////////////////////////////////////////////////////////////////// SFJ
  38.  
  39. #include<conio.h>
  40. #include<iostream.h>
  41. #include<stdio.h>
  42. #include<stdlib.h>
  43. #include<string.h>
  44. int np, sre, ses, i, b, c;
  45. float a, pre, pes, s, nM;
  46. float Tll[50], TS[50], TScop[50], TCo[50], TFi[50], TRe[50], TEs[50];
  47. void main () {
  48. cout<<"Ingrese el numero de procesos a planificar: ";
  49. cin>>np; cout<<endl;
  50. a=0; sre=0; ses=0;
  51. for (i=0;i<np;i++){
  52. cout<<"Ingrese el Tiempo de Llegada del proceso"<<i<<": ";
  53. cin>>Tll[i]; cout<<endl;
  54. cout<<"Ingrese el Tiempo de Servicio del proceso"<<i<<": ";
  55. cin>>TS[i]; cout<<endl;
  56. }
  57. nM=TS[0];
  58. for (i=1;i<np;i++){
  59. if (TS[i]>nM) nM=TS[i];
  60. }
  61. TCo[0]=0;
  62. TFi[0]=TS[0];
  63. for (i=0;i<np;i++){
  64. TScop[i]=TS[i];
  65. }
  66. s=0; c=0;
  67. do{
  68. b=1;
  69. for (i=1;i<np;i++){
  70. if (TScop[b]>TScop[i]){
  71. a=TScop[i];
  72. b=i;
  73. }
  74. }
  75. TCo[b]=TFi[c];
  76. TFi[b]=TCo[b]+TS[b];
  77. TScop[b]=nM+1;
  78. c=b;
  79. s=s+1;
  80. }while(s<(np-1));
  81. for (i=0;i<np;i++){
  82. TRe[i]=TFi[i]-Tll[i];
  83. sre=sre+TRe[i];
  84. TEs[i]=TCo[i]-Tll[i];
  85. ses=ses+TEs[i];
  86. }
  87. pre=sre/np;
  88. pes=ses/np;
  89. cout<<endl;
  90. cout<<"Proceso T.Llegada T.Servicio T.Comienzo T.Finalizacion T.Retorno T.Espera"<<endl;
  91. for (i=0;i<np;i++){
  92. cout<<" "<<i<<" "<<Tll[i]<<" "<<TS[i]<<" "<<TCo[i]<<" "<<TFi[i]<<" "<<TRe[i]<<" "<<TEs[i]<<endl;
  93. }
  94. cout<<"Promedio de Tiempo de Retorno: "<<pre<<endl;
  95. cout<<"Promedio de Tiempo de Espera: "<<pes<<endl;
  96. getch();
  97. }
  98. ////////////////////////////////////////////////////////////////////////////////////////////////////////ROUND ROBIN
  99. #include <iostream>
  100. #define limite 5
  101. using namespace std;
  102.  
  103. void insertar(char datos[], int tiempo[], int numero){
  104. for(int i = 0; i < (numero); i++){
  105. cout<<"inserte el tiempo en el proceso ["<<datos[i]<<"]: ";
  106. cin>>tiempo[i];
  107. }
  108. }
  109.  
  110. int quantum(int tiempo[],int numero){
  111. int resultado = 0;
  112. for(int i = 0; i < numero;++i)
  113. resultado += tiempo[i];
  114. resultado /= numero;
  115. return resultado;
  116. }
  117.  
  118. void RoundRobin(char datos[], int tiempo[], int numero){
  119. insertar(datos,tiempo, numero);
  120. int Quantum = quantum(tiempo,numero);
  121. cout<<"El quantum es: "<< Quantum <<endl;
  122. int tiempoFinal = 0;
  123. float sumatoria = 0.0f;
  124. int metalera = 0;
  125. int i = 0;
  126. do{
  127. tiempo[i] != 0 ? tiempo[i] -= Quantum : ++i;
  128. if(tiempo[i] > 0)
  129. tiempoFinal += Quantum;
  130. else{
  131. tiempoFinal += Quantum+tiempo[i];
  132. sumatoria += tiempoFinal;
  133. cout << "el tiempo de proceso de " << datos[i] << ": "
  134. << tiempoFinal << endl;
  135. metalera++;
  136. }
  137. i < (numero - 1) ? i++ : i = 0;
  138. }while(metalera < numero);
  139. sumatoria /= numero;
  140. cout << "Tiempo promedio de los procesos es: "
  141. << sumatoria << endl;
  142. }
  143.  
  144.  
  145. int main(){
  146. char datos[limite] = {'a','b','c','d','e'};
  147. int tiempo[limite];
  148. RoundRobin(datos,tiempo,limite);
  149. cin.get();
  150. cin.get();
  151. return 0;
  152. }
  153. ///////////////////////////////////////////Non preemptive priority
  154. // C++ implementation for Priority Scheduling with
  155. //Different Arrival Time priority scheduling
  156. /*1. sort the processes according to arrival time
  157. 2. if arrival time is same the acc to priority
  158. 3. apply fcfs
  159. */
  160.  
  161. #include <bits/stdc++.h>
  162.  
  163. using namespace std;
  164.  
  165. #define totalprocess 5
  166.  
  167. // Making a struct to hold the given input
  168.  
  169. struct process
  170. {
  171. int at,bt,pr,pno;
  172. };
  173.  
  174. process proc[50];
  175.  
  176. /*
  177. Writing comparator function to sort according to priority if
  178. arrival time is same
  179. */
  180.  
  181. bool comp(process a,process b)
  182. {
  183. if(a.at == b.at)
  184. {
  185. return a.pr<b.pr;
  186. }
  187. else
  188. {
  189. return a.at<b.at;
  190. }
  191. }
  192.  
  193. // Using FCFS Algorithm to find Waiting time
  194. void get_wt_time(int wt[])
  195. {
  196. // declaring service array that stores cumulative burst time
  197. int service[50];
  198.  
  199. // Initilising initial elements of the arrays
  200. service[0]=0;
  201. wt[0]=0;
  202.  
  203.  
  204. for(int i=1;i<totalprocess;i++)
  205. {
  206. service[i]=proc[i-1].bt+service[i-1];
  207.  
  208. wt[i]=service[i]-proc[i].at+1;
  209.  
  210. // If waiting time is negative, change it into zero
  211.  
  212. if(wt[i]<0)
  213. {
  214. wt[i]=0;
  215. }
  216. }
  217.  
  218. }
  219.  
  220. void get_tat_time(int tat[],int wt[])
  221. {
  222. // Filling turnaroundtime array
  223.  
  224. for(int i=0;i<totalprocess;i++)
  225. {
  226. tat[i]=proc[i].bt+wt[i];
  227. }
  228.  
  229. }
  230.  
  231. void findgc()
  232. {
  233. //Declare waiting time and turnaround time array
  234. int wt[50],tat[50];
  235.  
  236. double wavg=0,tavg=0;
  237.  
  238. // Function call to find waiting time array
  239. get_wt_time(wt);
  240. //Function call to find turnaround time
  241. get_tat_time(tat,wt);
  242.  
  243. int stime[50],ctime[50];
  244. stime[0]=1;
  245. ctime[0]=stime[0]+tat[0];
  246. // calculating starting and ending time
  247. for(int i=1;i<totalprocess;i++)
  248. {
  249. stime[i]=ctime[i-1];
  250. ctime[i]=stime[i]+tat[i]-wt[i];
  251. }
  252.  
  253. cout<<"Process_no\tStart_time\tComplete_time\tTurn_Around_Time\tWaiting_Time"<<endl;
  254.  
  255. // display the process details
  256.  
  257. for(int i=0;i<totalprocess;i++)
  258. {
  259. wavg += wt[i];
  260. tavg += tat[i];
  261.  
  262. cout<<proc[i].pno<<"\t\t"<<
  263. stime[i]<<"\t\t"<<ctime[i]<<"\t\t"<<
  264. tat[i]<<"\t\t\t"<<wt[i]<<endl;
  265. }
  266.  
  267. // display the average waiting time
  268. //and average turn around time
  269.  
  270. cout<<"Average waiting time is : ";
  271. cout<<wavg/(float)totalprocess<<endl;
  272. cout<<"average turnaround time : ";
  273. cout<<tavg/(float)totalprocess<<endl;
  274.  
  275. }
  276.  
  277. int main()
  278. {
  279. int arrivaltime[] = { 1, 2, 3, 4, 5 };
  280. int bursttime[] = { 3, 5, 1, 7, 4 };
  281. int priority[] = { 3, 4, 1, 7, 8 };
  282.  
  283. for(int i=0;i<totalprocess;i++)
  284. {
  285. proc[i].at=arrivaltime[i];
  286. proc[i].bt=bursttime[i];
  287. proc[i].pr=priority[i];
  288. proc[i].pno=i+1;
  289. }
  290.  
  291. //Using inbuilt sort function
  292.  
  293. sort(proc,proc+totalprocess,comp);
  294.  
  295. //Calling function findgc for finding Gantt Chart
  296.  
  297. findgc();
  298.  
  299. return 0;
  300. }
  301. ///////////////////////////////////////////////////////////// Preemptive priority AVANCE
  302. //C++ Program For PRIORITY WITH PREEMPTIVE Scheduling Algorithm
  303. #include<iostream>
  304. #include <conio.h>
  305. #include <vector>
  306. using namespace std;
  307.  
  308. template <class T>
  309. class proc
  310. {
  311. public:
  312. T at, bt, wt, tat, p;
  313. bool active;
  314. proc(T pbt, T pat, T pp)
  315. {
  316. p = pp;
  317. bt = pbt;
  318. at = pat;
  319. wt = tat = 0;
  320. active = false;
  321. }
  322. bool getactive()
  323. {
  324. return active;
  325. }
  326. T getp()
  327. {
  328. return p;
  329. }
  330. T getat()
  331. {
  332. return at;
  333. }
  334. T getbt()
  335. {
  336. return bt;
  337. }
  338. T getwt()
  339. {
  340. return wt;
  341. }
  342. T gettat()
  343. {
  344. return at;
  345. }
  346. void setat(T nat)
  347. {
  348. at = nat;
  349. }
  350. void setwt(T nwt)
  351. {
  352. wt = nwt;
  353. }
  354. void setbt(T nbt)
  355. {
  356. bt = nbt;
  357. }
  358. void setp(T np)
  359. {
  360. p = np;
  361. }
  362. void setactive(bool nactive)
  363. {
  364. active = nactive;
  365. }
  366. void settat(T ntat)
  367. {
  368. tat = ntat;
  369. }
  370. ~proc()
  371. {
  372. at = bt = wt = tat = p = 0;
  373. }
  374. };
  375.  
  376. int main()
  377. {
  378. int at1, bt1, wt1, tat1, p1;
  379. int t = 0;
  380. int decay = 2; //decays 1 every 2s
  381. vector<proc<int>>vecproc;
  382. vector<proc<int>>active;
  383. int n = 0;
  384. cout << "Ingrese el numero de procesos" << endl;
  385. cin >> n;
  386. for (int i = 0; i < n; i++)
  387. {
  388. at1 = bt1 = wt1 = tat1 = p1 = 0;
  389. cout << "Ingrese el valor de bt del proceso " << i + 1 << endl;
  390. cin >> bt1;
  391. cout << "Ingrese el valor de at del proceso " << i + 1 << endl;
  392. cin >> at1;
  393. cout << "Ingrese el valor de la prioridad del proceso" << i + 1 << endl;
  394. cin >> p1;
  395. vecproc.push_back(proc<int>(bt1, at1, p1));
  396. }
  397. loop:
  398. for (int i = 0; i < vecproc.size(); i++)
  399. {
  400. if (vecproc.at(i).at <= t)
  401. {
  402. active.push_back(vecproc.at(i));
  403. }
  404. }
  405. for (int i = 0; i < active.size()-1; i++)
  406. {
  407. for (int j = i + 1; j < active.size(); j++)
  408. {
  409. if (active.at(i).p > active.at(j).p)
  410. {
  411. proc<int>aux=active.at(i);
  412. active.at(i) = active.at(j);
  413. active.at(j) = aux;
  414. }
  415. else if (active.at(i).p == active.at(j).p)
  416. {
  417. if (active.at(i).at > active.at(j).at)
  418. {
  419. proc<int>aux = active.at(i);
  420. active.at(i) = active.at(j);
  421. active.at(j) = aux;
  422. }
  423. }
  424. }
  425. }
  426. if (active.empty() == false)
  427. {
  428. active.at(0).active = true;
  429. active.at(0).bt -= 2;
  430. t += 2;
  431. }
  432. for (int i = 0; i < active.size(); i++)
  433. {
  434. if (t%decay == 0 && active.at(i).active == false)
  435. {
  436. active.at(i).p -= 1;
  437. }
  438. }
  439. for (proc<int>m : active)
  440. {
  441. m.active = false;
  442. }
  443.  
  444. _getch();
  445. return 0;
  446. }
  447. ////////////////////////////////////////////////// Round Robin non preemptive
  448. #include <iostream>
  449. #include <vector>
  450.  
  451. /*at = Arrival time,
  452. bt = Burst time,
  453. time_quantum= Quantum time
  454. tat = Turn around time,
  455. wt = Waiting time*/
  456.  
  457. using namespace std;
  458.  
  459. int main(){
  460. int i,n,time,remain,temps=0,time_quantum;
  461.  
  462. int wt=0,tat=0;
  463.  
  464. cout<<"Enter the total number of process="<<endl;
  465. cin>>n;
  466.  
  467. remain=n;
  468. // assigning the number of process to remain variable
  469.  
  470. vector<int>at(n);
  471. vector<int>bt(n);
  472. vector<int>rt(n);
  473. //dynamic array declaration using vector method of (STL)
  474. //STL standard template library of C++
  475.  
  476. cout<<"Enter the Arrival time, Burst time for All the processes"<<endl;
  477. for(i=0;i<n;i++)
  478. {
  479. cin>>at[i];
  480. cin>>bt[i];
  481. rt[i]=bt[i];
  482. }
  483.  
  484. cout<<"Enter the value of time QUANTUM:"<<endl;
  485. cin>>time_quantum;
  486.  
  487. cout<<"\n\nProcess\t:Turnaround Time:Waiting Time\n\n";
  488. for(time=0,i=0;remain!=0;)
  489. {
  490. if(rt[i]<=time_quantum && rt[i]>0)
  491. {
  492. time += rt[i];
  493. //Addition using shorthand operators
  494. rt[i]=0;
  495. temps=1;
  496. }
  497.  
  498. else if(rt[i]>0)
  499. {
  500. rt[i] -= time_quantum;
  501. //Subtraction using shorthand operators
  502. time += time_quantum;
  503. //Addition using shorthand operators
  504. }
  505.  
  506. if(rt[i]==0 && temps==1)
  507. {
  508. remain--;
  509. //Desplaying the result of wating, turn around time:
  510. printf("Process{%d}\t:\t%d\t:\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
  511. cout<<endl;
  512.  
  513. wt += time-at[i]-bt[i];
  514. tat += time-at[i];
  515. temps=0;
  516. }
  517.  
  518. if(i == n-1)
  519. i=0;
  520. else if(at[i+1] <= time)
  521. i++;
  522. else
  523. i=0;
  524. }
  525.  
  526. cout<<"Average waiting time "<<wt*1.0/n<<endl;
  527. cout<<"Average turn around time "<<tat*1.0/n<<endl;;
  528.  
  529. return 0;
  530. }
  531.  
  532. ///////////////////////////////////////////////////
  533. //C++ Program to implement Round Robin RRQV
  534. #include <iostream>
  535. #include <vector>
  536. #include <conio.h>
  537. #include <algorithm>
  538.  
  539. using namespace std;
  540. using namespace System;
  541.  
  542. int main() {
  543. int tq = 0;
  544. int rq=0, n=0, aux=0, TBT;
  545. int BT=0 , AT = 0, t=0;
  546. //ingresar numero de procesos
  547. cout << "Ingrese el numero de procesos: " << endl;
  548. cin >> n;
  549. cout << "Ingresar BT y AT por proceso: " << endl;
  550. vector<pair<int,int>>v;
  551. vector<pair<int, int>>s;
  552. for (int i = 0; i < n; i++)
  553. {
  554. cout << "BT de " << i + 1 << endl;
  555. cin >> BT;
  556. cout << "AT de " << i + 1 << endl;
  557. cin >> AT;
  558. v.push_back(pair<int, int>(BT, AT));
  559. }
  560. for (pair<int, int>p : v)
  561. {
  562. cout << p.first << ' ' << p.second << endl;
  563. }
  564. loop:
  565. s.clear();
  566. for (int k = 0; k < v.size(); k++)
  567. {
  568. if (v.at(k).second <= t)
  569. {
  570. s.push_back(v.at(k));
  571. }
  572. }
  573. //ingresar BT y AT por proceso
  574. std::sort(s.begin(), s.end());
  575. n = 0;
  576. TBT = 0;
  577. for (pair<int, int>p : s)
  578. {
  579. n++;
  580. TBT += p.first;
  581. }
  582. if (n == 0)
  583. {
  584. int min = v.at(0).second;
  585. for (int b = 0; b < v.size(); b++)
  586. {
  587. if (v.at(b).second < min)min = v.at(b).second;
  588. }
  589. t += min-aux;
  590. aux += min;
  591. goto loop;
  592. }
  593. tq = TBT / n;
  594. cout << endl;
  595. for (pair<int, int>p : s)
  596. {
  597. cout << t << ' ';
  598. if (!(p.first<=tq))
  599. {
  600. t += tq;
  601. for (int q = 0; q < v.size(); q++)
  602. {
  603. if (p.first == v.at(q).first)
  604. {
  605. v.at(q).first -= tq;
  606. }
  607. }
  608. }
  609. else
  610. {
  611. t += p.first;
  612. for (int q = 0; q < v.size(); q++)
  613. {
  614. if (p.first == v.at(q).first)
  615. {
  616.  
  617. v.erase(v.begin()+q);
  618. }
  619. }
  620. }
  621. }
  622. if (v.empty())
  623. {
  624. goto end;
  625. }
  626. else
  627. {
  628. goto loop;
  629. }
  630. end:
  631. cout << endl << t << endl;
  632. _getch();
  633. return 0;
  634. }
Advertisement
Add Comment
Please, Sign In to add comment