Leo
Posted on April 20, 2024
Visit: https://dev.to/imnotleo/planificacion-de-procesos-c-3m5k
Esto es un respaldo de: https://dev.to/imnotleo/planificacion-de-procesos-c-3m5k
main
/*
AUTOR: Neftalí Leobardo Vázquez Castillo
CURSO: Sistemas Operativos
PROGRAMA: Planificación de Procesos
FECHA: 19 de Abril del 2024
*/
#include <iostream>
#include "so.hpp"
int main(){
int n, op;
cout << "\n\t Planificacion de procesos - Sistemas Operativos";
cout << "\n\n\tIngrese el numero total de procesos a trabajar: ";
cin >> n;
process so(n);
so.addProcess();
system("cls");
so.print();
do {
so.Menu();
cin >> op;
system("cls");
switch(op){
case 1:
so.FIFO();
break;
case 2:
so.SJF();
break;
case 3:
so.Prioridad();
break;
case 4:
so.RoundRobin();
break;
case 5:
cout << "\n\tRecuerda que puedes volver a calcular cuando gustes." << endl;
break;
default:
cout << "\n\tIngresa una opción valida." << endl;
break;
}
} while (op != 5);
return 0;
}
so.cpp
#include "so.hpp"
process::node::node(char i, int t, int q) {
_id = i;
_time = t;
_priority = q;
_next = nullptr;
}
process::process(int c){
n = c;
head = nullptr;
tail = nullptr;
}
process::~process(){
while(head != nullptr) {
node *p = head;
head = head -> next();
delete p;
}
}
void process::print(){
cout << "\n\t Planificacion de procesos - Sistemas Operativos";
cout << "\n\n\t Procesos ingresados (ID // Tiempo // Prioridad)\n\n" << endl;
cout << "\t";
node *p = head;
while(p != nullptr){
cout << " | " << p -> id() << " " << p -> tme() << " " << p -> prty() << " | ";
p = p -> next();
}
cout << endl;
}
void process::addProcess() {
for(int i = 0; i < n ; i++){
char a;
int b, c;
cout << "ID: ";
cin >> a;
cout << "Tiempo: ";
cin >> b;
cout << "Prioridad: ";
cin >> c;
node *p = new node(a, b, c);
if (empty()) {
head = p;
tail = p;
}else{
tail -> next(p);
tail = p;
}
s++;
cout << "\n\tProceso agregado." << endl;
}
}
void process::Menu() {
cout << "\n\t\tMENU DE PROCESOS\n" << endl;
cout << "\t1. FIFO" << endl;
cout << "\t2. SJF" << endl;
cout << "\t3. Prioridad (Dinamica)" << endl;
cout << "\t4. Round-Robin" << endl;
cout << "\t5. Salir" << endl;
cout << "\n\tIngrese su opcion: ";
}
int process::calcQ() {
int x = 0;
node *p = head;
while(p != nullptr) {
x += p -> tme();
p = p -> next();
}
return x/n;
}
void process::FIFO() {
cout << "\n\t Planificacion de procesos - Sistemas Operativos";
cout << "\n\n\t FIFO (Primera Entrada, Primera Salida)\n" << endl;
node *p = head;
float tR = 0, tRT = 0;
cout << "\n\tId //" << " Tiempo de ejecucion //" << " Tiempo de retorno //"<< " Procesos restantes (ID Tiempo)" << endl;
while (p != nullptr) {
tR += p -> tme();
tRT += tR;
cout << "\t"<< p -> id() << "\t\t"<< p -> tme()<< "\t\t"<< tR<< "\t\t";
node *t = p -> next();
while(t != nullptr){
cout << " | " << t -> id() << " " << t -> tme() << " | ";
t = t -> next();
}
cout << endl;
p = p -> next();
}
cout << "\n\tTiempo promedio: "<< (float)tRT/n<< endl;
}
void process::SJF() {
node *p = head;
while (p != nullptr) {
node *q = p -> next();
while (q != nullptr) {
if (q -> tme() < p -> tme()) {
char idx = p -> id();
int tmex = p -> tme();
int prtyx = p -> prty();
p -> setid(q -> id());
p -> settme(q -> tme());
p -> setprty(q -> prty());
q -> setid(idx);
q -> settme(tmex);
q -> setprty(prtyx);
}
q = q -> next();
}
p = p -> next();
}
cout << "\n\t Planificacion de procesos - Sistemas Operativos";
cout << "\n\n\t SJF (Tiempo mas corto primero)\n" << endl;
node *z = head;
float tR = 0, tRT = 0;
cout << "\n\tId //" << " Tiempo de ejecucion //" << " Tiempo de retorno //"<< " Procesos restantes (ID Tiempo)" << endl;
while (z != nullptr) {
tR += z -> tme();
tRT += tR;
cout << "\t"<< z -> id() << "\t\t"<< z -> tme()<< "\t\t"<< tR<< "\t\t";
node *t = z -> next();
while(t != nullptr){
cout << " | " << t -> id() << " " << t -> tme() << " | ";
t = t -> next();
}
cout << endl;
z = z -> next();
}
cout << "\n\tTiempo promedio: "<< (float)tRT/n<< endl;
}
void process::Prioridad() {
//se ordena por prioridad
node *p = head;
while (p != nullptr) {
node *q = p -> next();
while (q != nullptr) {
if (q -> prty() > p -> prty()) {
char idx = p -> id();
int tmex = p -> tme();
int prtyx = p -> prty();
p -> setid(q -> id());
p -> settme(q -> tme());
p -> setprty(q -> prty());
q -> setid(idx);
q -> settme(tmex);
q -> setprty(prtyx);
}
q = q -> next();
}
p = p -> next();
}
cout << "\n\t Planificacion de procesos - Sistemas Operativos";
cout << "\n\n\t Prioridad Dinamica (Prioridad de mayor a menor usando Quantum)\n" << endl;
node *r = head;
float tR = 0, tRT = 0;
int Q = calcQ();
cout << "\n\tId //" << " Tiempo de ejecucion //" << " Prioridad //" << " Tiempo de retorno //" << " Procesos restantes (ID Tiempo Prioridad)" << endl;
while (r != nullptr) {
if (r -> tme() <= Q) {
tR += r -> tme();
tRT += tR;
cout << "\t"<< r -> id() << "\t\t"<< r -> tme()<< "\t\t"<< r -> prty()<< "\t\t"<< tR<< "\t\t";
node *t = r -> next();
while(t != nullptr){
cout << " | " << t -> id() << " " << t -> tme() << " " << t -> prty() << " | ";
t = t -> next();
}
cout << endl;
r = r -> next();
}else{
tR += Q;
r -> settme(r -> tme() - Q);
r -> setprty(r -> prty() - 1);
cout << "\t"<< r -> id() << " pendiente "<< r -> tme()<< "\t\t"<< r -> prty()<< "\t\t"<< tR<< "\t\t";
node *t2 = r;
while (t2->next() != nullptr && t2->next()->prty() >= r->prty()) {
t2 = t2->next();
}
if (t2 != r) {
node *temp = r->next();
r->next(t2->next());
t2->next(r);
r = temp;
} else {
r = r->next();
}
node *t = r;
while(t != nullptr){
cout << " | " << t -> id() << " " << t -> tme() << " " << t -> prty() << " | ";
t = t -> next();
}
cout << endl;
}
}
cout << "\n\tTiempo promedio: "<< (float)tRT/n<< endl;
cout << "\n\tQuantum: "<< Q<< endl;
}
void process::RoundRobin() {
cout << "\n\t Planificacion de procesos - Sistemas Operativos";
cout << "\n\n\t Round Robin (Primera Entrada, Primera salida usando Quantum)\n" << endl;
node *r = head;
float tR = 0, tRT = 0;
int Q = calcQ();
cout << "\n\tId //" << " Tiempo de ejecucion //" << " Tiempo de retorno //" << " Procesos restantes (ID Tiempo)" << endl;
while (r != nullptr) {
if (r -> tme() <= Q) {
tR += r -> tme();
tRT += tR;
cout << "\t"<< r -> id() << "\t\t"<< r -> tme()<< "\t\t"<< tR<< "\t\t";
node *t = r -> next();
while(t != nullptr){
cout << " | " << t -> id() << " " << t -> tme() << " | ";
t = t -> next();
}
cout << endl;
r = r -> next();
}else{
tR += Q;
r -> settme(r -> tme() - Q);
cout << "\t"<< r -> id() << " pendiente "<< r -> tme()<< "\t\t"<< tR<< "\t\t";
node *t2 = r;
while (t2->next() != nullptr) {
t2 = t2->next();
}
if (t2 != r) {
node *temp = r->next();
r->next(t2->next());
t2->next(r);
r = temp;
} else {
r = r->next();
}
node *t = r;
while(t != nullptr){
std::cout << " | " << t -> id() << " " << t -> tme() << " | ";
t = t -> next();
}
cout << endl;
}
}
cout << "\n\tTiempo promedio: "<< (float)tRT/n<< endl;
cout << "\n\tQuantum: "<< Q<< endl;
}
so.hpp
#ifndef so_hpp
#define so_hpp
#include <iostream>
#include <assert.h>
using namespace std;
class process {
class node {
char _id;
int _time;
int _priority;
node *_next;
public:
node(char i, int t, int q);
char id() const { return _id; } //cual es el id
int tme() const { return _time; } //cual es el tiempo
int prty() const { return _priority; } //cual es la prioridad
node *next() const { return _next; } // cual es el siguiente apuntador
void next(node *p) { _next = p; } // cambia el siguiente ap
//cambiar datos
void setid(char x) { _id = x; }
void settme(int x) { _time = x; }
void setprty(int x) { _priority = x; }
};
int n; // Capacity of process
int s; // Size of process
node *head; // First item in queue
node *tail; // Last item in queue
public:
process(int);
~process();
int capacity() const { return n; }
int size() const { return s; }
bool full() const { return s==n; }
bool empty() const { return s==0; }
void print();
void addProcess();
void Menu();
int calcQ();
void FIFO();
void SJF();
void Prioridad();
void RoundRobin();
};
#endif /* so_hpp */
💖 💪 🙅 🚩
Leo
Posted on April 20, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.