Advertisement
xangelux

Maquina virtual: 1 util.c

Apr 3rd, 2011
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 42.69 KB | None | 0 0
  1. #include <termios.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "lib/util.h"
  8. #include "lib/opcad.h"
  9. #include <ncurses.h>
  10. #include "lib/opreg.h"
  11. #include <time.h>
  12.  
  13. #define linesize 100
  14. #define insize 23
  15.  
  16. int kbhit(void){
  17.  
  18.   struct termios oldt, newt;
  19.   int ch;
  20.   int oldf;
  21.  
  22.   tcgetattr(STDIN_FILENO, &oldt);
  23.   newt = oldt;
  24.   newt.c_lflag &= ~(ICANON | ECHO);
  25.   tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  26.   oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  27.   fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  28.  
  29.   ch = getchar();
  30.  
  31.   tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  32.   fcntl(STDIN_FILENO, F_SETFL, oldf);
  33.  
  34.   if(ch != EOF){
  35.  
  36.     ungetc(ch, stdin);
  37.     return 1;
  38.   }
  39.  
  40.   return 0;
  41. }
  42.  
  43. int com(char *comando, char c, int bfkbsize, char *prompt, int *vel){
  44.  
  45.     /*
  46.      * Verificando si se teclea Enter
  47.      */
  48.  
  49.     if( c != 13 ){
  50.         if( c == 27 ){
  51.             c = getchar();
  52.             if( c == 91 ){
  53.                 c = getchar();
  54.                 if( c == 68 ){
  55.                     if( *vel < 1000 )
  56.                         *vel += 50;
  57.                     return 0;
  58.                 }else if( c == 67 ){
  59.                     if( *vel > 0 )
  60.                         *vel -= 50;
  61.                     return 0;
  62.                 }
  63.             }
  64.         }else if( c == 16 ){
  65.                 move(1,10);clrtoeol();printw("PAUSA");move(0,strlen(prompt)+strlen(comando));refresh();
  66.                 while(1){
  67.                     noecho();
  68.                     if( kbhit() ){
  69.                         if( getchar() == 16 )
  70.                             break;
  71.                     }
  72.                 }
  73.                 move(0,0);clrtoeol();printw("%s %s",prompt,comando);
  74.                 move(1,10);printw("CONTINUANDO");move(0,strlen(prompt)+strlen(comando));refresh();
  75.         }
  76.         if( strlen(comando) >= bfkbsize ){
  77.             printw("%c",beep());
  78.             comando[bfkbsize-1] = '\0';
  79.         }else{
  80.     /*
  81.      * Verificando si no es Delete
  82.      */
  83.             if( c != 127 && c != 16 ){
  84.                 armaComando( comando,c );
  85.                 printw("%c",c);
  86.             }
  87.             else{
  88.                 if( strlen( comando ) == 0 ){
  89.                     printw("%c",beep());
  90.                 }else{
  91.                     noecho();
  92.                     comando[strlen(comando)-1] = '\0';
  93.                     move(0,0);
  94.                     clrtoeol();
  95.                     printw("%s%s",prompt,comando);
  96.                 }
  97.             }
  98.         }
  99.         refresh();
  100.         return 0;
  101.     }
  102.     if( strlen(comando) == 0){
  103.         printw("%c",beep());
  104.         refresh();
  105.         return 0;
  106.     }
  107.     return 1;
  108. }
  109.  
  110. int compc(char entrada[]){
  111.  
  112.     int i = 0, j = 0,k;
  113.     int n = strlen(entrada);
  114.     char aux[n+2], aux0[n+1];
  115.     for (k = 0; k<n;k++)
  116.         aux[k]=entrada[k];
  117.     aux[n]=' ';
  118.     aux[n+1]='\0';
  119.     while(aux[i]==' ')
  120.         i++;
  121.     while(aux[i]!=' '){
  122.         aux0[j]=aux[i];
  123.         i++;
  124.         j++;
  125.     }
  126.     aux0[j]='\0';
  127.     for (k = 0; k<strlen(aux0);k++)
  128.         entrada[k] = aux0[k];
  129.     if (compaux(aux0)==1){
  130.         entrada[k] = ' ';
  131.         while(aux[i]==' ')
  132.         {
  133.             if (i==n)
  134.                 return 0;
  135.             i++;
  136.         }      
  137.         while(aux[i]!=' ')
  138.         {
  139.             if (i==n)
  140.                 return 0;
  141.             if (aux[i]>47 && aux[i]<58)
  142.             {
  143.                 entrada[k+1]=aux[i];
  144.                 i++;
  145.                 k++;
  146.             }
  147.             else
  148.                 return 0;
  149.         }
  150.         entrada[k+1]='\0';
  151.         return 1;
  152.     }
  153.     else if (compaux(aux0)==2)
  154.     {
  155.         entrada[k] = '\0';
  156.         return 2;
  157.     }
  158.     else if (compaux(aux0)==3){
  159.         entrada[k] = ' ';
  160.         while(aux[i]==' ')
  161.         {
  162.             if (i==n)
  163.                 return 0;
  164.             i++;
  165.         }      
  166.         while(aux[i]!=' ')
  167.         {
  168.             if (i==n)
  169.                 return 0;
  170.             if (aux[i]>47 && aux[i]<58)
  171.             {
  172.                 entrada[k+1]=aux[i];
  173.                 i++;
  174.                 k++;
  175.             }
  176.             else
  177.                 return 0;
  178.         }
  179.         i++;
  180.         k++;
  181.         if ( aux[i] == '\0' )
  182.             return 3;
  183.         while( aux[i] != ' ' ){
  184.             if(i==n)
  185.                 return 0;
  186.             if (aux[i]>47 && aux[i]<58){
  187.                 entrada[k+1]=aux[i];
  188.                 i++;k++;
  189.             }
  190.             else
  191.                 return 0;
  192.         }          
  193.         entrada[k+1]='\0';
  194.         return 3;
  195.     }
  196.     else   
  197.     {
  198.         entrada[k] = '\0';
  199.         return 0;  
  200.     }
  201. }
  202.  
  203. /*
  204.  * Funcion compaux
  205.  * Funcion auxiliar de la funcion compc
  206. */
  207.  
  208. int compaux(char ent[]){
  209.     if (strcmp(ent,"mata")==0)
  210.         return 1;
  211.     else if (strcmp(ent,"salir")==0)
  212.         return 2;
  213.     else if (strcmp(ent,"fork")==0)
  214.         return 3;
  215.     else
  216.         return 0;
  217. }
  218.  
  219. int inicial( LISTA *lst ){
  220.     lst->num = 0;
  221.     lst->inicio = lst->fin = NULL;
  222.     if( lst->num == 0 && lst->inicio ==NULL )
  223.         return 0;
  224.     else
  225.         return 1;
  226. }
  227.  
  228. void ins_list( PROCESO *nuevo,LISTA *listos){
  229.  
  230.     if( listos->inicio == NULL ){
  231.         listos->inicio = nuevo;
  232.         listos->fin = nuevo;
  233.     }else{
  234.         listos->fin->siguiente = nuevo;
  235.         listos->fin = nuevo;
  236.         listos->fin->siguiente = NULL;
  237.     }
  238.     listos->num++;
  239. }
  240.  
  241. /*
  242.  * Funcion elim_list
  243.  * Elimina el proceso con el pid (mpid) de la lista listos y la agrega a
  244.  * la lista terminados
  245.  */
  246.  
  247. int mata(int mpid, LISTA *listos, LISTA *terminados, LISTA *ejecucion, LISTA *bloqueados, LISTA *suspendidos, int padre, int **tmm, char **ram, int *grps, int *recursos){
  248.  
  249.     PROCESO *actual, *pad, *aux, *desus;
  250.     bool encontrado = false;
  251.  
  252.     if( ejecucion->inicio != NULL && ejecucion->inicio->pid == mpid ){
  253.         actual = ejecucion->inicio;
  254.         encontrado = true;
  255.     }else if( listos->inicio != NULL ){
  256.         actual = listos->inicio;
  257.         while( actual->pid != mpid && actual->siguiente != NULL )
  258.             actual = actual->siguiente;
  259.         if ( actual->pid == mpid )
  260.             encontrado = true;
  261.     }
  262.     if( bloqueados->inicio != NULL && encontrado == false ){
  263.         actual = bloqueados->inicio;
  264.         while( actual->pid != mpid && actual->siguiente != NULL )
  265.             actual = actual->siguiente;
  266.         if ( actual->pid == mpid )
  267.             encontrado = true;
  268.     }
  269.     if( suspendidos->inicio != NULL && encontrado == false ){
  270.         actual = suspendidos->inicio;
  271.         while( actual->pid != mpid && actual->siguiente != NULL )
  272.             actual = actual->siguiente;
  273.         encontrado = true;
  274.         if ( actual->pid != mpid )
  275.             encontrado = false;
  276.     }
  277.     if( encontrado == false )
  278.         return 1;
  279.  
  280.     /*
  281.      * Aquí ya encontró el proceso a matar
  282.      */
  283.     libera( actual, tmm, ram );
  284.     recursos[0]+=actual->recAs[0];
  285.     recursos[1]+=actual->recAs[1];
  286.     recursos[2]+=actual->recAs[2];
  287.     recursos[3]+=actual->recAs[3];
  288.     actual->recAs[0] = actual->recAs[1] = actual->recAs[2] = actual->recAs[3] = 0;
  289.     desus = suspendidos->inicio;
  290.     while( suspendidos->inicio != NULL ){
  291.         suspendidos->inicio = suspendidos->inicio->siguiente;
  292.         ins_list(desus, listos);
  293.         desus = suspendidos->inicio;
  294.         suspendidos->num--;
  295.     }
  296.  
  297.     if( actual->hijos->inicio != NULL ){
  298.         if ( mata( actual->hijos->inicio->pid, listos,terminados,ejecucion, bloqueados, suspendidos,1,tmm, ram, grps, recursos ) != 0 )
  299.             return 1;
  300.         else
  301.             actual->hijos->inicio = NULL;
  302.     }
  303.  
  304.     /*
  305.      * padre es una variable para verificar desde donde se está mandando llamar mata(), si se llama explícitamente se invoca con 0 si es recurción se invoca con 1
  306.      */
  307.  
  308.     if( padre != 0 ){
  309.         if( actual->hermano != NULL ){
  310.             if ( mata( actual->hermano->pid, listos,terminados,ejecucion, bloqueados, suspendidos, 1, tmm, ram, grps, recursos ) != 0 )
  311.                 return 1;
  312.             else
  313.                 actual->hermano = NULL;
  314.         }
  315.     }
  316.     if( actual->pid != actual->ppid ){
  317.         pad = busca_padre( actual->ppid, listos );
  318.         if ( pad == NULL )
  319.             pad = busca_padre( actual->ppid, ejecucion );
  320.         if( pad == NULL )
  321.             pad = busca_padre( actual->ppid, bloqueados );
  322.         if( pad == NULL )
  323.             pad = busca_padre( actual->ppid, suspendidos );
  324.         if( pad == NULL )
  325.             return 1;
  326.         aux = pad->hijos->inicio;
  327.         if( aux->pid != actual->pid ){
  328.             if( aux->hermano != NULL )
  329.                 aux->hermano = aux->hermano->hermano;
  330.             else
  331.                 return 1;
  332.         }
  333.         else{
  334.             pad->hijos->inicio = pad->hijos->inicio->hermano;
  335.             if( pad->hijos->inicio == NULL )
  336.                 (*grps)--;
  337.             actual->hermano = NULL;
  338.         }
  339.     }else
  340.         (*grps)--;
  341.    
  342.     if( elim_list(actual->pid,listos,terminados,ejecucion,suspendidos,bloqueados) != 0 )
  343.         return 1;
  344.     else
  345.         return 0;
  346. }
  347.  
  348. int elim_list(int mpid, LISTA *listos, LISTA *terminados, LISTA *ejecucion, LISTA *suspendidos, LISTA *bloqueados ){
  349.     if( listos->inicio == NULL && ejecucion->inicio == NULL && suspendidos->inicio == NULL && bloqueados->inicio == NULL ){
  350.         return 1;
  351.     }else{
  352.         PROCESO *ant, *act;
  353. /*
  354.  * Eliminación en ejecución
  355.  */
  356.         if( ejecucion->inicio != NULL ){
  357.             ant = ejecucion->inicio;
  358.             act = ant->siguiente;
  359.    
  360.             if( ant->pid == mpid ){
  361.                 ejecucion->inicio = act;
  362.                 ant->siguiente = NULL;
  363.                 ins_list(ant, terminados);
  364.                 ejecucion->num--;
  365.                 if( fclose(terminados->fin->instrucciones) != 0 )
  366.                     return 1;
  367.                 else
  368.                     return 0;
  369.             }
  370.            
  371.             while( act != NULL ){
  372.                 if( act->pid == mpid ){
  373.                     ant->siguiente = act->siguiente;
  374.                     act->siguiente = NULL;
  375.                     ins_list(act, terminados);
  376.                     if( fclose(act->instrucciones) != 0 )
  377.                         return 1;
  378.                     else
  379.                         return 0;
  380.                 }
  381.                 ant = act;
  382.                 act = act->siguiente;
  383.             }
  384.         }
  385. /*
  386.  * Eliminación en listos
  387.  */
  388.         if( listos->inicio != NULL ){
  389.  
  390.             ant = listos->inicio;
  391.  
  392.             if ( ant->pid == mpid ){
  393.                 if ( listos->fin->pid == mpid ){
  394.                     listos->inicio = NULL;
  395.                     listos->fin = NULL;
  396.                 }else{
  397.                     listos->inicio = ant->siguiente;
  398.                     listos->num--;
  399.                 }
  400.                 act = ant;
  401.                 act->siguiente = NULL;
  402.                 ins_list(act, terminados);
  403.                 ant = NULL;
  404.                 if( fclose(act->instrucciones) != 0 )
  405.                     return 1;  
  406.                 else
  407.                     return 0;
  408.             }else{
  409.                 if ( ant->siguiente != NULL ){
  410.                     for ( act = ant->siguiente; act != NULL; act = act->siguiente )
  411.                         if ( act->pid == mpid ){
  412.                             if( act == listos->fin )
  413.                                 listos->fin = ant;
  414.                             ant->siguiente = act->siguiente;
  415.                             act->siguiente = NULL;
  416.                             ins_list(act, terminados);
  417.                             listos->num--;
  418.                             ant = NULL;
  419.                             if( fclose(act->instrucciones) != 0 )
  420.                                 return 1;  
  421.                             else
  422.                                 return 0;
  423.                         }else
  424.                             ant = act;
  425.                 }
  426.             }
  427.         }
  428. /*
  429.  * Eliminación en suspendidos
  430.  */
  431.         if( suspendidos->inicio != NULL ){
  432.  
  433.             ant = suspendidos->inicio;
  434.  
  435.             if ( ant->pid == mpid ){
  436.                 if ( suspendidos->fin->pid == mpid ){
  437.                     suspendidos->inicio = NULL;
  438.                     suspendidos->fin = NULL;   
  439.                 }else{
  440.                     suspendidos->inicio = ant->siguiente;
  441.                     suspendidos->num--;
  442.                 }
  443.                 act = ant;
  444.                 act->siguiente = NULL;
  445.                 ins_list(act, terminados);
  446.                 ant = NULL;
  447.                 if( fclose(act->instrucciones) != 0 )
  448.                     return 1;  
  449.                 else
  450.                     return 0;
  451.             }else{
  452.                 if ( ant->siguiente != NULL ){
  453.                     for ( act = ant->siguiente; act != NULL; act = act->siguiente )
  454.                         if ( act->pid == mpid ){
  455.                             if( act == suspendidos->fin )
  456.                                 suspendidos->fin = ant;
  457.                             ant->siguiente = act->siguiente;
  458.                             act->siguiente = NULL;
  459.                             ins_list(act, terminados);
  460.                             suspendidos->num--;
  461.                             ant = NULL;
  462.                             if( fclose(act->instrucciones) != 0 )
  463.                                 return 1;  
  464.                             else
  465.                                 return 0;
  466.                         }else
  467.                             ant = act;
  468.                 }
  469.             }
  470.         }
  471. /*
  472.  * Eliminación en bloqueados
  473.  */
  474.         if( bloqueados->inicio != NULL ){
  475.  
  476.             ant = bloqueados->inicio;
  477.  
  478.             if ( ant->pid == mpid ){
  479.                 if ( bloqueados->fin->pid == mpid ){
  480.                     bloqueados->inicio = NULL;
  481.                     bloqueados->fin = NULL;
  482.                 }else{
  483.                     bloqueados->inicio = ant->siguiente;
  484.                     bloqueados->num--;
  485.                 }
  486.                 act = ant;
  487.                 act->siguiente = NULL;
  488.                 ins_list(act, terminados);
  489.                 ant = NULL;
  490.                 if( fclose(act->instrucciones) != 0 )
  491.                     return 1;  
  492.                 else
  493.                     return 0;
  494.             }else{
  495.                 if ( ant->siguiente != NULL ){
  496.                     for ( act = ant->siguiente; act != NULL; act = act->siguiente )
  497.                         if ( act->pid == mpid ){
  498.                             if( act == bloqueados->fin )
  499.                                 bloqueados->fin = ant;
  500.                             ant->siguiente = act->siguiente;
  501.                             act->siguiente = NULL;
  502.                             ins_list(act, terminados);
  503.                             bloqueados->num--;
  504.                             ant = NULL;
  505.                             if( fclose(act->instrucciones) != 0 )
  506.                                 return 1;  
  507.                             else
  508.                                 return 0;
  509.                         }else
  510.                             ant = act;
  511.                 }
  512.             }
  513.         }
  514.        
  515.     }
  516.     return 1;
  517. }
  518.  
  519. /*
  520.  * Función para levantar los procesos
  521.  */
  522.  
  523. PROCESO * levanta_proceso(char *comando, int *pid, int status, int *recursos){
  524.     int i = 0, j = 0;
  525.     char temp[linesize] = "Vacio", OPT[4], R1[4], R2[4], R3[4], R4[4];
  526.  
  527.     PROCESO *nuevo = (PROCESO *) malloc ( sizeof( PROCESO ) );
  528.     nuevo->instrucciones = fopen(comando,"r");
  529.     move(1,status);clrtoeol();
  530.     if( nuevo->instrucciones == NULL ){
  531.         free(nuevo);
  532.         nuevo = NULL;
  533.         printw("Proceso %s no existe", comando);
  534.     }else{
  535.         fgets( temp, linesize, nuevo->instrucciones );
  536.         for( i = 0; temp[i] && temp[i] == ' '; i++ );
  537.         OPT[0] = temp[i+0]; OPT[1] = temp[i+1]; OPT[2] = temp[i+2]; OPT[3] = '\0';
  538.         if( strcmp(OPT , "MAX") != 0 ){
  539.             free(nuevo);
  540.             printw("Proceso sin cabecera de recursos");
  541.             return NULL;
  542.         }
  543.         OPT[0] = '\0';
  544.         if( parteInstruccion( temp, OPT, R1, R2, R3, R4 ) != 0 ){
  545.             printw("Error en cabecera de recursos");
  546.             return NULL;
  547.         }
  548.         if( recursos[0] < atoi(R1) || recursos[1] < atoi(R2) || recursos[2] < atoi(R3) || recursos[3] < atoi(R4) ){
  549.             printw("Recursos inválidos: R1 <= %d R2 <= %d R3 <= %d R4 <= %d", recursos[0], recursos[1], recursos[2], recursos[3] );
  550.             return NULL;
  551.         }
  552. /*
  553.  * Aquí el proceso ya es válido y se inicializa
  554.  */
  555.  
  556.         nuevo->recs = ( int * ) malloc( sizeof( int ) * 4 );
  557.         nuevo->recAs = ( int * ) malloc( sizeof( int ) * 4 );
  558.  
  559.         nuevo->recs[0] = atoi(R1); nuevo->recs[1] = atoi(R2); nuevo->recs[2] = atoi(R3); nuevo->recs[3] = atoi(R4);
  560.         nuevo->recAs[0] = nuevo->recAs[1] = nuevo->recAs[2] = nuevo->recAs[3] = 0;
  561.  
  562.         nuevo->pid = (*pid);
  563.         nuevo->ppid = (*pid)++;
  564.         nuevo->AX = 0x0;
  565.         nuevo->BX = 0x0;
  566.         nuevo->CX = 0x0;
  567.         nuevo->DX = 0x0;
  568.         nuevo->PC = 0;
  569.         nuevo->siguiente = NULL;
  570.         nuevo->nombre = (char *) malloc ( sizeof(char) * strlen(comando));
  571.         for( i = 0; comando[i] != '\0'; i++){
  572.             *((nuevo->nombre)+i) = *(comando+i);
  573.         }
  574.         *((nuevo->nombre)+i) = '\0';
  575.         printw("Ultimo Proceso levantado: #%d: %s",nuevo->pid, nuevo->nombre);
  576.         nuevo->hijos = (LISTA *) malloc ( sizeof ( LISTA ) );
  577.         nuevo->hijos->inicio = nuevo->hijos->fin = NULL;
  578.         nuevo->hermano = NULL;
  579.         nuevo->proceso = 0;
  580.         nuevo->grupo = 0;
  581.         nuevo->fp = 0;
  582.         nuevo->bloqueo = 0;
  583.         rewind( nuevo->instrucciones );
  584.         for( i = 0; !feof( nuevo->instrucciones ); i++)
  585.             fgets( temp, linesize, nuevo->instrucciones );
  586.         /*
  587.          * Se resta 1 debido a que la primera instrucción es la instrucción de requerimientos de recursos, no cuenta como instrucción
  588.          */
  589.  
  590.         rewind( nuevo->instrucciones );
  591.         nuevo->tmp = (int **) malloc ( sizeof( int[2] ) * ( i - 1 ) );
  592.         for ( j = 0; j < i-1; j++ ){
  593.             (nuevo->tmp)[j] = ( int *) malloc ( sizeof( int[2] ) );
  594.             (nuevo->tmp)[j][0] = 0;
  595.                         (nuevo->tmp)[j][1] = 0;
  596.         }
  597.         nuevo->lineas = i;
  598.         nuevo->fork = 0;
  599.     }
  600.     return nuevo;
  601. }
  602.  
  603. PROCESO * levanta_hijo(char *comando, int *pid, int status, LISTA *listos, LISTA *ejecucion, int *recursos, LISTA *suspendidos, LISTA *bloqueados ){
  604.     int ppid = 0, pc = 0, i = 0;
  605.     char IR[linesize];
  606.     PROCESO *padre, *actual, *nuevo;
  607.     div_fork( comando, &ppid, &pc );
  608.     move(1,status);clrtoeol();
  609.    
  610.     /*
  611.      * Búsqueda del padre
  612.      */
  613.  
  614.     padre = busca_padre(ppid,listos);
  615.  
  616.     /*
  617.      * Búsqueda en lista de ejecución
  618.      */
  619.  
  620.     if( padre == NULL )
  621.         padre = busca_padre(ppid,ejecucion);
  622.     if( padre == NULL )
  623.         padre = busca_padre(ppid,suspendidos);
  624.     if( padre == NULL )
  625.         padre = busca_padre(ppid,bloqueados);
  626.  
  627.     if( padre == NULL ){
  628.         printw("Proceso padre %d no encontrado",ppid);
  629.         return NULL;
  630.     }
  631.    
  632.     nuevo = levanta_proceso( padre->nombre, pid, status, recursos);
  633.     nuevo->fork = 1;
  634.     if( padre->hijos->inicio != NULL ){
  635.         actual = padre->hijos->inicio;
  636.         while( actual->hermano != NULL )
  637.             actual = actual->hermano;
  638.         actual->hermano = nuevo;
  639.     }else
  640.         padre->hijos->inicio = nuevo;
  641.     nuevo->ppid = ppid;
  642.  
  643.     /*
  644.      * Ciclo de búsqueda según el pc otorgado
  645.      */
  646.  
  647.     for ( i = 1; i < pc && !feof(nuevo->instrucciones); i++ )
  648.         fgets( IR, linesize, nuevo->instrucciones );
  649.     if ( feof( nuevo->instrucciones ) ){
  650.         move(1, status);clrtoeol();printw("No existen tantas instrucciones en el archivo");
  651.         return NULL;
  652.     }
  653.     nuevo->PC = pc;
  654.     return nuevo;
  655. }
  656.  
  657. PROCESO * busca_padre( int pid,LISTA *lista ){
  658.     PROCESO *actual;
  659.  
  660.     actual = lista->inicio;
  661.     if( actual != NULL ){
  662.         while( actual->pid != pid && actual->siguiente != NULL )
  663.             actual = actual->siguiente;
  664.         if( actual->pid == pid )
  665.             return actual;
  666.     }
  667.  
  668.         return NULL;
  669. }
  670.  
  671. void div_fork( char *comando, int *pid, int *pc ){
  672.     int i = 0, j = 0;
  673.     char temp[10];
  674.     temp[9] = '\0';
  675.     temp[0]='1';
  676.     temp[1]='\0';
  677.     while( comando[i] != ' ' )
  678.         i++;
  679.     i++;
  680.     while( comando[i] != ' ' && comando[i] != '\0' ){
  681.         temp[j] = comando[i];
  682.         j++;i++;
  683.     }
  684.     temp[j] = '\0';
  685.     *pid = atoi(temp);
  686.     temp[0] = '\0';
  687.     while( comando[i] == ' ' )
  688.         i++;
  689.     j = 0;
  690.     while( comando[i] != ' ' && comando[i] != '\0' ){
  691.         temp[j] = comando[i];
  692.         i++;j++;
  693.     }
  694.     temp[j] = '\0';
  695.     if ( strlen( temp ) == 0 )
  696.         *pc = 1;
  697.     else
  698.         *pc = atoi(temp);
  699. }
  700.  
  701. int traduce( int tmp, int pc ){
  702.     tmp*=2;
  703.     if( pc%2 != 0 )
  704.         tmp+=1;
  705.     return tmp;
  706. }
  707.  
  708. int ejecuta( unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX, LISTA *ejecucion, int timer, LISTA *listos, float wk, int vel, char **ram, int **tmm, LISTA *suspendidos, LISTA *bloqueados, int comandprompt, int *recursos, int *recdis ){
  709.  
  710.     int n = 0, i = 0, c = 0, x, ram_ind = 0, *used_frame, marcos;
  711.     char temp[linesize], IR[linesize], codop[4], op1[3], op2[16], op3[3], op4[3];
  712.     enum codops{ERROR, MOV, ADD, SUB, MUL, DIV, INC, DEC, END, JNZ, MAX, GET, USE, FRE}op;
  713.  
  714.     move(2,10);printw("EXEC");move(0,comandprompt);refresh();
  715.     int ppid = ejecucion->inicio->ppid;
  716.  
  717.     PROCESO *paux;
  718.  
  719.     /*
  720.      * used_frame contendrá los indices de los frames usados por el proceso en ejecución
  721.      */
  722.  
  723.     used_frame = (int *) malloc ( sizeof(int) * 3 );
  724.  
  725.     for ( n = 0; n < 3; n++ )
  726.         used_frame[n] = -1;
  727.  
  728.     for( n = 0; n < timer; n++ ){
  729.         /* regulador de velocidad, usado solo para esperar y que la ejecución sea visible por el usuario*/
  730.         napms(vel);
  731.         if( ejecucion->inicio->PC >= ejecucion->inicio->lineas || ejecucion->inicio->PC < 0 )
  732.             return 1;
  733.         if( ejecucion->inicio->tmp[ejecucion->inicio->PC][0] != 1 ){
  734.             ejecucion->inicio->fp++;
  735.             marcos = lleno( ejecucion->inicio, used_frame );
  736.             if( marcos >= 3 ){
  737.                 if ( switch_frame( ejecucion->inicio, tmm, ram, used_frame, marcos ) != 0 ){
  738.                     move(1,10);clrtoeol();
  739.                     printw("Fallo de intercambio de páginas");refresh();sleep(5);
  740.                     return 1;
  741.                 }
  742.             }else if( switch_other( ejecucion->inicio, tmm, ram ) != 0 ){
  743.                 move(1,10);clrtoeol();
  744.                 printw("Fallo de intercambio de páginas");refresh();sleep(5);
  745.             }
  746.         }
  747.  
  748.         ram_ind = traduce( ejecucion->inicio->tmp[ejecucion->inicio->PC][1], ejecucion->inicio->PC);
  749.  
  750.         for( x = 0; x < 100 && ram[ram_ind][x+1] != '\0'; x++ )
  751.             IR[x] = ram[ram_ind][x];
  752.         IR[x] = '\0';
  753.  
  754.         /*
  755.          * Es necesario reinicialiciar i y c
  756.          */
  757.  
  758.         i = c = 0;
  759.  
  760.         /*
  761.          * Eliminamos los espacios al final de la linea
  762.          */
  763.  
  764.         while( IR[strlen(IR)-1] == ' ' || IR[strlen(IR)-1] == '\n')
  765.             IR[strlen(IR)-1] = '\0';
  766.         /*
  767.          * Eliminamos los espacios al inicio de la linea
  768.          */
  769.  
  770.         while( IR[i] == ' ' )
  771.             i++;
  772.    
  773.         while( IR[i] != '\0' ){
  774.             temp[c] = IR[i];
  775.             i++;c++;
  776.         }
  777.         temp[i] = '\0';i = 0;
  778.  
  779.         while( temp[i] != '\0'){
  780.             IR[i] = temp[i];
  781.             i++;
  782.         }
  783.         IR[i] = '\0';
  784.  
  785.     /*
  786.      * Verificamos que no sea una instrucción muy larga o vacía
  787.      */
  788.  
  789.         if( strlen(IR) > insize )
  790.             return 1;
  791.         else
  792.             if( strlen(IR) == 0 )
  793.                 return 1;
  794.     /*
  795.      * Para nuestra función requerimos un espacio al final de la instrucción, se lo insertamos
  796.      */
  797.         if( parteInstruccion(IR,codop,op1,op2,op3,op4) != 0 ){
  798.             return 1;}
  799.         else if(strcmp(codop,"ADD") == 0)
  800.             op = ADD;
  801.         else if(strcmp(codop,"MOV") == 0)
  802.             op = MOV;
  803.         else if(strcmp(codop,"SUB") == 0)
  804.             op = SUB;
  805.         else if(strcmp(codop,"MUL") == 0)
  806.             op = MUL;
  807.         else if(strcmp(codop,"DIV") == 0)
  808.             op = DIV;
  809.         else if(strcmp(codop,"INC") == 0)
  810.             op = INC;
  811.         else if(strcmp(codop,"DEC") == 0)
  812.             op = DEC;
  813.         else if(strcmp(codop,"END") == 0)
  814.             op = END;
  815.         else if(strcmp(codop,"JNZ") == 0)
  816.             op = JNZ;
  817.         else if(strcmp(codop,"MAX") == 0)
  818.             op = MAX;
  819.         else if(strcmp(codop,"GET") == 0)
  820.             op = GET;
  821.         else if(strcmp(codop,"USE") == 0)
  822.             op = USE;
  823.         else if(strcmp(codop,"FRE") == 0)
  824.             op = FRE;
  825.         else
  826.             op = ERROR;
  827.  
  828.         ram_print( ram ); tmm_print( tmm );// tmp_print( ejecucion );
  829.         move(7,2);clrtoeol();
  830.         printw("%llX",*AX);move(7,11);printw("||%llX",*BX);move(7,23);printw("||%llX",*CX);move(7,35);printw("||%llX",*DX);move(7,47);printw("||%d",ejecucion->inicio->PC);if( IR != NULL ){move(7,59);printw("||%s",IR);}
  831.         move(7,77);printw("||");move(7,80);printw("%d: %s",ejecucion->inicio->pid,ejecucion->inicio->nombre);move(7,96);printw("||");move(7,102);printw("%f",wk);move(7,110);printw("||");move(7,115);printw("%d",recdis[0]);move(7,125);printw("%d",recdis[1]);move(7,135);printw("%d",recdis[2]);move(7,145);printw("%d",recdis[3]);move(7,152);printw("||");move(7,162);printw("||%s",ram[2]);move(7,175);printw("||");move(0,comandprompt);
  832.         refresh();
  833. ram_print(ram);tmm_print(tmm);//tmp_print( ejecucion);
  834.         switch(op){
  835.             case ADD:
  836.                 if( add(op1,op2,AX,BX,CX,DX) != 0 )
  837.                     return 1;
  838.                 break;
  839.             case MOV:
  840.                 if( mov(op1,op2,AX,BX,CX,DX) != 0 )
  841.                     return 1;
  842.                 break;
  843.             case SUB:
  844.                 if( sub(op1,op2,AX,BX,CX,DX) != 0 )
  845.                     return 1;
  846.                 break;
  847.             case MUL:
  848.                 if( mul(op1,op2,AX,BX,CX,DX) != 0 )
  849.                     return 1;
  850.                 break;
  851.             case DIV:
  852.                 if( divi(op1,op2,AX,BX,CX,DX) != 0 )
  853.                     return 1;
  854.                 break;
  855.             case INC:
  856.                 if( inc(op1,AX,BX,CX,DX) != 0 )
  857.                     return 1;
  858.                 break;
  859.             case DEC:
  860.                 if( dec(op1,AX,BX,CX,DX) != 0 )
  861.                     return 1;
  862.                 break;
  863.             case END:
  864.                 if( end(IR) != 0 )
  865.                     return 1;
  866.                 break;
  867.             case JNZ:
  868.                 if( jump(op1, CX, &(ejecucion->inicio->PC)) != 0 )
  869.                     return 1;
  870.                 break;
  871.             case GET:
  872.                
  873.                 i = get( recursos, recdis, op1, op2, op3, op4, listos, ejecucion );
  874.                 if( i == 1 ){
  875.                     move(1,10);clrtoeol();printw("Petición ilegal de recursos");refresh();sleep(2);
  876.                     return 1;
  877.                 }
  878.                 if( i == 2 ){
  879.                     move(1,10);clrtoeol();printw("Recursos disponibles no suficientes");refresh();
  880.                     ins_list( ejecucion->inicio, suspendidos );
  881.                     ejecucion->num--;
  882.                     ejecucion->inicio = ejecucion->fin = NULL;
  883.                     return 0;
  884.                 }
  885.                 if( i == 3 ){
  886.                     move(1,10);clrtoeol();printw("Prestamo inseguro");refresh();
  887.                     ins_list( ejecucion->inicio, suspendidos );
  888.                     ejecucion->num--;
  889.                     ejecucion->inicio = ejecucion->fin = NULL;
  890.                     return 0;
  891.                 }
  892.                 break;
  893.             case USE:
  894.                 i = use( ejecucion->inicio, op1, op2, op3, op4 );
  895.                 if( i == 1 ){
  896.                     move(1,10);clrtoeol();printw("Uso ilegal de recursos");refresh();sleep(2);
  897.                     return 1;
  898.                 }
  899.                 ejecucion->inicio->PC++;
  900.                 ins_list( ejecucion->inicio, bloqueados );
  901.                 ejecucion->num--;
  902.                 ejecucion->inicio = ejecucion->fin = NULL;
  903.                 return 0;
  904.                 break;
  905.             case FRE:
  906.                 i = fre( ejecucion->inicio, op1, op2, op3, op4, recdis );
  907.                 if( i == 1 ){
  908.                     move(1,10);clrtoeol();printw("Liberacion ilegal de recursos");refresh();sleep(100);
  909.                     return 1;
  910.                 }
  911.                 if( suspendidos->inicio != NULL ){
  912.                     paux = suspendidos->inicio;
  913.                     while( paux != NULL ){
  914.                         suspendidos->inicio = suspendidos->inicio->siguiente;
  915.                         ins_list( paux, listos );
  916.                         suspendidos->num--;
  917.                         paux = suspendidos->inicio;
  918.                     }
  919.                 }
  920.                 break;
  921.             default:
  922.                 return 1;
  923.                 break;
  924.         }
  925.         if ( op != JNZ )
  926.             ejecucion->inicio->PC++;
  927.         ejecucion->inicio->proceso+=20;
  928.         ejecucion->inicio->grupo+=20;
  929.         for ( paux = listos->inicio; paux != NULL; paux = paux->siguiente ){
  930.             if (paux->ppid==ppid)
  931.                 paux->grupo+=20;
  932.         }
  933.         napms(vel);
  934.         if( end(IR) == 0 )
  935.             break;
  936.     }
  937.     if( strcmp(IR, "END") == 0 )
  938.         return 2;
  939.     return 0;
  940. }
  941.  
  942. int switch_frame( PROCESO *proc, int **tmm, char **ram, int *used, int marcos ){
  943.     int x, viejo = used[0];
  944.     char *temp;
  945.     temp = (char *) malloc ( sizeof(char) * 100 );
  946.     temp = lee_ins( proc->instrucciones, proc->PC );
  947.     if( strcmp(temp,"ERROR") == 0 )
  948.         return 1;
  949. /*
  950.  * Encontrar el frame más viejo de los usados por el mismo proceso
  951.  */
  952.     for( x = 0; x < 3; x++ ){
  953.         if( tmm[viejo][0] < tmm[used[x]][0])
  954.             viejo = used[x];
  955.     }
  956.  
  957. /*
  958.  * Encontrar las lineas que estaban en el frame a reemplazar
  959.  */
  960.  
  961.     for( x = 0; x < proc->lineas-1; x++ )
  962.         if( proc->tmp[x][1] == viejo )
  963.             proc->tmp[x][1] = proc->tmp[x][0] = 0;
  964.  
  965.     proc->tmp[proc->PC][0] = 1;
  966.     proc->tmp[proc->PC][1] = viejo;
  967.  
  968.     for( x = 0; x < 16; x++ )
  969.         if( tmm[x][0] != 0 )
  970.             tmm[x][0]++;
  971.  
  972.     tmm[viejo][0] = 1;
  973.     ram[viejo*2] = temp;
  974.  
  975.     if( strcmp( lee_ins( proc->instrucciones, proc->PC+1 ), "ERROR" ) != 0 ){
  976.         ram[viejo*2+1] = lee_ins( proc->instrucciones, proc->PC+1 );
  977.         proc->tmp[proc->PC+1][0] = 1;
  978.         proc->tmp[proc->PC+1][1] = viejo;
  979.     }else
  980.         ram[viejo*2+1] = "Vacio";
  981.    
  982.     return 0;
  983. }
  984.  
  985. int switch_other( PROCESO *proc, int **tmm, char **ram ){
  986.     int i,j,x, marcos, *used_frame, tempTMM[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, filled = 1, viejo = 0;
  987.     char *temp;
  988.     used_frame = (int *) malloc ( sizeof(int) * 3 );
  989.     do{
  990.         viejo = 0;
  991.         while( tempTMM[viejo] == 1 && viejo < 16 )
  992.             viejo++;
  993.         if( viejo < 16 ){
  994.             for( i = 0; i < 16; i++ ){
  995.                 if( tmm[i][0] < tmm[viejo][0] && tempTMM[i] != 1 )
  996.                     viejo = i;
  997.             }
  998.             for( i = 0; i < 16; i++ ){
  999.                 if( tmm[viejo][1] == tmm[i][1] )
  1000.                     filled++;
  1001.             }
  1002.             if( filled <= 1 ){
  1003.                 tempTMM[viejo] = 1;
  1004.                 for( i = 0; i < 16; i++ )
  1005.                     if( tempTMM[i] == 0 )
  1006.                         break;
  1007.                 viejo = -1;
  1008.             }
  1009.             else
  1010.                 break;
  1011.         }
  1012.     }while( i < 16 );
  1013.  
  1014.     if ( viejo == -1 ){
  1015.         marcos = lleno( proc, used_frame );
  1016.         x = tmm[0][0];
  1017.         for ( j=0; j<16; j++ )
  1018.             if (x<tmm[j][0])
  1019.                 x=tmm[j][0];
  1020.         if ( switch_frame( proc, tmm, ram, used_frame, marcos ) != 0 )
  1021.             return 1;
  1022.         else
  1023.             return 0;
  1024.     }
  1025.     else{
  1026.         temp = (char *) malloc ( sizeof(char) * 100 );
  1027.         rewind( proc->instrucciones );
  1028.         for( j = 0; j <= proc->PC && !feof(proc->instrucciones) ; j++ )
  1029.             fgets( temp, 100, proc->instrucciones );
  1030.         if( feof( proc->instrucciones ) )
  1031.             return 1;
  1032.         x = tmm[0][0];
  1033.         for ( j=0; j<16; j++ )
  1034.             if (x<tmm[j][0])
  1035.                 x=tmm[j][0];
  1036.        
  1037.         tmm[viejo][0]=x+1;
  1038.         tmm[viejo][1]=proc->pid;
  1039.         ram[viejo*2]=temp;
  1040.         proc->tmp[proc->PC-1][0]=1;
  1041.         proc->tmp[proc->PC-1][1]=viejo;
  1042.         fgets( temp, 100, proc->instrucciones );
  1043.         if( feof( proc->instrucciones ) )
  1044.             return 0;
  1045.         ram[viejo*2+1]=temp;
  1046.         proc->tmp[proc->PC][0]=1;
  1047.         proc->tmp[proc->PC][1]=viejo;
  1048.         rewind(proc->instrucciones);
  1049.         return 0;
  1050.     }
  1051. }
  1052.  
  1053. int lleno( PROCESO *proc, int *used ){
  1054.     int marcos = 0, x;
  1055.     for( x = 0; x < (proc->lineas)-1; x++ )
  1056.         if( proc->tmp[x][0] != 0 ){
  1057.             if ( marcos == 0 ){
  1058.                 used[marcos] = proc->tmp[x][1];
  1059.                 marcos++;
  1060.             }
  1061.             else if ( used[marcos-1] != proc->tmp[x][1] ){
  1062.                 used[marcos] = proc->tmp[x][1];
  1063.                 marcos++;
  1064.             }
  1065.         }
  1066.     return marcos;
  1067. }
  1068.  
  1069. int planifica(LISTA *listos, LISTA *ejecucion, LISTA *finalizados, unsigned long long *AX, unsigned long long *BX, unsigned long long *CX, unsigned long long *DX, int base, float wk){
  1070.    
  1071.     /*
  1072.      * Se guarda contexto en caso de haber un proceso en ejecución
  1073.      */
  1074.  
  1075.     if( ejecucion->inicio != NULL ){
  1076.         ejecucion->inicio->AX = *AX;
  1077.         ejecucion->inicio->BX = *BX;
  1078.         ejecucion->inicio->CX = *CX;
  1079.         ejecucion->inicio->DX = *DX;
  1080.         ins_list( ejecucion->inicio, listos );
  1081.         ejecucion->num--;
  1082.         ejecucion->inicio = NULL;
  1083.         ejecucion->fin = NULL;
  1084.     }
  1085.     if( listos->inicio != NULL ){
  1086.         PROCESO *mp, *aux, *ant, *aux2;
  1087.         mp = listos->inicio;
  1088.         if (mp->siguiente == NULL){
  1089.             mp->proceso = (int)(mp->proceso/2);
  1090.             mp->grupo = (int)(mp->grupo/2);
  1091.             mp->prioridad = base+(mp->proceso/2)+(mp->grupo/4);
  1092.             ejecucion->inicio = mp;
  1093.             (ejecucion->num)++;
  1094.             ejecucion->fin = ejecucion->inicio;
  1095.             listos->inicio = listos->fin = NULL;
  1096.             (listos->num)--;
  1097.             *AX = ejecucion->inicio->AX;
  1098.             *BX = ejecucion->inicio->BX;
  1099.             *CX = ejecucion->inicio->CX;
  1100.             *DX = ejecucion->inicio->DX;
  1101.             return 0;
  1102.         }
  1103.         else{
  1104.             aux2 = ant = mp;
  1105.             for(aux=mp; aux!=NULL;aux=aux->siguiente)
  1106.             {
  1107.                 aux->proceso = (int)(aux->proceso/2);
  1108.                 aux->grupo = (int)(aux->grupo/2);
  1109.                 aux->prioridad = base+(aux->proceso/2)+(aux->grupo/(4*wk));
  1110.                 if ((mp->prioridad) > (aux->prioridad)){
  1111.                     aux2 = ant;
  1112.                     mp = aux;
  1113.                 }
  1114.                 ant = aux;
  1115.             }
  1116.             ejecucion->inicio = mp;
  1117.             (ejecucion->num)++;
  1118.             ejecucion->fin = ejecucion->inicio;
  1119.             if (aux2->pid == mp->pid)
  1120.                 listos->inicio = mp->siguiente;
  1121.             else if (mp->pid == listos->fin->pid){
  1122.                 listos->fin = aux2;
  1123.                 listos->fin->siguiente=NULL;
  1124.             }
  1125.             else
  1126.                 aux2->siguiente = mp->siguiente;
  1127.             mp->siguiente = NULL;
  1128.             (listos->num)--;
  1129.             *AX = ejecucion->inicio->AX;
  1130.             *BX = ejecucion->inicio->BX;
  1131.             *CX = ejecucion->inicio->CX;
  1132.             *DX = ejecucion->inicio->DX;
  1133.             mp=aux2=aux=ant=NULL;
  1134.             return 0;
  1135.         }
  1136.     }
  1137.     else
  1138.         return 1;
  1139. }
  1140.  
  1141. void grupo( PROCESO *ejecutando ){
  1142.     ejecutando->grupo+=20;
  1143.     if( ejecutando->hijos->inicio != NULL )
  1144.         grupo( ejecutando->hijos->inicio );
  1145.     if( ejecutando->hermano != NULL )
  1146.         grupo( ejecutando->hermano );
  1147. }
  1148.  
  1149. int imprime (LISTA *ejecucion, LISTA *listos, LISTA *finalizados, LISTA *nuevos, LISTA *suspendidos, LISTA *bloqueados){
  1150.  
  1151. PROCESO * imprimiendo = NULL;
  1152. int n = 13, x = 0;
  1153.  
  1154.     if( ejecucion->inicio != NULL )
  1155.         imprimiendo = ejecucion->inicio;
  1156.     while( imprimiendo != NULL ){
  1157.         move(n,0);clrtoeol();
  1158.         move(n,3);printw("%d",imprimiendo->pid);
  1159.         move(n,10);printw("%llX",imprimiendo->AX);
  1160.         move(n,22);printw("%llX",imprimiendo->BX);
  1161.         move(n,34);printw("%llX",imprimiendo->CX);
  1162.         move(n,46);printw("%llX",imprimiendo->DX);
  1163.         move(n,58);printw("%d",imprimiendo->PC);
  1164.         move(n,68);printw("Ejecutando");
  1165.         move(n,83);printw("%s",imprimiendo->nombre);
  1166.         move(n,96);printw("%d",imprimiendo->ppid);
  1167.         move(n,105);printw("%f",imprimiendo->prioridad);
  1168.         move(n,120);printw("%d",imprimiendo->proceso);
  1169.         move(n,125);printw("%d",imprimiendo->grupo);
  1170.         move(n,132);printw("%d",imprimiendo->fp);
  1171.         move(n,137);printw("%d",imprimiendo->recAs[0]);
  1172.         move(n,143);printw("%d",imprimiendo->recAs[1]);
  1173.         move(n,149);printw("%d",imprimiendo->recAs[2]);
  1174.         move(n,155);printw("%d",imprimiendo->recAs[3]);
  1175.         imprimiendo = imprimiendo->siguiente;
  1176.         n++;
  1177.     }
  1178.     if( listos->inicio != NULL ) imprimiendo = listos->inicio;
  1179.     while( imprimiendo != NULL ){
  1180.         move(n,0);clrtoeol();
  1181.         move(n,3);printw("%d",imprimiendo->pid);
  1182.         move(n,10);printw("%llX",imprimiendo->AX);
  1183.         move(n,22);printw("%llX",imprimiendo->BX);
  1184.         move(n,34);printw("%llX",imprimiendo->CX);
  1185.         move(n,46);printw("%llX",imprimiendo->DX);
  1186.         move(n,58);printw("%d",imprimiendo->PC);
  1187.         move(n,68);printw("Listo");
  1188.         move(n,83);printw("%s",imprimiendo->nombre);
  1189.         move(n,96);printw("%d",imprimiendo->ppid);
  1190.         move(n,105);printw("%f",imprimiendo->prioridad);
  1191.         move(n,120);printw("%d",imprimiendo->proceso);
  1192.         move(n,125);printw("%d",imprimiendo->grupo);
  1193.         move(n,132);printw("%d",imprimiendo->fp);
  1194.         move(n,137);printw("%d",imprimiendo->recAs[0]);
  1195.         move(n,143);printw("%d",imprimiendo->recAs[1]);
  1196.         move(n,149);printw("%d",imprimiendo->recAs[2]);
  1197.         move(n,155);printw("%d",imprimiendo->recAs[3]);
  1198.         imprimiendo = imprimiendo->siguiente;
  1199.         n++;
  1200.     }
  1201.         if( nuevos->inicio != NULL ) imprimiendo = nuevos->inicio;
  1202.     while( imprimiendo != NULL ){
  1203.         move(n,0);clrtoeol();
  1204.         move(n,3);printw("%d",imprimiendo->pid);
  1205.         move(n,10);printw("%llX",imprimiendo->AX);
  1206.         move(n,22);printw("%llX",imprimiendo->BX);
  1207.         move(n,34);printw("%llX",imprimiendo->CX);
  1208.         move(n,46);printw("%llX",imprimiendo->DX);
  1209.         move(n,58);printw("%d",imprimiendo->PC);
  1210.         move(n,68);printw("Nuevo");
  1211.         move(n,83);printw("%s",imprimiendo->nombre);
  1212.         move(n,96);printw("%d",imprimiendo->ppid);
  1213.         move(n,105);printw("%f",imprimiendo->prioridad);
  1214.         move(n,120);printw("%d",imprimiendo->proceso);
  1215.         move(n,125);printw("%d",imprimiendo->grupo);
  1216.         move(n,132);printw("%d",imprimiendo->fp);
  1217.         move(n,137);printw("%d",imprimiendo->recAs[0]);
  1218.         move(n,143);printw("%d",imprimiendo->recAs[1]);
  1219.         move(n,149);printw("%d",imprimiendo->recAs[2]);
  1220.         move(n,155);printw("%d",imprimiendo->recAs[3]);
  1221.         imprimiendo = imprimiendo->siguiente;
  1222.         n++;
  1223.     }
  1224.         if( suspendidos->inicio != NULL ) imprimiendo = suspendidos->inicio;
  1225.     while( imprimiendo != NULL ){
  1226.         move(n,0);clrtoeol();
  1227.         move(n,3);printw("%d",imprimiendo->pid);
  1228.         move(n,10);printw("%llX",imprimiendo->AX);
  1229.         move(n,22);printw("%llX",imprimiendo->BX);
  1230.         move(n,34);printw("%llX",imprimiendo->CX);
  1231.         move(n,46);printw("%llX",imprimiendo->DX);
  1232.         move(n,58);printw("%d",imprimiendo->PC);
  1233.         move(n,68);printw("Suspendido");
  1234.         move(n,83);printw("%s",imprimiendo->nombre);
  1235.         move(n,96);printw("%d",imprimiendo->ppid);
  1236.         move(n,105);printw("%f",imprimiendo->prioridad);
  1237.         move(n,120);printw("%d",imprimiendo->proceso);
  1238.         move(n,125);printw("%d",imprimiendo->grupo);
  1239.         move(n,132);printw("%d",imprimiendo->fp);
  1240.         move(n,137);printw("%d",imprimiendo->recAs[0]);
  1241.         move(n,143);printw("%d",imprimiendo->recAs[1]);
  1242.         move(n,149);printw("%d",imprimiendo->recAs[2]);
  1243.         move(n,155);printw("%d",imprimiendo->recAs[3]);
  1244.         imprimiendo = imprimiendo->siguiente;
  1245.         n++;
  1246.     }
  1247.         if( bloqueados->inicio != NULL ) imprimiendo = bloqueados->inicio;
  1248.     while( imprimiendo != NULL ){
  1249.         move(n,0);clrtoeol();
  1250.         move(n,3);printw("%d",imprimiendo->pid);
  1251.         move(n,10);printw("%llX",imprimiendo->AX);
  1252.         move(n,22);printw("%llX",imprimiendo->BX);
  1253.         move(n,34);printw("%llX",imprimiendo->CX);
  1254.         move(n,46);printw("%llX",imprimiendo->DX);
  1255.         move(n,58);printw("%d",imprimiendo->PC);
  1256.         move(n,68);printw("Bloqueado");
  1257.         move(n,83);printw("%s",imprimiendo->nombre);
  1258.         move(n,96);printw("%d",imprimiendo->ppid);
  1259.         move(n,105);printw("%f",imprimiendo->prioridad);
  1260.         move(n,120);printw("%d",imprimiendo->proceso);
  1261.         move(n,125);printw("%d",imprimiendo->grupo);
  1262.         move(n,132);printw("%d",imprimiendo->fp);
  1263.         move(n,137);printw("%d",imprimiendo->recAs[0]);
  1264.         move(n,143);printw("%d",imprimiendo->recAs[1]);
  1265.         move(n,149);printw("%d",imprimiendo->recAs[2]);
  1266.         move(n,155);printw("%d",imprimiendo->recAs[3]);
  1267.         imprimiendo = imprimiendo->siguiente;
  1268.         n++;
  1269.     }
  1270.     if( finalizados->inicio != NULL ) imprimiendo = finalizados->inicio;
  1271.     while( imprimiendo != NULL ){
  1272.         move(n,0);clrtoeol();
  1273.         move(n,3);printw("%d",imprimiendo->pid);
  1274.         move(n,10);printw("%llX",imprimiendo->AX);
  1275.         move(n,22);printw("%llX",imprimiendo->BX);
  1276.         move(n,34);printw("%llX",imprimiendo->CX);
  1277.         move(n,46);printw("%llX",imprimiendo->DX);
  1278.         move(n,58);printw("%d",imprimiendo->PC);
  1279.         move(n,68);printw("Finalizado");
  1280.         move(n,83);printw("%s",imprimiendo->nombre);
  1281.         move(n,96);printw("%d",imprimiendo->ppid);
  1282.         move(n,105);printw("%f",imprimiendo->prioridad);
  1283.         move(n,120);printw("%d",imprimiendo->proceso);
  1284.         move(n,125);printw("%d",imprimiendo->grupo);
  1285.         move(n,132);printw("%d",imprimiendo->fp);
  1286.         move(n,137);printw("%d",imprimiendo->recAs[0]);
  1287.         move(n,143);printw("%d",imprimiendo->recAs[1]);
  1288.         move(n,149);printw("%d",imprimiendo->recAs[2]);
  1289.         move(n,155);printw("%d",imprimiendo->recAs[3]);
  1290.         imprimiendo = imprimiendo->siguiente;
  1291.         n++;
  1292.     }
  1293.     for( x = n; x < 200; x++ ){
  1294.         move(x,0);
  1295.         clrtoeol();
  1296.     }
  1297. return 0;
  1298. }
  1299.  
  1300. void ram_print( char **ram ){
  1301.     int x, y = 0;
  1302.     move(2,162);printw("+-----RAM-----+");
  1303.     for( x = 0; x < 32; x++){
  1304.         if( x%2 == 0 ){
  1305.             y++;
  1306.             move(x+2+y,162);
  1307.             printw("||===========||");
  1308.         }
  1309.         move(x+3+y,162);clrtoeol();
  1310.         printw("||%s",ram[x]);
  1311.         move(x+3+y,175);
  1312.         printw("||");
  1313.     }
  1314.     move(x+3+y,162);
  1315.     printw("||===========||");
  1316.     move(x+4+y,162);
  1317.     printw("++-----------++");
  1318. }
  1319. void tmm_print( int **tmm ){
  1320.     int x;
  1321.     move(8,182);printw("+--TMM--+");
  1322.     for( x = 0; x < 16; x++ ){
  1323.             move(9+x, 182);printw("| %d, %d ",tmm[x][0],tmm[x][1]);
  1324.             move(9+x, 190);printw("|");
  1325.     }
  1326.     move(9+x,182);printw("+-------+");
  1327. }
  1328.  
  1329. void tmp_print( LISTA *ejecucion ){
  1330.     int x;
  1331.     move(8,192);printw("+--TMP--+");
  1332.     for( x = 0; x < ejecucion->inicio->lineas - 1; x++ ){
  1333.         move(9+x, 192);printw("| %d, %d ",(ejecucion->inicio->tmp)[x][0],(ejecucion->inicio->tmp)[x][1]);
  1334.         move(9+x, 200);printw("|");
  1335.     }
  1336.     move(9+x,192);printw("+-------+");
  1337. }
  1338.  
  1339. int free_frame( int **tmm ){
  1340.     int x;
  1341.     for( x = 0; x < 16 ; x++ )
  1342.         if( tmm[x][0] == 0 )
  1343.             break;
  1344.     if( x >= 16 )
  1345.         return -1;
  1346.     else
  1347.         return x;
  1348. }
  1349.  
  1350. /*
  1351.  * Función que saca un proceso del frente de la lista origen y lo pone al final de la lista destino
  1352.  */
  1353.  
  1354. int swap( LISTA *origen, LISTA *destino ){
  1355.     PROCESO *temp;
  1356.    
  1357.     if( origen->inicio == NULL )
  1358.         return 1;
  1359.     temp = origen->inicio;
  1360.     if( origen->inicio == origen->fin )        
  1361.         origen->inicio = origen->fin = NULL;
  1362.     else
  1363.         origen->inicio = origen->inicio->siguiente;
  1364.     ins_list( temp, destino );
  1365.     origen->num--;
  1366.     return 0;
  1367. }
  1368.  
  1369. char *lee_ins( FILE *archivo, int ins ){
  1370.     char *temp;
  1371.     int x;
  1372.     temp = (char *) malloc ( sizeof(char) * 100 );
  1373.     rewind( archivo );
  1374.     for( x = 1; x <= ins+2 && !feof(archivo) ; x++ )
  1375.         fgets( temp, 100, archivo );
  1376.     if( feof(archivo) && x != ins )
  1377.         return "ERROR";
  1378.     return temp;
  1379. }
  1380.  
  1381. void libera( PROCESO *proc, int **tmm, char **ram){
  1382.     int x;
  1383.     for( x = 0; x < 16; x++ ){
  1384.         if( tmm[x][0] != 0 && tmm[x][1] == proc->pid ){
  1385.             ram[x*2] = ram[x*2+1] = "Vacio";
  1386.             tmm[x][0] = tmm[x][1] = 0;
  1387.         }
  1388.     }
  1389. }
  1390.  
  1391. int get( int *rectot, int *recdisp, char *op1, char *op2, char *op3, char *op4, LISTA *listos, LISTA *ejecucion ){
  1392.  
  1393.     int procount = 1, i, *procesos, *pendientes;
  1394.     PROCESO *aux;
  1395.  
  1396. /*
  1397.  * Verificando legalidad de petición de recursos
  1398.  */
  1399.  
  1400.     if( rectot[0] < atoi(op1) || rectot[1] < atoi(op2) || rectot[2] < atoi(op3) || rectot[3] < atoi(op4) ||
  1401.     (ejecucion->inicio->recAs[0] + atoi(op1)) > ejecucion->inicio->recs[0] ||
  1402.     (ejecucion->inicio->recAs[1] + atoi(op2)) > ejecucion->inicio->recs[1] ||
  1403.     (ejecucion->inicio->recAs[2] + atoi(op2)) > ejecucion->inicio->recs[2] ||
  1404.     (ejecucion->inicio->recAs[3] + atoi(op2)) > ejecucion->inicio->recs[3] )
  1405.         return 1;
  1406.    
  1407. /*
  1408.  * Verificando disponibilidad de recursos
  1409.  */
  1410.     if( recdisp[0] < atoi(op1) || recdisp[1] < atoi(op2) || recdisp[2] < atoi(op3) || recdisp[3] < atoi(op4) )
  1411.         return 2;
  1412.  
  1413. /*
  1414.  * Aplicación del algoritmo banquero para calcular seguridad de prestamo de recursos
  1415.  */
  1416.     int *disp, count = 0, *ejec, x, pen = 0;
  1417.     bool satisfecho = false;
  1418.  
  1419.     aux = listos->inicio;
  1420.     while( aux != NULL ){
  1421.         procount++;
  1422.         aux = aux->siguiente;
  1423.     }
  1424. /*
  1425.  * Preparamos variable para marcado de procesos
  1426.  */
  1427.     procesos = (int *) malloc( sizeof(int) * procount );
  1428.     pendientes = (int *) malloc( sizeof(int) * procount );
  1429.     for( i = 0; i < procount; i++ )
  1430.         procesos[i] = pendientes[i] = 0;
  1431.  
  1432.     disp = (int *) malloc( sizeof(int) * 4 );
  1433.     disp[0] = recdisp[0];
  1434.     disp[1] = recdisp[1];
  1435.     disp[2] = recdisp[2];
  1436.     disp[3] = recdisp[3];
  1437.    
  1438. /*
  1439.  * Finjimos que damos los recursos
  1440.  */
  1441.     disp[0]-=atoi(op1);disp[1]-=atoi(op2); disp[2]-=atoi(op3); disp[3]-=atoi(op4);
  1442.  
  1443.     ejec = ( int * ) malloc ( sizeof(int) * 4 );
  1444.     ejec[0] = ejecucion->inicio->recAs[0] + atoi(op1);
  1445.     ejec[1] = ejecucion->inicio->recAs[1] + atoi(op2);
  1446.     ejec[2] = ejecucion->inicio->recAs[2] + atoi(op3);
  1447.     ejec[3] = ejecucion->inicio->recAs[3] + atoi(op4);
  1448.    
  1449. /*
  1450.  * Verificamos si no hay más por satisfacer del proceso en ejecución
  1451.  */
  1452.     if( ejec[0] - ejecucion->inicio->recs[0] == 0 && ejec[1] - ejecucion->inicio->recs[1] == 0 && ejec[2] - ejecucion->inicio->recs[2] == 0 && ejec[3] - ejecucion->inicio->recs[3] == 0 ){
  1453.         disp[0]+=ejecucion->inicio->recs[0];
  1454.         disp[1]+=ejecucion->inicio->recs[1];
  1455.         disp[2]+=ejecucion->inicio->recs[2];
  1456.         disp[3]+=ejecucion->inicio->recs[3];
  1457.         procesos[count] = ejecucion->inicio->pid;
  1458.         count++;
  1459.         satisfecho = true;
  1460.     }
  1461.  
  1462. /*
  1463.  * Comienza simulación
  1464.  */
  1465.     aux = listos->inicio;
  1466.  
  1467.     while( suma_marc_pend( procesos, pendientes,procount ) == 0 && listos->inicio != NULL ){
  1468.        
  1469.         if( satisfecho != true && marcado(ejecucion->inicio->pid, pendientes, procount ) != 0 ){
  1470.             if( ejecucion->inicio->recs[0] - ejec[0] <= disp[0] && ejecucion->inicio->recs[1] - ejec[1] <= disp[1] && ejecucion->inicio->recs[2] - ejec[2] <= disp[2] && ejecucion->inicio->recs[3] - ejec[3] <= disp[3] ){
  1471.                 satisfecho = true;
  1472.                 disp[0]+=ejec[0];
  1473.                 disp[1]+=ejec[1];
  1474.                 disp[2]+=ejec[2];
  1475.                 disp[3]+=ejec[3];
  1476.                 procesos[count] = ejecucion->inicio->pid;
  1477.                 count++;
  1478.             }else{
  1479.                 pendientes[pen] = ejecucion->inicio->pid;
  1480.                 pen++;
  1481.             }
  1482.         }
  1483.         if( marcado(aux->pid, procesos, procount ) == 0 || marcado(aux->pid, pendientes, procount) == 0 ){
  1484.             if( aux->siguiente != NULL )
  1485.                 aux = aux->siguiente;
  1486.             else
  1487.                 return 3;
  1488.         }else{
  1489.             if( aux->recs[0] - aux->recAs[0] <= disp[0] && aux->recs[1] - aux->recAs[1] <= disp[1] && aux->recs[2] - aux->recAs[2] <= disp[2] && aux->recs[3] - aux->recAs[3] <= disp[3] ){
  1490.                 disp[0]+=aux->recAs[0];
  1491.                 disp[1]+=aux->recAs[1];
  1492.                 disp[2]+=aux->recAs[2];
  1493.                 disp[3]+=aux->recAs[3];
  1494.                 procesos[count] = aux->pid;
  1495.                 count++;
  1496.                 aux = listos->inicio;
  1497.                 x = 0;
  1498.                 while( pendientes[x] ){
  1499.                     pendientes[x] = 0;
  1500.                     x++;
  1501.                 }
  1502.                 pen = 0;
  1503.             }else{
  1504.                 pendientes[pen] = aux->pid;
  1505.                 pen++;
  1506.             }
  1507.         }
  1508.     }
  1509.     for( x = 0; x < procount && listos->inicio != NULL; x++ )
  1510.         if( procesos[x] == 0 )
  1511.             return 3;
  1512. /*
  1513.  * Si llega aquí la transacción es segura
  1514.  */
  1515.     ejecucion->inicio->recAs[0]+=atoi(op1);
  1516.     ejecucion->inicio->recAs[1]+=atoi(op2);
  1517.     ejecucion->inicio->recAs[2]+=atoi(op3);
  1518.     ejecucion->inicio->recAs[3]+=atoi(op4);
  1519.    
  1520.     recdisp[0]-=atoi(op1);
  1521.     recdisp[1]-=atoi(op2);
  1522.     recdisp[2]-=atoi(op3);
  1523.     recdisp[3]-=atoi(op4);
  1524.  
  1525.     return 0;
  1526. }
  1527.  
  1528. int suma_marc_pend(int *marcados, int *pendientes, int total ){
  1529.     int suma = 0, x;
  1530.     for( x = 0; x < total; x++ )
  1531.         if( marcados[x] != 0 || pendientes[x] != 0 )
  1532.             suma++;
  1533.     if( suma == total )
  1534.         return 1;
  1535.     else
  1536.         return 0;
  1537. }
  1538.  
  1539. int marcado( int pid, int *lista, int total){
  1540.     int x;
  1541.     for( x = 0; x < total; x++ ){
  1542.         if( lista[x] == pid )
  1543.             return 0;
  1544.     }
  1545.     return 1;
  1546. }
  1547.  
  1548. int use( PROCESO *ejecucion, char *op1, char *op2, char *op3, char *op4 ){
  1549.     int random = rand() % 100000;
  1550.     if( ejecucion->recAs[0] <= atoi(op1) || ejecucion->recAs[1] <= atoi(op2) || ejecucion->recAs[2] <= atoi(op3) || ejecucion->recAs[3] <= atoi(op4) )
  1551.         return 1;
  1552.     ejecucion->bloqueo = random;
  1553.     return 0;
  1554. }
  1555.  
  1556. void bloqueadoschk( LISTA *listos, LISTA *bloqueados){
  1557.     PROCESO *actual, *siguiente;
  1558.     if( bloqueados->inicio != NULL ){
  1559.         if( bloqueados->inicio->siguiente == NULL )
  1560.             if( bloqueados->inicio->bloqueo == 0 ){
  1561.                 ins_list( bloqueados->inicio, listos);
  1562.                 bloqueados->num--;
  1563.                 bloqueados->inicio = bloqueados->fin = NULL;
  1564.             }else
  1565.                 bloqueados->inicio->bloqueo--;
  1566.         else{
  1567.             actual = bloqueados->inicio;
  1568.             siguiente = bloqueados->inicio->siguiente;
  1569.             while( actual->siguiente != NULL ){
  1570.                 siguiente = actual->siguiente;
  1571.                 if( siguiente->bloqueo == 0 ){
  1572.                     actual->siguiente = actual->siguiente->siguiente;
  1573.                     ins_list( siguiente, listos);
  1574.                     bloqueados->num--;
  1575.                 }else{
  1576.                     siguiente->bloqueo--;
  1577.                     actual = actual->siguiente;
  1578.                 }
  1579.             }
  1580.             if( bloqueados->inicio != NULL ){
  1581.                 if( bloqueados->inicio->bloqueo == 0 ){
  1582.                     actual = bloqueados->inicio;
  1583.                     bloqueados->inicio = bloqueados->inicio->siguiente;
  1584.                     ins_list( actual, listos);
  1585.                     bloqueados->num--;
  1586.                 }else
  1587.                     bloqueados->inicio->bloqueo--;
  1588.             }
  1589.         }
  1590.     }
  1591. }
  1592.  
  1593. int fre( PROCESO *proc, char *op1, char *op2, char *op3, char *op4, int *recursos ){
  1594.     if( proc->recAs[0] < atoi(op1) || proc->recAs[1] < atoi(op2) || proc->recAs[2] < atoi(op3) || proc->recAs[3] < atoi(op4) )
  1595.         return 1;
  1596.     proc->recAs[0]-=atoi(op1); proc->recAs[1]-=atoi(op2); proc->recAs[2]-=atoi(op3); proc->recAs[3]-=atoi(op4);
  1597.     recursos[0]+=atoi(op1);recursos[1]+=atoi(op2);recursos[2]+=atoi(op3);recursos[3]+=atoi(op4);
  1598.     return 0;
  1599. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement