Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <ncurses.h>
- #include <unistd.h>
- #include <string.h>
- #include "lib/opreg.h"
- #include "lib/opcad.h"
- #include "lib/util.h"
- #define bfkbsize 100
- #define prompt "[cipbri@prompt maquina ]$ "
- #define timer 3
- #define status 10
- #define base 60
- #define frames 16
- /*
- * Definición de cantidad de recursos
- */
- #define R1 15
- #define R2 9
- #define R3 5
- #define R4 20
- int main( int argc, char **argv ){
- /*
- * Declaraciones
- */
- char comando[bfkbsize], **ram, *ins_temp;
- LISTA listos, finalizados, ejecucion, nuevos, bloqueados, suspendidos;
- PROCESO *nuevo = NULL;
- int proc = 1, mpid = -1, grps = 0, vel = 200, x = 0, b, **tmm, ff = -1, reuse, *recursos, *recdis;
- recursos = (int *) malloc ( sizeof(int) * 4 );
- recdis = (int *) malloc ( sizeof(int) * 4 );
- recursos[0] = recdis[0] = R1;
- recursos[1] = recdis[1] = R2;
- recursos[2] = recdis[2] = R3;
- recursos[3] = recdis[3] = R4;
- unsigned long long AX, BX, CX, DX;
- /*
- * Inicializaciones
- */
- AX = BX = CX = DX = 0x0;
- if( inicial(&listos) != 0 || inicial(&finalizados) != 0 || inicial(&ejecucion) != 0 || inicial(&nuevos) != 0 || inicial(&bloqueados) != 0 || inicial(&suspendidos) != 0){
- printf("Alguna de las listas no fue inicializada!!!");
- exit(1);
- }
- ram = ( char ** ) malloc ( sizeof( char[100] ) * 32 );
- for( x = 0; x < 32; x++ ){
- ram[x] = (char *) malloc( 100 * sizeof( char ) );
- ram[x] = "Vacio";
- }
- comando[0] = '\0';
- tmm = ( int ** ) malloc ( sizeof( int[2] ) * 16 );
- for( x = 0; x < 16 ; x++ ){
- tmm[x] = (int *) malloc( sizeof( int ) * 2 );
- tmm[x][0] = tmm[x][1] = 0;
- }
- ins_temp = (char *) malloc ( sizeof( char ) * bfkbsize );
- /*
- * Inicio de pantalla Ncurses
- */
- initscr();
- move(1,0);printw(" Status: ");
- move(2,0);printw("General: ");
- move(5,0);printw("++-------------------------Registros del CPU------------------------------------------------------------------++-------------Recursos disponibles-------++");
- move(6,0);printw("|| AX || BX || CX || DX || PC || IR || Nombre || wk || R1 || R2 || R3 || R4 ||");
- move(7,0);printw("|| || ||");
- move(8,0);printw("++------------------------------------------------------------------------------------------------------------++----------------------------------------++");
- move(10,0);printw("++------------------------------------------------Monitor de Procesos-----------------------------------------------------------------------------------------++");
- move(11,0);printw("|| pid || AX || BX || CX || DX || PC || STATUS || Nombre || Grupo || Prioridad || CPU || GCPU ||fp|| R1 || R2 || R3 || R4 ||");
- move(12,0);printw("++------------------------------------------------------------------------------------------------------------------------------------------------------------++");
- move(0,0);printw("%s",prompt);
- refresh();
- while(1){
- if( kbhit() ){
- if( com( comando, getchar(), bfkbsize, prompt, &vel ) != 0 ){
- b = compc( comando );
- /*
- * Levantar proceso
- */
- if( b == 0 ){
- nuevo = levanta_proceso(comando,&proc,status, recursos);
- if( nuevo != NULL )
- ins_list(nuevo, &nuevos);
- /*
- * Matar proceso
- */
- }else if( b == 1 ){
- mpid = atoi( &comando[5] );
- move(1,status); clrtoeol();
- if ( mata( mpid, &listos, &finalizados, &ejecucion, &bloqueados, &suspendidos, 0, tmm, ram, &grps, recdis) == 0 )
- printw("Proceso %d eliminado",mpid);
- else
- printw(" Proceso: %d no existe!",mpid);
- move(0,strlen(prompt)+strlen(comando));
- /*
- * Salir
- */
- }else if( b == 2 ){
- while( listos.inicio != NULL ){
- nuevo = listos.inicio;
- move(1,status);
- clrtoeol();
- if( fclose(listos.inicio->instrucciones) != 0 )
- printw("Error al matar el proceso número %d",listos.inicio->pid);
- else
- printw("Matando proceso %d",listos.inicio->pid);
- move(0,strlen(prompt)+strlen(comando));refresh();
- napms( vel * 2 );
- if( listos.inicio->siguiente != NULL ){
- listos.inicio = listos.inicio->siguiente;
- listos.num--;
- }
- else
- listos.inicio = NULL;
- free(nuevo);
- }
- move(1,status);
- clrtoeol();
- printw("Adios");
- refresh();
- sleep(1);
- endwin();
- exit(0);
- /*
- * Fork
- */
- }else if( b == 3 ){
- nuevo = levanta_hijo(comando,&proc,status,&listos,&ejecucion, recursos, &suspendidos, &bloqueados );
- if( nuevo != NULL )
- ins_list(nuevo, &nuevos);
- else{
- move(1,status);clrtoeol();
- printw("Proceso padre no existe");
- }
- }
- refresh();
- comando[0] = '\0';
- move(0,strlen(prompt));
- clrtoeol();
- refresh();
- }
- }else{
- /*************************************************************************************************************************************************************************************/
- /* Memoria: levantando un proceso */
- /*************************************************************************************************************************************************************************************/
- if( nuevos.inicio != NULL && free_frame( tmm ) != -1 ){
- /*
- * Verificar si hay frames libres
- */
- for( x = 0; x < 3; x++ ){
- ff = free_frame( tmm );/* ff Contiene el indice de TMM que está marcado como libre */
- if( ff == -1 )
- break;
- ins_temp = lee_ins( nuevos.inicio->instrucciones, 2*x );
- if( strcmp ( ins_temp, "ERROR" ) == 0 ){
- if( x == 0 ){
- move(1,status);printw("Error 1: ");
- fclose( nuevos.inicio->instrucciones );
- if ( swap( &nuevos, &finalizados) != 0 )
- printw("No se envio a finalizados Error 2: ");
- printw("Archivo muy pequeño");
- refresh();sleep(10);
- }
- break;
- }
- for( reuse = 0; reuse < frames; reuse++ )
- if( tmm[reuse][0] != 0 )
- tmm[reuse][0]++;
- tmm[ff][0] = 1;
- tmm[ff][1] = nuevos.inicio->pid;
- /* Se llena la primera instrucción del frame libre */
- ram[ff*2] = ins_temp;
- /* Se marca la tmp del proceso */
- (nuevos.inicio->tmp)[2*x][0] = 1;
- (nuevos.inicio->tmp)[2*x][1] = ff;
- ins_temp = lee_ins( nuevos.inicio->instrucciones, 2*x+1 );
- if( strcmp ( ins_temp, "ERROR" ) == 0 ){
- x++;
- break;
- }
- (nuevos.inicio->tmp)[2*x+1][0] = 1;
- (nuevos.inicio->tmp)[2*x+1][1] = ff;
- ram[ff*2+1] = ins_temp;
- }
- if( x != 0){
- /* Hay que checar si no envía de nuevos a listos */
- if ( swap( &nuevos, &listos ) != 0 ){
- move(1,status);clrtoeol();
- printw("Hubo un error al pasar de nuevos a listos");refresh();sleep(2);
- }else{
- if( listos.inicio->fork == 0 )
- grps++;
- else{
- nuevo = busca_padre( listos.inicio->ppid, &listos );
- if( nuevo == NULL )
- nuevo = busca_padre( listos.inicio->ppid, &ejecucion );
- if( nuevo == NULL )
- nuevo = busca_padre( listos.inicio->ppid, &suspendidos );
- if( nuevo == NULL )
- nuevo = busca_padre( listos.inicio->ppid, &bloqueados );
- if( nuevo != NULL && nuevo->hijos->inicio == listos.inicio )
- grps++;
- }
- }
- }
- }
- /*************************************************************************************************************************************************************************************/
- /*************************************************************************************************************************************************************************************/
- /*
- * Planificar y Ejecutar
- */
- if( planifica(&listos, &ejecucion, &finalizados, &AX, &BX, &CX, &DX, base, 1.0 / (float) grps ) == 0 ){
- switch( ejecuta( &AX, &BX, &CX, &DX, &ejecucion, timer, &listos, 1.0 / (float) grps, vel, ram, tmm, &suspendidos, &bloqueados, strlen(comando)+strlen(prompt), recursos, recdis)){
- case 0:
- break;
- case 1:
- if ( mata( ejecucion.inicio->pid, &listos, &finalizados, &ejecucion, &bloqueados, &suspendidos, 0, tmm, ram, &grps, recdis) == 0 )
- move(1,10);printw("Proceso %d (%s) Finalizado inesperadamente",finalizados.fin->pid,finalizados.fin->nombre);
- break;
- default:
- if ( mata( ejecucion.inicio->pid, &listos, &finalizados, &ejecucion, &bloqueados, &suspendidos, 0, tmm, ram, &grps, recdis) == 0 )
- move(1,10);printw("Proceso %d (%s) Finalizo",finalizados.fin->pid,finalizados.fin->nombre);
- break;
- }
- }
- }
- bloqueadoschk( &listos, &bloqueados );
- move(3,0);printw("Grupos: %d",grps);
- move(2,status);
- printw("IDLE");
- move(0, strlen(prompt)+strlen(comando));
- refresh();
- if ( imprime(&ejecucion, &listos, &finalizados, &nuevos, &suspendidos, &bloqueados) != 0 ){
- move(1,status);
- printw("Error al imprimir monitor de procesos !!!");
- sleep(3);
- exit(1);
- }
- ram_print( ram );
- tmm_print( tmm );
- move(0, strlen(prompt));
- printw("%s",comando);
- refresh();
- }
- endwin();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement