Planificación de Procesos (C++)

RMAG news

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 << “nt Planificacion de procesos – Sistemas Operativos”;
cout << “nntIngrese el numero total de procesos a trabajar: “;
while (!(cin >> n)) {
cin.clear();
cin.ignore(100, ‘n’);
system(“cls”);
cout << “ntError: Entrada invalida. ntPor favor, ingrese un numero entero para el total de procesos: “;
}
system(“cls”);
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:
so.print();
break;
case 6:
cout << “ntRecuerda que puedes volver a calcular cuando gustes.” << endl;
break;
default:
cout << “ntIngresa una opción valida.” << endl;
break;
}
} while (op != 6);

return 0;
}

so.cpp

#include “so.hpp”

process::node::node(string 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 << “nt Planificacion de procesos – Sistemas Operativos”;
cout << “nnt Procesos registrados (ID // Tiempo // Prioridad)nn” << 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++){
string 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 << “ntProceso agregado.” << endl;
}
}

void process::Menu() {

cout << “nttMENU DE PROCESOSn” << endl;
cout << “t1. FIFO” << endl;
cout << “t2. SJF” << endl;
cout << “t3. Prioridad (Dinamica)” << endl;
cout << “t4. Round-Robin” << endl;
cout << “t5. Ver procesos registrados” << endl;
cout << “t6. Salir” << endl;
cout << “ntIngrese 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 << “nt Planificacion de procesos – Sistemas Operativos”;
cout << “nnt FIFO (Primera Entrada, Primera Salida)n” << endl;

node *c_head = nullptr;
node *c_tail = nullptr;
node *p = head;

while (p != nullptr) {

node *r = new node(p -> id(), p -> tme(), p -> prty());
if (c_head == nullptr) {
c_head = r;
c_tail = r;
} else {
c_tail -> next(r);
c_tail = r;
}
p = p -> next();
}

float tR = 0, tRT = 0;
cout << “ntId //” << ” Tiempo de ejecucion //” << ” Tiempo de retorno //”<< ” Procesos restantes (ID Tiempo)” << endl;
p = c_head;
while (p != nullptr) {

tR += p -> tme();
tRT += tR;
cout << “t”<< p -> id() << “tt”<< p -> tme()<< “tt”<< tR<< “tt”;
node *t = p -> next();
while(t != nullptr){
cout << ” | ” << t -> id() << ” ” << t -> tme() << ” | “;
t = t -> next();
}
cout << endl;
p = p -> next();
}

p = c_head;
while (p != nullptr) {
node *xd = p;
p = p -> next();
delete xd;
}

cout << “ntTiempo promedio: “<< (float)tRT/n<< endl;
}

void process::SJF() {

node *c_head = nullptr;
node *c_tail = nullptr;
node *p = head;

while (p != nullptr) {

node *r = new node(p -> id(), p -> tme(), p -> prty());
if (c_head == nullptr) {
c_head = r;
c_tail = r;
} else {
c_tail -> next(r);
c_tail = r;
}
p = p -> next();
}
p = c_head;
while (p != nullptr) {
node *q = p -> next();
while (q != nullptr) {

if (q -> tme() < p -> tme()) {

string 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 << “nt Planificacion de procesos – Sistemas Operativos”;
cout << “nnt SJF (Tiempo mas corto primero)n” << endl;
node *z = c_head;
float tR = 0, tRT = 0;
cout << “ntId //” << ” Tiempo de ejecucion //” << ” Tiempo de retorno //”<< ” Procesos restantes (ID Tiempo)” << endl;
while (z != nullptr) {

tR += z -> tme();
tRT += tR;
cout << “t”<< z -> id() << “tt”<< z -> tme()<< “tt”<< tR<< “tt”;
node *t = z -> next();
while(t != nullptr){
cout << ” | ” << t -> id() << ” ” << t -> tme() << ” | “;
t = t -> next();
}
cout << endl;
z = z -> next();
}

p = c_head;
while (p != nullptr) {
node *xd = p;
p = p -> next();
delete xd;
}
cout << “ntTiempo promedio: “<< (float)tRT/n<< endl;
}

void process::Prioridad() {

node *c_head = nullptr;
node *c_tail = nullptr;
node *p = head;

while (p != nullptr) {

node *r = new node(p -> id(), p -> tme(), p -> prty());
if (c_head == nullptr) {
c_head = r;
c_tail = r;
} else {
c_tail -> next(r);
c_tail = r;
}
p = p -> next();
}
//se ordena por prioridad
p = c_head;
while (p != nullptr) {
node *q = p -> next();
while (q != nullptr) {

if (q -> prty() > p -> prty()) {

string 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 << “nt Planificacion de procesos – Sistemas Operativos”;
cout << “nnt Prioridad Dinamica (Prioridad de mayor a menor usando Quantum)n” << endl;

node *r = c_head;
float tR = 0, tRT = 0;
int Q = calcQ();
cout << “ntId //” << ” 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() << “tt”<< r -> tme()<< “tt”<< r -> prty()<< “tt”<< tR<< “tt”;
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()<< “tt”<< r -> prty()<< “tt”<< tR<< “tt”;

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;
}
}

p = c_head;
while (p != nullptr) {
node *xd = p;
p = p -> next();
delete xd;
}
cout << “ntTiempo promedio: “<< (float)tRT/n<< endl;
cout << “ntQuantum: “<< Q<< endl;
}

void process::RoundRobin() {

cout << “nt Planificacion de procesos – Sistemas Operativos”;
cout << “nnt Round Robin (Primera Entrada, Primera salida usando Quantum)n” << endl;

node *c_head = nullptr;
node *c_tail = nullptr;
node *r = head;

while (r != nullptr) {

node *m = new node(r -> id(), r -> tme(), r -> prty());
if (c_head == nullptr) {
c_head = m;
c_tail = m;
} else {
c_tail -> next(m);
c_tail = m;
}
r = r -> next();
}
r = c_head;
float tR = 0, tRT = 0;
int Q = calcQ();
cout << “ntId //” << ” 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() << “tt”<< r -> tme()<< “tt”<< tR<< “tt”;
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()<< “tt”<< tR<< “tt”;

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;
}
}

r = c_head;
while (r != nullptr) {
node *xd = r;
r = r -> next();
delete xd;
}
cout << “ntTiempo promedio: “<< (float)tRT/n<< endl;
cout << “ntQuantum: “<< Q<< endl;
}

so.hpp

#ifndef so_hpp
#define so_hpp

#include <iostream>
#include <assert.h>
using namespace std;

class process {

class node {

string _id;
int _time;
int _priority;
node *_next;

public:

node(string i, int t, int q);

string 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(string 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 */

Leave a Reply

Your email address will not be published. Required fields are marked *