DEV Community

Leo
Leo

Posted on • Edited on

so xd

check out: https://dev.to/imnotleo/planificacion-de-procesos-c-3m5k

mas detalles en: https://dev.to/imnotleo/planificacion-de-procesos-c-3m5k


main

/*
AUTOR:    Leobardo Vázquez
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 Planificación de procesos - Sistemas Operativos";
    cout << "\n\n\tIngrese el número total de procesos a trabajar: ";
    cin >> n;
    process so(n);
    so.addProcess();
    system("cls");
    do {
    so.Menu();
    cin >> op;
    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;
}
Enter fullscreen mode Exit fullscreen mode

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;
      s = 0;
      head = nullptr;
      tail = nullptr;
  }

  process::~process(){

      while(head != nullptr) {

          node *p = head;
          head = head -> next();
          delete p;
      }
  }

  void process::push(char i, int t, int q){

      assert(!full());
    node *p = new node(i, t, q);
    if(empty()) {
        head = p;
        tail = p;
      }else{
        tail -> next(p);
        tail = p;
    }
    s++;
  }

  void process::pop(){

      assert(!empty());
          char i = head -> id();
          int t = head -> tme();
          int q = head -> prty();

          node *p = head;
          head = p -> next(); 
          delete p;
          s--;
          //return x;
  }

  void process::print(){

      node *p = head;
      while(p != nullptr){
      std::cout << " | " << p -> id() << " " << p -> tme() << " " << p -> prty() << " | ";
      p = p -> next();
      }
}



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\tMENU DE PROCESOS" << endl;
     cout << "1. Calcular FIFO" << endl;
     cout << "2. Calcular SJF" << endl;
     cout << "3. Calcular prioridad" << endl;
     cout << "4. Calcular Round-Robin" << endl;
     cout << "5. Salir" << endl;
     cout << "Ingrese 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() {

      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){
      std::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();
      }

      FIFO();
  }

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


      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){
      std::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){
      std::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() {

      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){
      std::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;

  }

Enter fullscreen mode Exit fullscreen mode

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();

  void push(char, int, int);
  void pop(void);

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

Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs