Advertisement
xangelux

Maquina virtual: 1 main.c

Apr 3rd, 2011
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.34 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <ncurses.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include "lib/opreg.h"
  6. #include "lib/opcad.h"
  7. #include "lib/util.h"
  8.  
  9. #define bfkbsize 100
  10. #define prompt "[cipbri@prompt maquina ]$ "
  11. #define timer 3
  12. #define status 10
  13. #define base 60
  14. #define frames 16
  15.  
  16. /*
  17.  * Definición de cantidad de recursos
  18.  */
  19.  
  20. #define R1 15
  21. #define R2 9
  22. #define R3 5
  23. #define R4 20
  24.  
  25. int main( int argc, char **argv ){
  26.  
  27.     /*
  28.      * Declaraciones
  29.      */
  30.  
  31.     char comando[bfkbsize], **ram, *ins_temp;
  32.  
  33.     LISTA listos, finalizados, ejecucion, nuevos, bloqueados, suspendidos;
  34.  
  35.     PROCESO *nuevo = NULL;
  36.  
  37.     int proc = 1, mpid = -1, grps = 0, vel = 200, x = 0, b, **tmm, ff = -1, reuse, *recursos, *recdis;
  38.  
  39.     recursos = (int *) malloc ( sizeof(int) * 4 );
  40.     recdis = (int *) malloc ( sizeof(int) * 4 );
  41.     recursos[0] = recdis[0] = R1;
  42.     recursos[1] = recdis[1] = R2;
  43.     recursos[2] = recdis[2] = R3;
  44.     recursos[3] = recdis[3] = R4;
  45.  
  46.     unsigned long long AX, BX, CX, DX;
  47.  
  48.     /*
  49.      * Inicializaciones
  50.      */
  51.  
  52.     AX = BX = CX = DX = 0x0;
  53.  
  54.     if( inicial(&listos) != 0 || inicial(&finalizados) != 0 || inicial(&ejecucion) != 0 || inicial(&nuevos) != 0 || inicial(&bloqueados) != 0 || inicial(&suspendidos) != 0){
  55.         printf("Alguna de las listas no fue inicializada!!!");
  56.         exit(1);
  57.     }
  58.  
  59.     ram = ( char ** ) malloc ( sizeof( char[100] ) * 32 );
  60.  
  61.     for( x = 0; x < 32; x++ ){
  62.         ram[x] = (char *) malloc( 100 * sizeof( char ) );
  63.         ram[x] = "Vacio";
  64.     }
  65.     comando[0] = '\0';
  66.     tmm = ( int ** ) malloc ( sizeof( int[2] ) * 16 );
  67.  
  68.     for( x = 0; x < 16 ; x++ ){
  69.         tmm[x] = (int *) malloc( sizeof( int ) * 2 );
  70.         tmm[x][0] = tmm[x][1] = 0;
  71.     }
  72.     ins_temp = (char *) malloc ( sizeof( char ) * bfkbsize );
  73.  
  74.     /*
  75.      * Inicio de pantalla Ncurses
  76.      */
  77.  
  78.     initscr();
  79.     move(1,0);printw(" Status: ");
  80.     move(2,0);printw("General: ");
  81.     move(5,0);printw("++-------------------------Registros del CPU------------------------------------------------------------------++-------------Recursos disponibles-------++");
  82.     move(6,0);printw("||    AX   ||    BX    ||    CX    ||    DX    ||    PC    ||    IR          || Nombre          ||   wk       ||     R1   ||   R2    ||   R3   ||   R4  ||");
  83.     move(7,0);printw("||                                                                                                            ||                                        ||");
  84.     move(8,0);printw("++------------------------------------------------------------------------------------------------------------++----------------------------------------++");
  85.     move(10,0);printw("++------------------------------------------------Monitor de Procesos-----------------------------------------------------------------------------------------++");
  86.     move(11,0);printw("|| pid ||    AX    ||    BX    ||    CX    ||    DX    ||   PC   ||    STATUS   ||   Nombre  || Grupo || Prioridad || CPU || GCPU ||fp|| R1 || R2 || R3 || R4 ||");
  87.     move(12,0);printw("++------------------------------------------------------------------------------------------------------------------------------------------------------------++");
  88.     move(0,0);printw("%s",prompt);
  89.     refresh();
  90.  
  91.     while(1){
  92.         if( kbhit() ){
  93.             if( com( comando, getchar(), bfkbsize, prompt, &vel ) != 0 ){
  94.  
  95.                 b = compc( comando );
  96.                
  97.                 /*
  98.                  * Levantar proceso
  99.                  */
  100.  
  101.                 if( b == 0 ){
  102.  
  103.                     nuevo = levanta_proceso(comando,&proc,status, recursos);
  104.  
  105.                     if( nuevo != NULL )
  106.                         ins_list(nuevo, &nuevos);
  107.    
  108.                 /*
  109.                  * Matar proceso
  110.                  */
  111.  
  112.                 }else if( b == 1 ){
  113.                     mpid = atoi( &comando[5] );
  114.                     move(1,status); clrtoeol();
  115.                     if ( mata( mpid, &listos, &finalizados, &ejecucion, &bloqueados, &suspendidos, 0, tmm, ram, &grps, recdis) == 0 )
  116.                         printw("Proceso %d eliminado",mpid);
  117.                     else
  118.                         printw(" Proceso: %d  no existe!",mpid);
  119.                     move(0,strlen(prompt)+strlen(comando));
  120.  
  121.                 /*
  122.                  * Salir
  123.                  */
  124.  
  125.                 }else if( b == 2 ){
  126.  
  127.                     while( listos.inicio != NULL ){
  128.                         nuevo = listos.inicio;
  129.                         move(1,status);
  130.                         clrtoeol();
  131.                         if( fclose(listos.inicio->instrucciones) != 0 )
  132.                             printw("Error al matar el proceso número %d",listos.inicio->pid);
  133.                         else
  134.                             printw("Matando proceso %d",listos.inicio->pid);
  135.                         move(0,strlen(prompt)+strlen(comando));refresh();
  136.                         napms( vel * 2 );
  137.                         if( listos.inicio->siguiente != NULL ){
  138.                             listos.inicio = listos.inicio->siguiente;
  139.                             listos.num--;
  140.                         }
  141.                         else
  142.                             listos.inicio = NULL;
  143.                         free(nuevo);
  144.                     }
  145.                     move(1,status);
  146.                     clrtoeol();
  147.                     printw("Adios");
  148.                     refresh();
  149.                     sleep(1);
  150.                     endwin();
  151.                     exit(0);
  152.  
  153.                 /*
  154.                  * Fork
  155.                  */
  156.  
  157.                 }else if( b == 3 ){
  158.                    
  159.                     nuevo = levanta_hijo(comando,&proc,status,&listos,&ejecucion, recursos, &suspendidos, &bloqueados );
  160.  
  161.                     if( nuevo != NULL )
  162.                         ins_list(nuevo, &nuevos);
  163.                     else{
  164.                         move(1,status);clrtoeol();
  165.                         printw("Proceso padre no existe");
  166.                     }
  167.                 }
  168.                 refresh();
  169.                 comando[0] = '\0';
  170.                 move(0,strlen(prompt));
  171.                 clrtoeol();
  172.                 refresh();
  173.             }
  174.         }else{
  175. /*************************************************************************************************************************************************************************************/
  176. /*          Memoria: levantando un proceso                                                                   */
  177. /*************************************************************************************************************************************************************************************/
  178.             if( nuevos.inicio != NULL && free_frame( tmm ) != -1 ){
  179.                
  180.  
  181.                 /*
  182.                  * Verificar si hay frames libres
  183.                  */
  184.  
  185.                 for( x = 0; x < 3; x++ ){
  186.  
  187.                     ff = free_frame( tmm );/* ff Contiene el indice de TMM que está marcado como libre */
  188.  
  189.                     if( ff == -1 )
  190.                         break;
  191.  
  192.                     ins_temp = lee_ins( nuevos.inicio->instrucciones, 2*x );
  193.                     if( strcmp ( ins_temp, "ERROR" ) == 0 ){
  194.                         if( x == 0 ){
  195.                             move(1,status);printw("Error 1: ");
  196.                             fclose( nuevos.inicio->instrucciones );
  197.                             if ( swap( &nuevos, &finalizados) != 0 )
  198.                                 printw("No se envio a finalizados Error 2: ");
  199.                             printw("Archivo muy pequeño");
  200.                             refresh();sleep(10);
  201.                         }
  202.                         break;
  203.                     }
  204.                     for( reuse = 0; reuse < frames; reuse++ )
  205.                         if( tmm[reuse][0] != 0 )
  206.                             tmm[reuse][0]++;
  207.                     tmm[ff][0] = 1;
  208.                     tmm[ff][1] = nuevos.inicio->pid;
  209.  
  210.                     /* Se llena la primera instrucción del frame libre */
  211.                     ram[ff*2] = ins_temp;
  212.  
  213.                     /* Se marca la tmp del proceso */
  214.                     (nuevos.inicio->tmp)[2*x][0] = 1;
  215.                     (nuevos.inicio->tmp)[2*x][1] = ff;
  216.                    
  217.                     ins_temp = lee_ins( nuevos.inicio->instrucciones, 2*x+1 );
  218.  
  219.                     if( strcmp ( ins_temp, "ERROR" ) == 0 ){
  220.                         x++;
  221.                         break;
  222.                     }
  223.                     (nuevos.inicio->tmp)[2*x+1][0] = 1;
  224.                     (nuevos.inicio->tmp)[2*x+1][1] = ff;
  225.                     ram[ff*2+1] = ins_temp;
  226.                 }
  227.  
  228.                 if( x != 0){
  229.                 /* Hay que checar si no envía de nuevos a listos */
  230.                     if ( swap( &nuevos, &listos ) != 0 ){
  231.                         move(1,status);clrtoeol();
  232.                         printw("Hubo un error al pasar de nuevos a listos");refresh();sleep(2);
  233.                     }else{
  234.                         if( listos.inicio->fork == 0 )
  235.                             grps++;
  236.                         else{
  237.                             nuevo = busca_padre( listos.inicio->ppid, &listos );
  238.                             if( nuevo == NULL )
  239.                                 nuevo = busca_padre( listos.inicio->ppid, &ejecucion );
  240.                             if( nuevo == NULL )
  241.                                 nuevo = busca_padre( listos.inicio->ppid, &suspendidos );
  242.                             if( nuevo == NULL )
  243.                                 nuevo = busca_padre( listos.inicio->ppid, &bloqueados );
  244.  
  245.                             if( nuevo != NULL && nuevo->hijos->inicio == listos.inicio )
  246.                                 grps++;
  247.                         }
  248.                     }
  249.                 }
  250.             }
  251. /*************************************************************************************************************************************************************************************/
  252. /*************************************************************************************************************************************************************************************/
  253.  
  254.             /*
  255.              * Planificar y Ejecutar
  256.              */
  257.                         if( planifica(&listos, &ejecucion, &finalizados, &AX, &BX, &CX, &DX, base, 1.0 / (float) grps ) == 0 ){
  258.                 switch( ejecuta( &AX, &BX, &CX, &DX, &ejecucion, timer, &listos, 1.0 / (float) grps, vel, ram, tmm, &suspendidos, &bloqueados, strlen(comando)+strlen(prompt), recursos, recdis)){
  259.                     case 0:
  260.                         break;
  261.                     case 1:
  262.                         if ( mata( ejecucion.inicio->pid, &listos, &finalizados, &ejecucion, &bloqueados, &suspendidos, 0, tmm, ram, &grps, recdis) == 0 )
  263.                         move(1,10);printw("Proceso %d (%s) Finalizado inesperadamente",finalizados.fin->pid,finalizados.fin->nombre);
  264.                         break;
  265.                     default:
  266.                         if ( mata( ejecucion.inicio->pid, &listos, &finalizados, &ejecucion, &bloqueados, &suspendidos, 0, tmm, ram, &grps, recdis) == 0 )
  267.                         move(1,10);printw("Proceso %d (%s) Finalizo",finalizados.fin->pid,finalizados.fin->nombre);
  268.                         break;
  269.                 }
  270.             }
  271.         }
  272.         bloqueadoschk( &listos, &bloqueados );
  273.         move(3,0);printw("Grupos: %d",grps);
  274.         move(2,status);
  275.         printw("IDLE");
  276.         move(0, strlen(prompt)+strlen(comando));
  277.         refresh();
  278.         if ( imprime(&ejecucion, &listos, &finalizados, &nuevos, &suspendidos, &bloqueados) != 0 ){
  279.             move(1,status);
  280.             printw("Error al imprimir monitor de procesos !!!");
  281.             sleep(3);
  282.             exit(1);
  283.         }
  284.         ram_print( ram );
  285.         tmm_print( tmm );
  286.         move(0, strlen(prompt));
  287.         printw("%s",comando);
  288.         refresh();
  289.     }
  290.     endwin();
  291.     return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement