Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <termios.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "lib/util.h"
- #include "lib/opcad.h"
- #include <ncurses.h>
- #include "lib/opreg.h"
- #include <time.h>
- #define linesize 100
- #define insize 23
- int kbhit(void){
- struct termios oldt, newt;
- int ch;
- int oldf;
- tcgetattr(STDIN_FILENO, &oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
- oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
- fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
- ch = getchar();
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
- fcntl(STDIN_FILENO, F_SETFL, oldf);
- if(ch != EOF){
- ungetc(ch, stdin);
- return 1;
- }
- return 0;
- }
- int com(char *comando, char c, int bfkbsize, char *prompt, int *vel){
- /*
- * Verificando si se teclea Enter
- */
- if( c != 13 ){
- if( c == 27 ){
- c = getchar();
- if( c == 91 ){
- c = getchar();
- if( c == 68 ){
- if( *vel < 1000 )
- *vel += 50;
- return 0;
- }else if( c == 67 ){
- if( *vel > 0 )
- *vel -= 50;
- return 0;
- }
- }
- }else if( c == 16 ){
- move(1,10);clrtoeol();printw("PAUSA");move(0,strlen(prompt)+strlen(comando));refresh();
- while(1){
- noecho();
- if( kbhit() ){
- if( getchar() == 16 )
- break;
- }
- }
- move(0,0);clrtoeol();printw("%s %s",prompt,comando);
- move(1,10);printw("CONTINUANDO");move(0,strlen(prompt)+strlen(comando));refresh();
- }
- if( strlen(comando) >= bfkbsize ){
- printw("%c",beep());
- comando[bfkbsize-1] = '\0';
- }else{
- /*
- * Verificando si no es Delete
- */
- if( c != 127 && c != 16 ){
- armaComando( comando,c );
- printw("%c",c);
- }
- else{
- if( strlen( comando ) == 0 ){
- printw("%c",beep());
- }else{
- noecho();
- comando[strlen(comando)-1] = '\0';
- move(0,0);
- clrtoeol();
- printw("%s%s",prompt,comando);
- }
- }
- }
- refresh();
- return 0;
- }
- if( strlen(comando) == 0){
- printw("%c",beep());
- refresh();
- return 0;
- }
- return 1;
- }
- int compc(char entrada[]){
- int i = 0, j = 0,k;
- int n = strlen(entrada);
- char aux[n+2], aux0[n+1];
- for (k = 0; k<n;k++)
- aux[k]=entrada[k];
- aux[n]=' ';
- aux[n+1]='\0';
- while(aux[i]==' ')
- i++;
- while(aux[i]!=' '){
- aux0[j]=aux[i];
- i++;
- j++;
- }
- aux0[j]='\0';
- for (k = 0; k<strlen(aux0);k++)
- entrada[k] = aux0[k];
- if (compaux(aux0)==1){
- entrada[k] = ' ';
- while(aux[i]==' ')
- {
- if (i==n)
- return 0;
- i++;
- }
- while(aux[i]!=' ')
- {
- if (i==n)
- return 0;
- if (aux[i]>47 && aux[i]<58)
- {
- entrada[k+1]=aux[i];
- i++;
- k++;
- }
- else
- return 0;
- }
- entrada[k+1]='\0';
- return 1;
- }
- else if (compaux(aux0)==2)
- {
- entrada[k] = '\0';
- return 2;
- }
- else if (compaux(aux0)==3){
- entrada[k] = ' ';
- while(aux[i]==' ')
- {
- if (i==n)
- return 0;
- i++;
- }
- while(aux[i]!=' ')
- {
- if (i==n)
- return 0;
- if (aux[i]>47 && aux[i]<58)
- {
- entrada[k+1]=aux[i];
- i++;
- k++;
- }
- else
- return 0;
- }
- i++;
- k++;
- if ( aux[i] == '\0' )
- return 3;
- while( aux[i] != ' ' ){
- if(i==n)
- return 0;
- if (aux[i]>47 && aux[i]<58){
- entrada[k+1]=aux[i];
- i++;k++;
- }
- else
- return 0;
- }
- entrada[k+1]='\0';
- return 3;
- }
- else
- {
- entrada[k] = '\0';
- return 0;
- }
- }
- /*
- * Funcion compaux
- * Funcion auxiliar de la funcion compc
- */
- int compaux(char ent[]){
- if (strcmp(ent,"mata")==0)
- return 1;
- else if (strcmp(ent,"salir")==0)
- return 2;
- else if (strcmp(ent,"fork")==0)
- return 3;
- else
- return 0;
- }
- int inicial( LISTA *lst ){
- lst->num = 0;
- lst->inicio = lst->fin = NULL;
- if( lst->num == 0 && lst->inicio ==NULL )
- return 0;
- else
- return 1;
- }
- void ins_list( PROCESO *nuevo,LISTA *listos){
- if( listos->inicio == NULL ){
- listos->inicio = nuevo;
- listos->fin = nuevo;
- }else{
- listos->fin->siguiente = nuevo;
- listos->fin = nuevo;
- listos->fin->siguiente = NULL;
- }
- listos->num++;
- }
- /*
- * Funcion elim_list
- * Elimina el proceso con el pid (mpid) de la lista listos y la agrega a
- * la lista terminados
- */
- int mata(int mpid, LISTA *listos, LISTA *terminados, LISTA *ejecucion, LISTA *bloqueados, LISTA *suspendidos, int padre, int **tmm, char **ram, int *grps, int *recursos){
- PROCESO *actual, *pad, *aux, *desus;
- bool encontrado = false;
- if( ejecucion->inicio != NULL && ejecucion->inicio->pid == mpid ){
- actual = ejecucion->inicio;
- encontrado = true;
- }else if( listos->inicio != NULL ){
- actual = listos->inicio;
- while( actual->pid != mpid && actual->siguiente != NULL )
- actual = actual->siguiente;
- if ( actual->pid == mpid )
- encontrado = true;
- }
- if( bloqueados->inicio != NULL && encontrado == false ){
- actual = bloqueados->inicio;
- while( actual->pid != mpid && actual->siguiente != NULL )
- actual = actual->siguiente;
- if ( actual->pid == mpid )
- encontrado = true;
- }
- if( suspendidos->inicio != NULL && encontrado == false ){
- actual = suspendidos->inicio;
- while( actual->pid != mpid && actual->siguiente != NULL )
- actual = actual->siguiente;
- encontrado = true;
- if ( actual->pid != mpid )
- encontrado = false;
- }
- if( encontrado == false )
- return 1;
- /*
- * Aquí ya encontró el proceso a matar
- */
- libera( actual, tmm, ram );
- recursos[0]+=actual->recAs[0];
- recursos[1]+=actual->recAs[1];
- recursos[2]+=actual->recAs[2];
- recursos[3]+=actual->recAs[3];
- actual->recAs[0] = actual->recAs[1] = actual->recAs[2] = actual->recAs[3] = 0;
- desus = suspendidos->inicio;
- while( suspendidos->inicio != NULL ){
- suspendidos->inicio = suspendidos->inicio->siguiente;
- ins_list(desus, listos);
- desus = suspendidos->inicio;
- suspendidos->num--;
- }
- if( actual->hijos->inicio != NULL ){
- if ( mata( actual->hijos->inicio->pid, listos,terminados,ejecucion, bloqueados, suspendidos,1,tmm, ram, grps, recursos ) != 0 )
- return 1;
- else
- actual->hijos->inicio = NULL;
- }
- /*
- * 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
- */
- if( padre != 0 ){
- if( actual->hermano != NULL ){
- if ( mata( actual->hermano->pid, listos,terminados,ejecucion, bloqueados, suspendidos, 1, tmm, ram, grps, recursos ) != 0 )
- return 1;
- else
- actual->hermano = NULL;
- }
- }
- if( actual->pid != actual->ppid ){
- pad = busca_padre( actual->ppid, listos );
- if ( pad == NULL )
- pad = busca_padre( actual->ppid, ejecucion );
- if( pad == NULL )
- pad = busca_padre( actual->ppid, bloqueados );
- if( pad == NULL )
- pad = busca_padre( actual->ppid, suspendidos );
- if( pad == NULL )
- return 1;
- aux = pad->hijos->inicio;
- if( aux->pid != actual->pid ){
- if( aux->hermano != NULL )
- aux->hermano = aux->hermano->hermano;
- else
- return 1;
- }
- else{
- pad->hijos->inicio = pad->hijos->inicio->hermano;
- if( pad->hijos->inicio == NULL )
- (*grps)--;
- actual->hermano = NULL;
- }
- }else
- (*grps)--;
- if( elim_list(actual->pid,listos,terminados,ejecucion,suspendidos,bloqueados) != 0 )
- return 1;
- else
- return 0;
- }
- int elim_list(int mpid, LISTA *listos, LISTA *terminados, LISTA *ejecucion, LISTA *suspendidos, LISTA *bloqueados ){
- if( listos->inicio == NULL && ejecucion->inicio == NULL && suspendidos->inicio == NULL && bloqueados->inicio == NULL ){
- return 1;
- }else{
- PROCESO *ant, *act;
- /*
- * Eliminación en ejecución
- */
- if( ejecucion->inicio != NULL ){
- ant = ejecucion->inicio;
- act = ant->siguiente;
- if( ant->pid == mpid ){
- ejecucion->inicio = act;
- ant->siguiente = NULL;
- ins_list(ant, terminados);
- ejecucion->num--;
- if( fclose(terminados->fin->instrucciones) != 0 )
- return 1;
- else
- return 0;
- }
- while( act != NULL ){
- if( act->pid == mpid ){
- ant->siguiente = act->siguiente;
- act->siguiente = NULL;
- ins_list(act, terminados);
- if( fclose(act->instrucciones) != 0 )
- return 1;
- else
- return 0;
- }
- ant = act;
- act = act->siguiente;
- }
- }
- /*
- * Eliminación en listos
- */
- if( listos->inicio != NULL ){
- ant = listos->inicio;
- if ( ant->pid == mpid ){
- if ( listos->fin->pid == mpid ){
- listos->inicio = NULL;
- listos->fin = NULL;
- }else{
- listos->inicio = ant->siguiente;
- listos->num--;
- }
- act = ant;
- act->siguiente = NULL;
- ins_list(act, terminados);
- ant = NULL;
- if( fclose(act->instrucciones) != 0 )
- return 1;
- else
- return 0;
- }else{
- if ( ant->siguiente != NULL ){
- for ( act = ant->siguiente; act != NULL; act = act->siguiente )
- if ( act->pid == mpid ){
- if( act == listos->fin )
- listos->fin = ant;
- ant->siguiente = act->siguiente;
- act->siguiente = NULL;
- ins_list(act, terminados);
- listos->num--;
- ant = NULL;
- if( fclose(act->instrucciones) != 0 )
- return 1;
- else
- return 0;
- }else
- ant = act;
- }
- }
- }
- /*
- * Eliminación en suspendidos
- */
- if( suspendidos->inicio != NULL ){
- ant = suspendidos->inicio;
- if ( ant->pid == mpid ){
- if ( suspendidos->fin->pid == mpid ){
- suspendidos->inicio = NULL;
- suspendidos->fin = NULL;
- }else{
- suspendidos->inicio = ant->siguiente;
- suspendidos->num--;
- }
- act = ant;
- act->siguiente = NULL;
- ins_list(act, terminados);
- ant = NULL;
- if( fclose(act->instrucciones) != 0 )
- return 1;
- else
- return 0;
- }else{
- if ( ant->siguiente != NULL ){
- for ( act = ant->siguiente; act != NULL; act = act->siguiente )
- if ( act->pid == mpid ){
- if( act == suspendidos->fin )
- suspendidos->fin = ant;
- ant->siguiente = act->siguiente;
- act->siguiente = NULL;
- ins_list(act, terminados);
- suspendidos->num--;
- ant = NULL;
- if( fclose(act->instrucciones) != 0 )
- return 1;
- else
- return 0;
- }else
- ant = act;
- }
- }
- }
- /*
- * Eliminación en bloqueados
- */
- if( bloqueados->inicio != NULL ){
- ant = bloqueados->inicio;
- if ( ant->pid == mpid ){
- if ( bloqueados->fin->pid == mpid ){
- bloqueados->inicio = NULL;
- bloqueados->fin = NULL;
- }else{
- bloqueados->inicio = ant->siguiente;
- bloqueados->num--;
- }
- act = ant;
- act->siguiente = NULL;
- ins_list(act, terminados);
- ant = NULL;
- if( fclose(act->instrucciones) != 0 )
- return 1;
- else
- return 0;
- }else{
- if ( ant->siguiente != NULL ){
- for ( act = ant->siguiente; act != NULL; act = act->siguiente )
- if ( act->pid == mpid ){
- if( act == bloqueados->fin )
- bloqueados->fin = ant;
- ant->siguiente = act->siguiente;
- act->siguiente = NULL;
- ins_list(act, terminados);
- bloqueados->num--;
- ant = NULL;
- if( fclose(act->instrucciones) != 0 )
- return 1;
- else
- return 0;
- }else
- ant = act;
- }
- }
- }
- }
- return 1;
- }
- /*
- * Función para levantar los procesos
- */
- PROCESO * levanta_proceso(char *comando, int *pid, int status, int *recursos){
- int i = 0, j = 0;
- char temp[linesize] = "Vacio", OPT[4], R1[4], R2[4], R3[4], R4[4];
- PROCESO *nuevo = (PROCESO *) malloc ( sizeof( PROCESO ) );
- nuevo->instrucciones = fopen(comando,"r");
- move(1,status);clrtoeol();
- if( nuevo->instrucciones == NULL ){
- free(nuevo);
- nuevo = NULL;
- printw("Proceso %s no existe", comando);
- }else{
- fgets( temp, linesize, nuevo->instrucciones );
- for( i = 0; temp[i] && temp[i] == ' '; i++ );
- OPT[0] = temp[i+0]; OPT[1] = temp[i+1]; OPT[2] = temp[i+2]; OPT[3] = '\0';
- if( strcmp(OPT , "MAX") != 0 ){
- free(nuevo);
- printw("Proceso sin cabecera de recursos");
- return NULL;
- }
- OPT[0] = '\0';
- if( parteInstruccion( temp, OPT, R1, R2, R3, R4 ) != 0 ){
- printw("Error en cabecera de recursos");
- return NULL;
- }
- if( recursos[0] < atoi(R1) || recursos[1] < atoi(R2) || recursos[2] < atoi(R3) || recursos[3] < atoi(R4) ){
- printw("Recursos inválidos: R1 <= %d R2 <= %d R3 <= %d R4 <= %d", recursos[0], recursos[1], recursos[2], recursos[3] );
- return NULL;
- }
- /*
- * Aquí el proceso ya es válido y se inicializa
- */
- nuevo->recs = ( int * ) malloc( sizeof( int ) * 4 );
- nuevo->recAs = ( int * ) malloc( sizeof( int ) * 4 );
- nuevo->recs[0] = atoi(R1); nuevo->recs[1] = atoi(R2); nuevo->recs[2] = atoi(R3); nuevo->recs[3] = atoi(R4);
- nuevo->recAs[0] = nuevo->recAs[1] = nuevo->recAs[2] = nuevo->recAs[3] = 0;
- nuevo->pid = (*pid);
- nuevo->ppid = (*pid)++;
- nuevo->AX = 0x0;
- nuevo->BX = 0x0;
- nuevo->CX = 0x0;
- nuevo->DX = 0x0;
- nuevo->PC = 0;
- nuevo->siguiente = NULL;
- nuevo->nombre = (char *) malloc ( sizeof(char) * strlen(comando));
- for( i = 0; comando[i] != '\0'; i++){
- *((nuevo->nombre)+i) = *(comando+i);
- }
- *((nuevo->nombre)+i) = '\0';
- printw("Ultimo Proceso levantado: #%d: %s",nuevo->pid, nuevo->nombre);
- nuevo->hijos = (LISTA *) malloc ( sizeof ( LISTA ) );
- nuevo->hijos->inicio = nuevo->hijos->fin = NULL;
- nuevo->hermano = NULL;
- nuevo->proceso = 0;
- nuevo->grupo = 0;
- nuevo->fp = 0;
- nuevo->bloqueo = 0;
- rewind( nuevo->instrucciones );
- for( i = 0; !feof( nuevo->instrucciones ); i++)
- fgets( temp, linesize, nuevo->instrucciones );
- /*
- * Se resta 1 debido a que la primera instrucción es la instrucción de requerimientos de recursos, no cuenta como instrucción
- */
- rewind( nuevo->instrucciones );
- nuevo->tmp = (int **) malloc ( sizeof( int[2] ) * ( i - 1 ) );
- for ( j = 0; j < i-1; j++ ){
- (nuevo->tmp)[j] = ( int *) malloc ( sizeof( int[2] ) );
- (nuevo->tmp)[j][0] = 0;
- (nuevo->tmp)[j][1] = 0;
- }
- nuevo->lineas = i;
- nuevo->fork = 0;
- }
- return nuevo;
- }
- PROCESO * levanta_hijo(char *comando, int *pid, int status, LISTA *listos, LISTA *ejecucion, int *recursos, LISTA *suspendidos, LISTA *bloqueados ){
- int ppid = 0, pc = 0, i = 0;
- char IR[linesize];
- PROCESO *padre, *actual, *nuevo;
- div_fork( comando, &ppid, &pc );
- move(1,status);clrtoeol();
- /*
- * Búsqueda del padre
- */
- padre = busca_padre(ppid,listos);
- /*
- * Búsqueda en lista de ejecución
- */
- if( padre == NULL )
- padre = busca_padre(ppid,ejecucion);
- if( padre == NULL )
- padre = busca_padre(ppid,suspendidos);
- if( padre == NULL )
- padre = busca_padre(ppid,bloqueados);
- if( padre == NULL ){
- printw("Proceso padre %d no encontrado",ppid);
- return NULL;
- }
- nuevo = levanta_proceso( padre->nombre, pid, status, recursos);
- nuevo->fork = 1;
- if( padre->hijos->inicio != NULL ){
- actual = padre->hijos->inicio;
- while( actual->hermano != NULL )
- actual = actual->hermano;
- actual->hermano = nuevo;
- }else
- padre->hijos->inicio = nuevo;
- nuevo->ppid = ppid;
- /*
- * Ciclo de búsqueda según el pc otorgado
- */
- for ( i = 1; i < pc && !feof(nuevo->instrucciones); i++ )
- fgets( IR, linesize, nuevo->instrucciones );
- if ( feof( nuevo->instrucciones ) ){
- move(1, status);clrtoeol();printw("No existen tantas instrucciones en el archivo");
- return NULL;
- }
- nuevo->PC = pc;
- return nuevo;
- }
- PROCESO * busca_padre( int pid,LISTA *lista ){
- PROCESO *actual;
- actual = lista->inicio;
- if( actual != NULL ){
- while( actual->pid != pid && actual->siguiente != NULL )
- actual = actual->siguiente;
- if( actual->pid == pid )
- return actual;
- }
- return NULL;
- }
- void div_fork( char *comando, int *pid, int *pc ){
- int i = 0, j = 0;
- char temp[10];
- temp[9] = '\0';
- temp[0]='1';
- temp[1]='\0';
- while( comando[i] != ' ' )
- i++;
- i++;
- while( comando[i] != ' ' && comando[i] != '\0' ){
- temp[j] = comando[i];
- j++;i++;
- }
- temp[j] = '\0';
- *pid = atoi(temp);
- temp[0] = '\0';
- while( comando[i] == ' ' )
- i++;
- j = 0;
- while( comando[i] != ' ' && comando[i] != '\0' ){
- temp[j] = comando[i];
- i++;j++;
- }
- temp[j] = '\0';
- if ( strlen( temp ) == 0 )
- *pc = 1;
- else
- *pc = atoi(temp);
- }
- int traduce( int tmp, int pc ){
- tmp*=2;
- if( pc%2 != 0 )
- tmp+=1;
- return tmp;
- }
- 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 ){
- int n = 0, i = 0, c = 0, x, ram_ind = 0, *used_frame, marcos;
- char temp[linesize], IR[linesize], codop[4], op1[3], op2[16], op3[3], op4[3];
- enum codops{ERROR, MOV, ADD, SUB, MUL, DIV, INC, DEC, END, JNZ, MAX, GET, USE, FRE}op;
- move(2,10);printw("EXEC");move(0,comandprompt);refresh();
- int ppid = ejecucion->inicio->ppid;
- PROCESO *paux;
- /*
- * used_frame contendrá los indices de los frames usados por el proceso en ejecución
- */
- used_frame = (int *) malloc ( sizeof(int) * 3 );
- for ( n = 0; n < 3; n++ )
- used_frame[n] = -1;
- for( n = 0; n < timer; n++ ){
- /* regulador de velocidad, usado solo para esperar y que la ejecución sea visible por el usuario*/
- napms(vel);
- if( ejecucion->inicio->PC >= ejecucion->inicio->lineas || ejecucion->inicio->PC < 0 )
- return 1;
- if( ejecucion->inicio->tmp[ejecucion->inicio->PC][0] != 1 ){
- ejecucion->inicio->fp++;
- marcos = lleno( ejecucion->inicio, used_frame );
- if( marcos >= 3 ){
- if ( switch_frame( ejecucion->inicio, tmm, ram, used_frame, marcos ) != 0 ){
- move(1,10);clrtoeol();
- printw("Fallo de intercambio de páginas");refresh();sleep(5);
- return 1;
- }
- }else if( switch_other( ejecucion->inicio, tmm, ram ) != 0 ){
- move(1,10);clrtoeol();
- printw("Fallo de intercambio de páginas");refresh();sleep(5);
- }
- }
- ram_ind = traduce( ejecucion->inicio->tmp[ejecucion->inicio->PC][1], ejecucion->inicio->PC);
- for( x = 0; x < 100 && ram[ram_ind][x+1] != '\0'; x++ )
- IR[x] = ram[ram_ind][x];
- IR[x] = '\0';
- /*
- * Es necesario reinicialiciar i y c
- */
- i = c = 0;
- /*
- * Eliminamos los espacios al final de la linea
- */
- while( IR[strlen(IR)-1] == ' ' || IR[strlen(IR)-1] == '\n')
- IR[strlen(IR)-1] = '\0';
- /*
- * Eliminamos los espacios al inicio de la linea
- */
- while( IR[i] == ' ' )
- i++;
- while( IR[i] != '\0' ){
- temp[c] = IR[i];
- i++;c++;
- }
- temp[i] = '\0';i = 0;
- while( temp[i] != '\0'){
- IR[i] = temp[i];
- i++;
- }
- IR[i] = '\0';
- /*
- * Verificamos que no sea una instrucción muy larga o vacía
- */
- if( strlen(IR) > insize )
- return 1;
- else
- if( strlen(IR) == 0 )
- return 1;
- /*
- * Para nuestra función requerimos un espacio al final de la instrucción, se lo insertamos
- */
- if( parteInstruccion(IR,codop,op1,op2,op3,op4) != 0 ){
- return 1;}
- else if(strcmp(codop,"ADD") == 0)
- op = ADD;
- else if(strcmp(codop,"MOV") == 0)
- op = MOV;
- else if(strcmp(codop,"SUB") == 0)
- op = SUB;
- else if(strcmp(codop,"MUL") == 0)
- op = MUL;
- else if(strcmp(codop,"DIV") == 0)
- op = DIV;
- else if(strcmp(codop,"INC") == 0)
- op = INC;
- else if(strcmp(codop,"DEC") == 0)
- op = DEC;
- else if(strcmp(codop,"END") == 0)
- op = END;
- else if(strcmp(codop,"JNZ") == 0)
- op = JNZ;
- else if(strcmp(codop,"MAX") == 0)
- op = MAX;
- else if(strcmp(codop,"GET") == 0)
- op = GET;
- else if(strcmp(codop,"USE") == 0)
- op = USE;
- else if(strcmp(codop,"FRE") == 0)
- op = FRE;
- else
- op = ERROR;
- ram_print( ram ); tmm_print( tmm );// tmp_print( ejecucion );
- move(7,2);clrtoeol();
- 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);}
- 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);
- refresh();
- ram_print(ram);tmm_print(tmm);//tmp_print( ejecucion);
- switch(op){
- case ADD:
- if( add(op1,op2,AX,BX,CX,DX) != 0 )
- return 1;
- break;
- case MOV:
- if( mov(op1,op2,AX,BX,CX,DX) != 0 )
- return 1;
- break;
- case SUB:
- if( sub(op1,op2,AX,BX,CX,DX) != 0 )
- return 1;
- break;
- case MUL:
- if( mul(op1,op2,AX,BX,CX,DX) != 0 )
- return 1;
- break;
- case DIV:
- if( divi(op1,op2,AX,BX,CX,DX) != 0 )
- return 1;
- break;
- case INC:
- if( inc(op1,AX,BX,CX,DX) != 0 )
- return 1;
- break;
- case DEC:
- if( dec(op1,AX,BX,CX,DX) != 0 )
- return 1;
- break;
- case END:
- if( end(IR) != 0 )
- return 1;
- break;
- case JNZ:
- if( jump(op1, CX, &(ejecucion->inicio->PC)) != 0 )
- return 1;
- break;
- case GET:
- i = get( recursos, recdis, op1, op2, op3, op4, listos, ejecucion );
- if( i == 1 ){
- move(1,10);clrtoeol();printw("Petición ilegal de recursos");refresh();sleep(2);
- return 1;
- }
- if( i == 2 ){
- move(1,10);clrtoeol();printw("Recursos disponibles no suficientes");refresh();
- ins_list( ejecucion->inicio, suspendidos );
- ejecucion->num--;
- ejecucion->inicio = ejecucion->fin = NULL;
- return 0;
- }
- if( i == 3 ){
- move(1,10);clrtoeol();printw("Prestamo inseguro");refresh();
- ins_list( ejecucion->inicio, suspendidos );
- ejecucion->num--;
- ejecucion->inicio = ejecucion->fin = NULL;
- return 0;
- }
- break;
- case USE:
- i = use( ejecucion->inicio, op1, op2, op3, op4 );
- if( i == 1 ){
- move(1,10);clrtoeol();printw("Uso ilegal de recursos");refresh();sleep(2);
- return 1;
- }
- ejecucion->inicio->PC++;
- ins_list( ejecucion->inicio, bloqueados );
- ejecucion->num--;
- ejecucion->inicio = ejecucion->fin = NULL;
- return 0;
- break;
- case FRE:
- i = fre( ejecucion->inicio, op1, op2, op3, op4, recdis );
- if( i == 1 ){
- move(1,10);clrtoeol();printw("Liberacion ilegal de recursos");refresh();sleep(100);
- return 1;
- }
- if( suspendidos->inicio != NULL ){
- paux = suspendidos->inicio;
- while( paux != NULL ){
- suspendidos->inicio = suspendidos->inicio->siguiente;
- ins_list( paux, listos );
- suspendidos->num--;
- paux = suspendidos->inicio;
- }
- }
- break;
- default:
- return 1;
- break;
- }
- if ( op != JNZ )
- ejecucion->inicio->PC++;
- ejecucion->inicio->proceso+=20;
- ejecucion->inicio->grupo+=20;
- for ( paux = listos->inicio; paux != NULL; paux = paux->siguiente ){
- if (paux->ppid==ppid)
- paux->grupo+=20;
- }
- napms(vel);
- if( end(IR) == 0 )
- break;
- }
- if( strcmp(IR, "END") == 0 )
- return 2;
- return 0;
- }
- int switch_frame( PROCESO *proc, int **tmm, char **ram, int *used, int marcos ){
- int x, viejo = used[0];
- char *temp;
- temp = (char *) malloc ( sizeof(char) * 100 );
- temp = lee_ins( proc->instrucciones, proc->PC );
- if( strcmp(temp,"ERROR") == 0 )
- return 1;
- /*
- * Encontrar el frame más viejo de los usados por el mismo proceso
- */
- for( x = 0; x < 3; x++ ){
- if( tmm[viejo][0] < tmm[used[x]][0])
- viejo = used[x];
- }
- /*
- * Encontrar las lineas que estaban en el frame a reemplazar
- */
- for( x = 0; x < proc->lineas-1; x++ )
- if( proc->tmp[x][1] == viejo )
- proc->tmp[x][1] = proc->tmp[x][0] = 0;
- proc->tmp[proc->PC][0] = 1;
- proc->tmp[proc->PC][1] = viejo;
- for( x = 0; x < 16; x++ )
- if( tmm[x][0] != 0 )
- tmm[x][0]++;
- tmm[viejo][0] = 1;
- ram[viejo*2] = temp;
- if( strcmp( lee_ins( proc->instrucciones, proc->PC+1 ), "ERROR" ) != 0 ){
- ram[viejo*2+1] = lee_ins( proc->instrucciones, proc->PC+1 );
- proc->tmp[proc->PC+1][0] = 1;
- proc->tmp[proc->PC+1][1] = viejo;
- }else
- ram[viejo*2+1] = "Vacio";
- return 0;
- }
- int switch_other( PROCESO *proc, int **tmm, char **ram ){
- 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;
- char *temp;
- used_frame = (int *) malloc ( sizeof(int) * 3 );
- do{
- viejo = 0;
- while( tempTMM[viejo] == 1 && viejo < 16 )
- viejo++;
- if( viejo < 16 ){
- for( i = 0; i < 16; i++ ){
- if( tmm[i][0] < tmm[viejo][0] && tempTMM[i] != 1 )
- viejo = i;
- }
- for( i = 0; i < 16; i++ ){
- if( tmm[viejo][1] == tmm[i][1] )
- filled++;
- }
- if( filled <= 1 ){
- tempTMM[viejo] = 1;
- for( i = 0; i < 16; i++ )
- if( tempTMM[i] == 0 )
- break;
- viejo = -1;
- }
- else
- break;
- }
- }while( i < 16 );
- if ( viejo == -1 ){
- marcos = lleno( proc, used_frame );
- x = tmm[0][0];
- for ( j=0; j<16; j++ )
- if (x<tmm[j][0])
- x=tmm[j][0];
- if ( switch_frame( proc, tmm, ram, used_frame, marcos ) != 0 )
- return 1;
- else
- return 0;
- }
- else{
- temp = (char *) malloc ( sizeof(char) * 100 );
- rewind( proc->instrucciones );
- for( j = 0; j <= proc->PC && !feof(proc->instrucciones) ; j++ )
- fgets( temp, 100, proc->instrucciones );
- if( feof( proc->instrucciones ) )
- return 1;
- x = tmm[0][0];
- for ( j=0; j<16; j++ )
- if (x<tmm[j][0])
- x=tmm[j][0];
- tmm[viejo][0]=x+1;
- tmm[viejo][1]=proc->pid;
- ram[viejo*2]=temp;
- proc->tmp[proc->PC-1][0]=1;
- proc->tmp[proc->PC-1][1]=viejo;
- fgets( temp, 100, proc->instrucciones );
- if( feof( proc->instrucciones ) )
- return 0;
- ram[viejo*2+1]=temp;
- proc->tmp[proc->PC][0]=1;
- proc->tmp[proc->PC][1]=viejo;
- rewind(proc->instrucciones);
- return 0;
- }
- }
- int lleno( PROCESO *proc, int *used ){
- int marcos = 0, x;
- for( x = 0; x < (proc->lineas)-1; x++ )
- if( proc->tmp[x][0] != 0 ){
- if ( marcos == 0 ){
- used[marcos] = proc->tmp[x][1];
- marcos++;
- }
- else if ( used[marcos-1] != proc->tmp[x][1] ){
- used[marcos] = proc->tmp[x][1];
- marcos++;
- }
- }
- return marcos;
- }
- 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){
- /*
- * Se guarda contexto en caso de haber un proceso en ejecución
- */
- if( ejecucion->inicio != NULL ){
- ejecucion->inicio->AX = *AX;
- ejecucion->inicio->BX = *BX;
- ejecucion->inicio->CX = *CX;
- ejecucion->inicio->DX = *DX;
- ins_list( ejecucion->inicio, listos );
- ejecucion->num--;
- ejecucion->inicio = NULL;
- ejecucion->fin = NULL;
- }
- if( listos->inicio != NULL ){
- PROCESO *mp, *aux, *ant, *aux2;
- mp = listos->inicio;
- if (mp->siguiente == NULL){
- mp->proceso = (int)(mp->proceso/2);
- mp->grupo = (int)(mp->grupo/2);
- mp->prioridad = base+(mp->proceso/2)+(mp->grupo/4);
- ejecucion->inicio = mp;
- (ejecucion->num)++;
- ejecucion->fin = ejecucion->inicio;
- listos->inicio = listos->fin = NULL;
- (listos->num)--;
- *AX = ejecucion->inicio->AX;
- *BX = ejecucion->inicio->BX;
- *CX = ejecucion->inicio->CX;
- *DX = ejecucion->inicio->DX;
- return 0;
- }
- else{
- aux2 = ant = mp;
- for(aux=mp; aux!=NULL;aux=aux->siguiente)
- {
- aux->proceso = (int)(aux->proceso/2);
- aux->grupo = (int)(aux->grupo/2);
- aux->prioridad = base+(aux->proceso/2)+(aux->grupo/(4*wk));
- if ((mp->prioridad) > (aux->prioridad)){
- aux2 = ant;
- mp = aux;
- }
- ant = aux;
- }
- ejecucion->inicio = mp;
- (ejecucion->num)++;
- ejecucion->fin = ejecucion->inicio;
- if (aux2->pid == mp->pid)
- listos->inicio = mp->siguiente;
- else if (mp->pid == listos->fin->pid){
- listos->fin = aux2;
- listos->fin->siguiente=NULL;
- }
- else
- aux2->siguiente = mp->siguiente;
- mp->siguiente = NULL;
- (listos->num)--;
- *AX = ejecucion->inicio->AX;
- *BX = ejecucion->inicio->BX;
- *CX = ejecucion->inicio->CX;
- *DX = ejecucion->inicio->DX;
- mp=aux2=aux=ant=NULL;
- return 0;
- }
- }
- else
- return 1;
- }
- void grupo( PROCESO *ejecutando ){
- ejecutando->grupo+=20;
- if( ejecutando->hijos->inicio != NULL )
- grupo( ejecutando->hijos->inicio );
- if( ejecutando->hermano != NULL )
- grupo( ejecutando->hermano );
- }
- int imprime (LISTA *ejecucion, LISTA *listos, LISTA *finalizados, LISTA *nuevos, LISTA *suspendidos, LISTA *bloqueados){
- PROCESO * imprimiendo = NULL;
- int n = 13, x = 0;
- if( ejecucion->inicio != NULL )
- imprimiendo = ejecucion->inicio;
- while( imprimiendo != NULL ){
- move(n,0);clrtoeol();
- move(n,3);printw("%d",imprimiendo->pid);
- move(n,10);printw("%llX",imprimiendo->AX);
- move(n,22);printw("%llX",imprimiendo->BX);
- move(n,34);printw("%llX",imprimiendo->CX);
- move(n,46);printw("%llX",imprimiendo->DX);
- move(n,58);printw("%d",imprimiendo->PC);
- move(n,68);printw("Ejecutando");
- move(n,83);printw("%s",imprimiendo->nombre);
- move(n,96);printw("%d",imprimiendo->ppid);
- move(n,105);printw("%f",imprimiendo->prioridad);
- move(n,120);printw("%d",imprimiendo->proceso);
- move(n,125);printw("%d",imprimiendo->grupo);
- move(n,132);printw("%d",imprimiendo->fp);
- move(n,137);printw("%d",imprimiendo->recAs[0]);
- move(n,143);printw("%d",imprimiendo->recAs[1]);
- move(n,149);printw("%d",imprimiendo->recAs[2]);
- move(n,155);printw("%d",imprimiendo->recAs[3]);
- imprimiendo = imprimiendo->siguiente;
- n++;
- }
- if( listos->inicio != NULL ) imprimiendo = listos->inicio;
- while( imprimiendo != NULL ){
- move(n,0);clrtoeol();
- move(n,3);printw("%d",imprimiendo->pid);
- move(n,10);printw("%llX",imprimiendo->AX);
- move(n,22);printw("%llX",imprimiendo->BX);
- move(n,34);printw("%llX",imprimiendo->CX);
- move(n,46);printw("%llX",imprimiendo->DX);
- move(n,58);printw("%d",imprimiendo->PC);
- move(n,68);printw("Listo");
- move(n,83);printw("%s",imprimiendo->nombre);
- move(n,96);printw("%d",imprimiendo->ppid);
- move(n,105);printw("%f",imprimiendo->prioridad);
- move(n,120);printw("%d",imprimiendo->proceso);
- move(n,125);printw("%d",imprimiendo->grupo);
- move(n,132);printw("%d",imprimiendo->fp);
- move(n,137);printw("%d",imprimiendo->recAs[0]);
- move(n,143);printw("%d",imprimiendo->recAs[1]);
- move(n,149);printw("%d",imprimiendo->recAs[2]);
- move(n,155);printw("%d",imprimiendo->recAs[3]);
- imprimiendo = imprimiendo->siguiente;
- n++;
- }
- if( nuevos->inicio != NULL ) imprimiendo = nuevos->inicio;
- while( imprimiendo != NULL ){
- move(n,0);clrtoeol();
- move(n,3);printw("%d",imprimiendo->pid);
- move(n,10);printw("%llX",imprimiendo->AX);
- move(n,22);printw("%llX",imprimiendo->BX);
- move(n,34);printw("%llX",imprimiendo->CX);
- move(n,46);printw("%llX",imprimiendo->DX);
- move(n,58);printw("%d",imprimiendo->PC);
- move(n,68);printw("Nuevo");
- move(n,83);printw("%s",imprimiendo->nombre);
- move(n,96);printw("%d",imprimiendo->ppid);
- move(n,105);printw("%f",imprimiendo->prioridad);
- move(n,120);printw("%d",imprimiendo->proceso);
- move(n,125);printw("%d",imprimiendo->grupo);
- move(n,132);printw("%d",imprimiendo->fp);
- move(n,137);printw("%d",imprimiendo->recAs[0]);
- move(n,143);printw("%d",imprimiendo->recAs[1]);
- move(n,149);printw("%d",imprimiendo->recAs[2]);
- move(n,155);printw("%d",imprimiendo->recAs[3]);
- imprimiendo = imprimiendo->siguiente;
- n++;
- }
- if( suspendidos->inicio != NULL ) imprimiendo = suspendidos->inicio;
- while( imprimiendo != NULL ){
- move(n,0);clrtoeol();
- move(n,3);printw("%d",imprimiendo->pid);
- move(n,10);printw("%llX",imprimiendo->AX);
- move(n,22);printw("%llX",imprimiendo->BX);
- move(n,34);printw("%llX",imprimiendo->CX);
- move(n,46);printw("%llX",imprimiendo->DX);
- move(n,58);printw("%d",imprimiendo->PC);
- move(n,68);printw("Suspendido");
- move(n,83);printw("%s",imprimiendo->nombre);
- move(n,96);printw("%d",imprimiendo->ppid);
- move(n,105);printw("%f",imprimiendo->prioridad);
- move(n,120);printw("%d",imprimiendo->proceso);
- move(n,125);printw("%d",imprimiendo->grupo);
- move(n,132);printw("%d",imprimiendo->fp);
- move(n,137);printw("%d",imprimiendo->recAs[0]);
- move(n,143);printw("%d",imprimiendo->recAs[1]);
- move(n,149);printw("%d",imprimiendo->recAs[2]);
- move(n,155);printw("%d",imprimiendo->recAs[3]);
- imprimiendo = imprimiendo->siguiente;
- n++;
- }
- if( bloqueados->inicio != NULL ) imprimiendo = bloqueados->inicio;
- while( imprimiendo != NULL ){
- move(n,0);clrtoeol();
- move(n,3);printw("%d",imprimiendo->pid);
- move(n,10);printw("%llX",imprimiendo->AX);
- move(n,22);printw("%llX",imprimiendo->BX);
- move(n,34);printw("%llX",imprimiendo->CX);
- move(n,46);printw("%llX",imprimiendo->DX);
- move(n,58);printw("%d",imprimiendo->PC);
- move(n,68);printw("Bloqueado");
- move(n,83);printw("%s",imprimiendo->nombre);
- move(n,96);printw("%d",imprimiendo->ppid);
- move(n,105);printw("%f",imprimiendo->prioridad);
- move(n,120);printw("%d",imprimiendo->proceso);
- move(n,125);printw("%d",imprimiendo->grupo);
- move(n,132);printw("%d",imprimiendo->fp);
- move(n,137);printw("%d",imprimiendo->recAs[0]);
- move(n,143);printw("%d",imprimiendo->recAs[1]);
- move(n,149);printw("%d",imprimiendo->recAs[2]);
- move(n,155);printw("%d",imprimiendo->recAs[3]);
- imprimiendo = imprimiendo->siguiente;
- n++;
- }
- if( finalizados->inicio != NULL ) imprimiendo = finalizados->inicio;
- while( imprimiendo != NULL ){
- move(n,0);clrtoeol();
- move(n,3);printw("%d",imprimiendo->pid);
- move(n,10);printw("%llX",imprimiendo->AX);
- move(n,22);printw("%llX",imprimiendo->BX);
- move(n,34);printw("%llX",imprimiendo->CX);
- move(n,46);printw("%llX",imprimiendo->DX);
- move(n,58);printw("%d",imprimiendo->PC);
- move(n,68);printw("Finalizado");
- move(n,83);printw("%s",imprimiendo->nombre);
- move(n,96);printw("%d",imprimiendo->ppid);
- move(n,105);printw("%f",imprimiendo->prioridad);
- move(n,120);printw("%d",imprimiendo->proceso);
- move(n,125);printw("%d",imprimiendo->grupo);
- move(n,132);printw("%d",imprimiendo->fp);
- move(n,137);printw("%d",imprimiendo->recAs[0]);
- move(n,143);printw("%d",imprimiendo->recAs[1]);
- move(n,149);printw("%d",imprimiendo->recAs[2]);
- move(n,155);printw("%d",imprimiendo->recAs[3]);
- imprimiendo = imprimiendo->siguiente;
- n++;
- }
- for( x = n; x < 200; x++ ){
- move(x,0);
- clrtoeol();
- }
- return 0;
- }
- void ram_print( char **ram ){
- int x, y = 0;
- move(2,162);printw("+-----RAM-----+");
- for( x = 0; x < 32; x++){
- if( x%2 == 0 ){
- y++;
- move(x+2+y,162);
- printw("||===========||");
- }
- move(x+3+y,162);clrtoeol();
- printw("||%s",ram[x]);
- move(x+3+y,175);
- printw("||");
- }
- move(x+3+y,162);
- printw("||===========||");
- move(x+4+y,162);
- printw("++-----------++");
- }
- void tmm_print( int **tmm ){
- int x;
- move(8,182);printw("+--TMM--+");
- for( x = 0; x < 16; x++ ){
- move(9+x, 182);printw("| %d, %d ",tmm[x][0],tmm[x][1]);
- move(9+x, 190);printw("|");
- }
- move(9+x,182);printw("+-------+");
- }
- void tmp_print( LISTA *ejecucion ){
- int x;
- move(8,192);printw("+--TMP--+");
- for( x = 0; x < ejecucion->inicio->lineas - 1; x++ ){
- move(9+x, 192);printw("| %d, %d ",(ejecucion->inicio->tmp)[x][0],(ejecucion->inicio->tmp)[x][1]);
- move(9+x, 200);printw("|");
- }
- move(9+x,192);printw("+-------+");
- }
- int free_frame( int **tmm ){
- int x;
- for( x = 0; x < 16 ; x++ )
- if( tmm[x][0] == 0 )
- break;
- if( x >= 16 )
- return -1;
- else
- return x;
- }
- /*
- * Función que saca un proceso del frente de la lista origen y lo pone al final de la lista destino
- */
- int swap( LISTA *origen, LISTA *destino ){
- PROCESO *temp;
- if( origen->inicio == NULL )
- return 1;
- temp = origen->inicio;
- if( origen->inicio == origen->fin )
- origen->inicio = origen->fin = NULL;
- else
- origen->inicio = origen->inicio->siguiente;
- ins_list( temp, destino );
- origen->num--;
- return 0;
- }
- char *lee_ins( FILE *archivo, int ins ){
- char *temp;
- int x;
- temp = (char *) malloc ( sizeof(char) * 100 );
- rewind( archivo );
- for( x = 1; x <= ins+2 && !feof(archivo) ; x++ )
- fgets( temp, 100, archivo );
- if( feof(archivo) && x != ins )
- return "ERROR";
- return temp;
- }
- void libera( PROCESO *proc, int **tmm, char **ram){
- int x;
- for( x = 0; x < 16; x++ ){
- if( tmm[x][0] != 0 && tmm[x][1] == proc->pid ){
- ram[x*2] = ram[x*2+1] = "Vacio";
- tmm[x][0] = tmm[x][1] = 0;
- }
- }
- }
- int get( int *rectot, int *recdisp, char *op1, char *op2, char *op3, char *op4, LISTA *listos, LISTA *ejecucion ){
- int procount = 1, i, *procesos, *pendientes;
- PROCESO *aux;
- /*
- * Verificando legalidad de petición de recursos
- */
- if( rectot[0] < atoi(op1) || rectot[1] < atoi(op2) || rectot[2] < atoi(op3) || rectot[3] < atoi(op4) ||
- (ejecucion->inicio->recAs[0] + atoi(op1)) > ejecucion->inicio->recs[0] ||
- (ejecucion->inicio->recAs[1] + atoi(op2)) > ejecucion->inicio->recs[1] ||
- (ejecucion->inicio->recAs[2] + atoi(op2)) > ejecucion->inicio->recs[2] ||
- (ejecucion->inicio->recAs[3] + atoi(op2)) > ejecucion->inicio->recs[3] )
- return 1;
- /*
- * Verificando disponibilidad de recursos
- */
- if( recdisp[0] < atoi(op1) || recdisp[1] < atoi(op2) || recdisp[2] < atoi(op3) || recdisp[3] < atoi(op4) )
- return 2;
- /*
- * Aplicación del algoritmo banquero para calcular seguridad de prestamo de recursos
- */
- int *disp, count = 0, *ejec, x, pen = 0;
- bool satisfecho = false;
- aux = listos->inicio;
- while( aux != NULL ){
- procount++;
- aux = aux->siguiente;
- }
- /*
- * Preparamos variable para marcado de procesos
- */
- procesos = (int *) malloc( sizeof(int) * procount );
- pendientes = (int *) malloc( sizeof(int) * procount );
- for( i = 0; i < procount; i++ )
- procesos[i] = pendientes[i] = 0;
- disp = (int *) malloc( sizeof(int) * 4 );
- disp[0] = recdisp[0];
- disp[1] = recdisp[1];
- disp[2] = recdisp[2];
- disp[3] = recdisp[3];
- /*
- * Finjimos que damos los recursos
- */
- disp[0]-=atoi(op1);disp[1]-=atoi(op2); disp[2]-=atoi(op3); disp[3]-=atoi(op4);
- ejec = ( int * ) malloc ( sizeof(int) * 4 );
- ejec[0] = ejecucion->inicio->recAs[0] + atoi(op1);
- ejec[1] = ejecucion->inicio->recAs[1] + atoi(op2);
- ejec[2] = ejecucion->inicio->recAs[2] + atoi(op3);
- ejec[3] = ejecucion->inicio->recAs[3] + atoi(op4);
- /*
- * Verificamos si no hay más por satisfacer del proceso en ejecución
- */
- 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 ){
- disp[0]+=ejecucion->inicio->recs[0];
- disp[1]+=ejecucion->inicio->recs[1];
- disp[2]+=ejecucion->inicio->recs[2];
- disp[3]+=ejecucion->inicio->recs[3];
- procesos[count] = ejecucion->inicio->pid;
- count++;
- satisfecho = true;
- }
- /*
- * Comienza simulación
- */
- aux = listos->inicio;
- while( suma_marc_pend( procesos, pendientes,procount ) == 0 && listos->inicio != NULL ){
- if( satisfecho != true && marcado(ejecucion->inicio->pid, pendientes, procount ) != 0 ){
- 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] ){
- satisfecho = true;
- disp[0]+=ejec[0];
- disp[1]+=ejec[1];
- disp[2]+=ejec[2];
- disp[3]+=ejec[3];
- procesos[count] = ejecucion->inicio->pid;
- count++;
- }else{
- pendientes[pen] = ejecucion->inicio->pid;
- pen++;
- }
- }
- if( marcado(aux->pid, procesos, procount ) == 0 || marcado(aux->pid, pendientes, procount) == 0 ){
- if( aux->siguiente != NULL )
- aux = aux->siguiente;
- else
- return 3;
- }else{
- 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] ){
- disp[0]+=aux->recAs[0];
- disp[1]+=aux->recAs[1];
- disp[2]+=aux->recAs[2];
- disp[3]+=aux->recAs[3];
- procesos[count] = aux->pid;
- count++;
- aux = listos->inicio;
- x = 0;
- while( pendientes[x] ){
- pendientes[x] = 0;
- x++;
- }
- pen = 0;
- }else{
- pendientes[pen] = aux->pid;
- pen++;
- }
- }
- }
- for( x = 0; x < procount && listos->inicio != NULL; x++ )
- if( procesos[x] == 0 )
- return 3;
- /*
- * Si llega aquí la transacción es segura
- */
- ejecucion->inicio->recAs[0]+=atoi(op1);
- ejecucion->inicio->recAs[1]+=atoi(op2);
- ejecucion->inicio->recAs[2]+=atoi(op3);
- ejecucion->inicio->recAs[3]+=atoi(op4);
- recdisp[0]-=atoi(op1);
- recdisp[1]-=atoi(op2);
- recdisp[2]-=atoi(op3);
- recdisp[3]-=atoi(op4);
- return 0;
- }
- int suma_marc_pend(int *marcados, int *pendientes, int total ){
- int suma = 0, x;
- for( x = 0; x < total; x++ )
- if( marcados[x] != 0 || pendientes[x] != 0 )
- suma++;
- if( suma == total )
- return 1;
- else
- return 0;
- }
- int marcado( int pid, int *lista, int total){
- int x;
- for( x = 0; x < total; x++ ){
- if( lista[x] == pid )
- return 0;
- }
- return 1;
- }
- int use( PROCESO *ejecucion, char *op1, char *op2, char *op3, char *op4 ){
- int random = rand() % 100000;
- if( ejecucion->recAs[0] <= atoi(op1) || ejecucion->recAs[1] <= atoi(op2) || ejecucion->recAs[2] <= atoi(op3) || ejecucion->recAs[3] <= atoi(op4) )
- return 1;
- ejecucion->bloqueo = random;
- return 0;
- }
- void bloqueadoschk( LISTA *listos, LISTA *bloqueados){
- PROCESO *actual, *siguiente;
- if( bloqueados->inicio != NULL ){
- if( bloqueados->inicio->siguiente == NULL )
- if( bloqueados->inicio->bloqueo == 0 ){
- ins_list( bloqueados->inicio, listos);
- bloqueados->num--;
- bloqueados->inicio = bloqueados->fin = NULL;
- }else
- bloqueados->inicio->bloqueo--;
- else{
- actual = bloqueados->inicio;
- siguiente = bloqueados->inicio->siguiente;
- while( actual->siguiente != NULL ){
- siguiente = actual->siguiente;
- if( siguiente->bloqueo == 0 ){
- actual->siguiente = actual->siguiente->siguiente;
- ins_list( siguiente, listos);
- bloqueados->num--;
- }else{
- siguiente->bloqueo--;
- actual = actual->siguiente;
- }
- }
- if( bloqueados->inicio != NULL ){
- if( bloqueados->inicio->bloqueo == 0 ){
- actual = bloqueados->inicio;
- bloqueados->inicio = bloqueados->inicio->siguiente;
- ins_list( actual, listos);
- bloqueados->num--;
- }else
- bloqueados->inicio->bloqueo--;
- }
- }
- }
- }
- int fre( PROCESO *proc, char *op1, char *op2, char *op3, char *op4, int *recursos ){
- if( proc->recAs[0] < atoi(op1) || proc->recAs[1] < atoi(op2) || proc->recAs[2] < atoi(op3) || proc->recAs[3] < atoi(op4) )
- return 1;
- proc->recAs[0]-=atoi(op1); proc->recAs[1]-=atoi(op2); proc->recAs[2]-=atoi(op3); proc->recAs[3]-=atoi(op4);
- recursos[0]+=atoi(op1);recursos[1]+=atoi(op2);recursos[2]+=atoi(op3);recursos[3]+=atoi(op4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement