Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////////FIFO
- #include <iostream>
- using namespace std;
- #define limite 5
- void insertar(char datos[],int tiempo[],int l){
- for(int i = 0; i < (l); i++){
- cout << "inserte el tiempo en el proceso [" << datos[i] << "]: ";
- cin >> tiempo[i];
- system("cls");
- }
- }
- void fifo(char datos[],int tiempo[], int l){
- int tiempoTotal = 0;
- float tiempoReturn = 0.0f;
- insertar(datos,tiempo, l);
- for(int j = 0;j < l; j++){
- tiempoTotal += tiempo[j];
- tiempoReturn += tiempoTotal;
- cout <<"\n""tiempo de retorno de["<<datos[j]<<"]: "<<tiempoTotal<<"\t";
- }
- tiempoReturn = tiempoReturn / l;
- cout<<"\nEl tiempo de las entradas son: "<<tiempoReturn;
- }
- int main(){
- cout<<"\t\t\tSimulacion de FIFO en C++"<<endl;
- cout<<"\t\t_______________________________________\n"<<endl;
- char datos[limite] = {'a','b','c','d','e'};
- int tiempo[limite];
- fifo(datos,tiempo,limite);
- cin.get();
- cin.get();
- return 0;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////// SFJ
- #include<conio.h>
- #include<iostream.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int np, sre, ses, i, b, c;
- float a, pre, pes, s, nM;
- float Tll[50], TS[50], TScop[50], TCo[50], TFi[50], TRe[50], TEs[50];
- void main () {
- cout<<"Ingrese el numero de procesos a planificar: ";
- cin>>np; cout<<endl;
- a=0; sre=0; ses=0;
- for (i=0;i<np;i++){
- cout<<"Ingrese el Tiempo de Llegada del proceso"<<i<<": ";
- cin>>Tll[i]; cout<<endl;
- cout<<"Ingrese el Tiempo de Servicio del proceso"<<i<<": ";
- cin>>TS[i]; cout<<endl;
- }
- nM=TS[0];
- for (i=1;i<np;i++){
- if (TS[i]>nM) nM=TS[i];
- }
- TCo[0]=0;
- TFi[0]=TS[0];
- for (i=0;i<np;i++){
- TScop[i]=TS[i];
- }
- s=0; c=0;
- do{
- b=1;
- for (i=1;i<np;i++){
- if (TScop[b]>TScop[i]){
- a=TScop[i];
- b=i;
- }
- }
- TCo[b]=TFi[c];
- TFi[b]=TCo[b]+TS[b];
- TScop[b]=nM+1;
- c=b;
- s=s+1;
- }while(s<(np-1));
- for (i=0;i<np;i++){
- TRe[i]=TFi[i]-Tll[i];
- sre=sre+TRe[i];
- TEs[i]=TCo[i]-Tll[i];
- ses=ses+TEs[i];
- }
- pre=sre/np;
- pes=ses/np;
- cout<<endl;
- cout<<"Proceso T.Llegada T.Servicio T.Comienzo T.Finalizacion T.Retorno T.Espera"<<endl;
- for (i=0;i<np;i++){
- cout<<" "<<i<<" "<<Tll[i]<<" "<<TS[i]<<" "<<TCo[i]<<" "<<TFi[i]<<" "<<TRe[i]<<" "<<TEs[i]<<endl;
- }
- cout<<"Promedio de Tiempo de Retorno: "<<pre<<endl;
- cout<<"Promedio de Tiempo de Espera: "<<pes<<endl;
- getch();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////ROUND ROBIN
- #include <iostream>
- #define limite 5
- using namespace std;
- void insertar(char datos[], int tiempo[], int numero){
- for(int i = 0; i < (numero); i++){
- cout<<"inserte el tiempo en el proceso ["<<datos[i]<<"]: ";
- cin>>tiempo[i];
- }
- }
- int quantum(int tiempo[],int numero){
- int resultado = 0;
- for(int i = 0; i < numero;++i)
- resultado += tiempo[i];
- resultado /= numero;
- return resultado;
- }
- void RoundRobin(char datos[], int tiempo[], int numero){
- insertar(datos,tiempo, numero);
- int Quantum = quantum(tiempo,numero);
- cout<<"El quantum es: "<< Quantum <<endl;
- int tiempoFinal = 0;
- float sumatoria = 0.0f;
- int metalera = 0;
- int i = 0;
- do{
- tiempo[i] != 0 ? tiempo[i] -= Quantum : ++i;
- if(tiempo[i] > 0)
- tiempoFinal += Quantum;
- else{
- tiempoFinal += Quantum+tiempo[i];
- sumatoria += tiempoFinal;
- cout << "el tiempo de proceso de " << datos[i] << ": "
- << tiempoFinal << endl;
- metalera++;
- }
- i < (numero - 1) ? i++ : i = 0;
- }while(metalera < numero);
- sumatoria /= numero;
- cout << "Tiempo promedio de los procesos es: "
- << sumatoria << endl;
- }
- int main(){
- char datos[limite] = {'a','b','c','d','e'};
- int tiempo[limite];
- RoundRobin(datos,tiempo,limite);
- cin.get();
- cin.get();
- return 0;
- }
- ///////////////////////////////////////////Non preemptive priority
- // C++ implementation for Priority Scheduling with
- //Different Arrival Time priority scheduling
- /*1. sort the processes according to arrival time
- 2. if arrival time is same the acc to priority
- 3. apply fcfs
- */
- #include <bits/stdc++.h>
- using namespace std;
- #define totalprocess 5
- // Making a struct to hold the given input
- struct process
- {
- int at,bt,pr,pno;
- };
- process proc[50];
- /*
- Writing comparator function to sort according to priority if
- arrival time is same
- */
- bool comp(process a,process b)
- {
- if(a.at == b.at)
- {
- return a.pr<b.pr;
- }
- else
- {
- return a.at<b.at;
- }
- }
- // Using FCFS Algorithm to find Waiting time
- void get_wt_time(int wt[])
- {
- // declaring service array that stores cumulative burst time
- int service[50];
- // Initilising initial elements of the arrays
- service[0]=0;
- wt[0]=0;
- for(int i=1;i<totalprocess;i++)
- {
- service[i]=proc[i-1].bt+service[i-1];
- wt[i]=service[i]-proc[i].at+1;
- // If waiting time is negative, change it into zero
- if(wt[i]<0)
- {
- wt[i]=0;
- }
- }
- }
- void get_tat_time(int tat[],int wt[])
- {
- // Filling turnaroundtime array
- for(int i=0;i<totalprocess;i++)
- {
- tat[i]=proc[i].bt+wt[i];
- }
- }
- void findgc()
- {
- //Declare waiting time and turnaround time array
- int wt[50],tat[50];
- double wavg=0,tavg=0;
- // Function call to find waiting time array
- get_wt_time(wt);
- //Function call to find turnaround time
- get_tat_time(tat,wt);
- int stime[50],ctime[50];
- stime[0]=1;
- ctime[0]=stime[0]+tat[0];
- // calculating starting and ending time
- for(int i=1;i<totalprocess;i++)
- {
- stime[i]=ctime[i-1];
- ctime[i]=stime[i]+tat[i]-wt[i];
- }
- cout<<"Process_no\tStart_time\tComplete_time\tTurn_Around_Time\tWaiting_Time"<<endl;
- // display the process details
- for(int i=0;i<totalprocess;i++)
- {
- wavg += wt[i];
- tavg += tat[i];
- cout<<proc[i].pno<<"\t\t"<<
- stime[i]<<"\t\t"<<ctime[i]<<"\t\t"<<
- tat[i]<<"\t\t\t"<<wt[i]<<endl;
- }
- // display the average waiting time
- //and average turn around time
- cout<<"Average waiting time is : ";
- cout<<wavg/(float)totalprocess<<endl;
- cout<<"average turnaround time : ";
- cout<<tavg/(float)totalprocess<<endl;
- }
- int main()
- {
- int arrivaltime[] = { 1, 2, 3, 4, 5 };
- int bursttime[] = { 3, 5, 1, 7, 4 };
- int priority[] = { 3, 4, 1, 7, 8 };
- for(int i=0;i<totalprocess;i++)
- {
- proc[i].at=arrivaltime[i];
- proc[i].bt=bursttime[i];
- proc[i].pr=priority[i];
- proc[i].pno=i+1;
- }
- //Using inbuilt sort function
- sort(proc,proc+totalprocess,comp);
- //Calling function findgc for finding Gantt Chart
- findgc();
- return 0;
- }
- ///////////////////////////////////////////////////////////// Preemptive priority AVANCE
- //C++ Program For PRIORITY WITH PREEMPTIVE Scheduling Algorithm
- #include<iostream>
- #include <conio.h>
- #include <vector>
- using namespace std;
- template <class T>
- class proc
- {
- public:
- T at, bt, wt, tat, p;
- bool active;
- proc(T pbt, T pat, T pp)
- {
- p = pp;
- bt = pbt;
- at = pat;
- wt = tat = 0;
- active = false;
- }
- bool getactive()
- {
- return active;
- }
- T getp()
- {
- return p;
- }
- T getat()
- {
- return at;
- }
- T getbt()
- {
- return bt;
- }
- T getwt()
- {
- return wt;
- }
- T gettat()
- {
- return at;
- }
- void setat(T nat)
- {
- at = nat;
- }
- void setwt(T nwt)
- {
- wt = nwt;
- }
- void setbt(T nbt)
- {
- bt = nbt;
- }
- void setp(T np)
- {
- p = np;
- }
- void setactive(bool nactive)
- {
- active = nactive;
- }
- void settat(T ntat)
- {
- tat = ntat;
- }
- ~proc()
- {
- at = bt = wt = tat = p = 0;
- }
- };
- int main()
- {
- int at1, bt1, wt1, tat1, p1;
- int t = 0;
- int decay = 2; //decays 1 every 2s
- vector<proc<int>>vecproc;
- vector<proc<int>>active;
- int n = 0;
- cout << "Ingrese el numero de procesos" << endl;
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- at1 = bt1 = wt1 = tat1 = p1 = 0;
- cout << "Ingrese el valor de bt del proceso " << i + 1 << endl;
- cin >> bt1;
- cout << "Ingrese el valor de at del proceso " << i + 1 << endl;
- cin >> at1;
- cout << "Ingrese el valor de la prioridad del proceso" << i + 1 << endl;
- cin >> p1;
- vecproc.push_back(proc<int>(bt1, at1, p1));
- }
- loop:
- for (int i = 0; i < vecproc.size(); i++)
- {
- if (vecproc.at(i).at <= t)
- {
- active.push_back(vecproc.at(i));
- }
- }
- for (int i = 0; i < active.size()-1; i++)
- {
- for (int j = i + 1; j < active.size(); j++)
- {
- if (active.at(i).p > active.at(j).p)
- {
- proc<int>aux=active.at(i);
- active.at(i) = active.at(j);
- active.at(j) = aux;
- }
- else if (active.at(i).p == active.at(j).p)
- {
- if (active.at(i).at > active.at(j).at)
- {
- proc<int>aux = active.at(i);
- active.at(i) = active.at(j);
- active.at(j) = aux;
- }
- }
- }
- }
- if (active.empty() == false)
- {
- active.at(0).active = true;
- active.at(0).bt -= 2;
- t += 2;
- }
- for (int i = 0; i < active.size(); i++)
- {
- if (t%decay == 0 && active.at(i).active == false)
- {
- active.at(i).p -= 1;
- }
- }
- for (proc<int>m : active)
- {
- m.active = false;
- }
- _getch();
- return 0;
- }
- ////////////////////////////////////////////////// Round Robin non preemptive
- #include <iostream>
- #include <vector>
- /*at = Arrival time,
- bt = Burst time,
- time_quantum= Quantum time
- tat = Turn around time,
- wt = Waiting time*/
- using namespace std;
- int main(){
- int i,n,time,remain,temps=0,time_quantum;
- int wt=0,tat=0;
- cout<<"Enter the total number of process="<<endl;
- cin>>n;
- remain=n;
- // assigning the number of process to remain variable
- vector<int>at(n);
- vector<int>bt(n);
- vector<int>rt(n);
- //dynamic array declaration using vector method of (STL)
- //STL standard template library of C++
- cout<<"Enter the Arrival time, Burst time for All the processes"<<endl;
- for(i=0;i<n;i++)
- {
- cin>>at[i];
- cin>>bt[i];
- rt[i]=bt[i];
- }
- cout<<"Enter the value of time QUANTUM:"<<endl;
- cin>>time_quantum;
- cout<<"\n\nProcess\t:Turnaround Time:Waiting Time\n\n";
- for(time=0,i=0;remain!=0;)
- {
- if(rt[i]<=time_quantum && rt[i]>0)
- {
- time += rt[i];
- //Addition using shorthand operators
- rt[i]=0;
- temps=1;
- }
- else if(rt[i]>0)
- {
- rt[i] -= time_quantum;
- //Subtraction using shorthand operators
- time += time_quantum;
- //Addition using shorthand operators
- }
- if(rt[i]==0 && temps==1)
- {
- remain--;
- //Desplaying the result of wating, turn around time:
- printf("Process{%d}\t:\t%d\t:\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
- cout<<endl;
- wt += time-at[i]-bt[i];
- tat += time-at[i];
- temps=0;
- }
- if(i == n-1)
- i=0;
- else if(at[i+1] <= time)
- i++;
- else
- i=0;
- }
- cout<<"Average waiting time "<<wt*1.0/n<<endl;
- cout<<"Average turn around time "<<tat*1.0/n<<endl;;
- return 0;
- }
- ///////////////////////////////////////////////////
- //C++ Program to implement Round Robin RRQV
- #include <iostream>
- #include <vector>
- #include <conio.h>
- #include <algorithm>
- using namespace std;
- using namespace System;
- int main() {
- int tq = 0;
- int rq=0, n=0, aux=0, TBT;
- int BT=0 , AT = 0, t=0;
- //ingresar numero de procesos
- cout << "Ingrese el numero de procesos: " << endl;
- cin >> n;
- cout << "Ingresar BT y AT por proceso: " << endl;
- vector<pair<int,int>>v;
- vector<pair<int, int>>s;
- for (int i = 0; i < n; i++)
- {
- cout << "BT de " << i + 1 << endl;
- cin >> BT;
- cout << "AT de " << i + 1 << endl;
- cin >> AT;
- v.push_back(pair<int, int>(BT, AT));
- }
- for (pair<int, int>p : v)
- {
- cout << p.first << ' ' << p.second << endl;
- }
- loop:
- s.clear();
- for (int k = 0; k < v.size(); k++)
- {
- if (v.at(k).second <= t)
- {
- s.push_back(v.at(k));
- }
- }
- //ingresar BT y AT por proceso
- std::sort(s.begin(), s.end());
- n = 0;
- TBT = 0;
- for (pair<int, int>p : s)
- {
- n++;
- TBT += p.first;
- }
- if (n == 0)
- {
- int min = v.at(0).second;
- for (int b = 0; b < v.size(); b++)
- {
- if (v.at(b).second < min)min = v.at(b).second;
- }
- t += min-aux;
- aux += min;
- goto loop;
- }
- tq = TBT / n;
- cout << endl;
- for (pair<int, int>p : s)
- {
- cout << t << ' ';
- if (!(p.first<=tq))
- {
- t += tq;
- for (int q = 0; q < v.size(); q++)
- {
- if (p.first == v.at(q).first)
- {
- v.at(q).first -= tq;
- }
- }
- }
- else
- {
- t += p.first;
- for (int q = 0; q < v.size(); q++)
- {
- if (p.first == v.at(q).first)
- {
- v.erase(v.begin()+q);
- }
- }
- }
- }
- if (v.empty())
- {
- goto end;
- }
- else
- {
- goto loop;
- }
- end:
- cout << endl << t << endl;
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment