Thread: Can you help with my program?

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    2

    Can you help with my program?

    Hello friends, I have the following program ..

    Code:
    #include <stdio.h> 
    #include <math.h> 
    #include <stdlib.h> 
     
    #define SERVIDORES 5 
    #define NUMERO_COLAS 1 
    #define MAX_EN_COLA 15 
     
    typedef struct{ 
        int elem[MAX_EN_COLA]; 
        int cuenta; 
        int frente; 
        int ultimo; 
    }cola_t; 
     
    void ColaInicializar(cola_t *colap){ 
        colap->cuenta=colap->frente=colap->ultimo=0; 
    } 
     
    int ColaVacia(cola_t *colap){ 
        return colap->cuenta==0; 
    } 
     
    int ColaCuenta(cola_t *colap){ 
        return colap->cuenta; 
    } 
     
    int ColaMeter(cola_t *colap,int dato){ 
        if(colap->cuenta==MAX_EN_COLA) 
            return 0; 
        colap->cuenta++; 
        colap->elem[colap->ultimo]=dato; 
        colap->ultimo=(colap->ultimo+1)%MAX_EN_COLA; 
        return 1; 
    } 
     
    int ColaSacar(cola_t *colap,int *dato){ 
        if(colap->cuenta==0) 
            return 0; 
        colap->cuenta--; 
        *dato=colap->elem[colap->frente]; 
        colap->frente=(colap->frente+1)%MAX_EN_COLA; 
        return 1; 
    } 
     
    // Estructuras para colas y servidores 
     
    cola_t colas[NUMERO_COLAS]; 
     
    struct { 
        int minimo; 
        int maximo; 
        int acumulado; 
        int cantidad; 
        int max_longitud; 
    } estadisticas_colas[NUMERO_COLAS]; 
     
    struct { 
        int media_atencion; 
        int ocupado; 
        int ocupado_hasta; 
        int cola_que_atiende; 
        int numeros_atendidos; 
        int tiempo_acumulado; 
    } servidores[SERVIDORES]; 
     
    int t_simulacion = 32400, t_cierre = 28800; // Tiempos simulacion 
     
    // Inicia colas 
     
    void inicia_colas(void){ 
        int i; 
     
        for(i=0;i<NUMERO_COLAS;i++){ 
            ColaInicializar(&colas[i]); 
            estadisticas_colas[i].minimo = 28800; 
            estadisticas_colas[i].maximo = 0; 
            estadisticas_colas[i].acumulado = 0; 
            estadisticas_colas[i].cantidad = 0; 
            estadisticas_colas[i].max_longitud = 0; 
        } 
    } 
     
    // Inicia los servidores 
     
    void inicia_servidores(void){ 
        int i; 
     
        for(i=0;i<SERVIDORES;i++){ 
            servidores[i].cola_que_atiende = i; 
            servidores[i].ocupado = 1; 
            servidores[i].ocupado_hasta =  150-i*10;    // tiempo inicio de atencion 
            servidores[i].media_atencion = 300+i*5; 
            servidores[i].numeros_atendidos = 0; 
            servidores[i].tiempo_acumulado = 0; 
        } 
    } 
     
    int cola_mas_corta(){ 
        int min, i, j; 
     
        j=0; 
        min=ColaCuenta(&colas[0]); 
        for(i=1; i<NUMERO_COLAS; i++) 
            if(min > ColaCuenta(&colas[i])){ 
                min = ColaCuenta(&colas[i]); 
                j = i; 
            } 
        return j; 
    } 
     
    void actualiza_estadisticas_colas(int cola,int intervalo){ 
        if(estadisticas_colas[cola].minimo > intervalo) 
            estadisticas_colas[cola].minimo = intervalo; 
        if(estadisticas_colas[cola].maximo < intervalo) 
            estadisticas_colas[cola].maximo = intervalo; 
        estadisticas_colas[cola].acumulado += intervalo; 
        estadisticas_colas[cola].cantidad ++; 
        if(estadisticas_colas[cola].max_longitud < ColaCuenta(&colas[cola])) 
            estadisticas_colas[cola].max_longitud = ColaCuenta(&colas[cola]); 
    } 
     
    void resumen_colas(void){ 
        int i, acumulado=0, cantidad=0; 
     
        printf("\nEstadisticas Colas\n\n"); 
        printf("\tCola Clientes Minimo Maximo Promedio Max_Cant\n"); 
        printf("\t=============================================\n"); 
        for(i=0;i<NUMERO_COLAS;i++){ 
            printf("\t%4d %8d %6d %6d %8.2f %8d\n",i,estadisticas_colas[i].cantidad, 
                estadisticas_colas[i].minimo,estadisticas_colas[i].maximo, 
                (float)estadisticas_colas[i].acumulado/estadisticas_colas[i].cantidad, 
                estadisticas_colas[i].max_longitud); 
                acumulado += estadisticas_colas[i].acumulado; 
                cantidad += estadisticas_colas[i].cantidad; 
        } 
        printf("\n\t Cantidad Atendida: %d Promedio: %.2f\n", 
                cantidad,(float)acumulado/cantidad); 
        printf("\n\t Intervalo de llegada medio: %.2f\n",(float)t_cierre/cantidad); 
    } 
     
    void resumen_servidores(void){ 
        int i, acumulado=0, cantidad=0; 
     
        printf("\nEstadisticas Servidores\n\n"); 
        printf("\tServidor Clientes Promedio T_Ocupado T_Desocupado\n"); 
        printf("\t=================================================\n"); 
        for(i=0;i<SERVIDORES;i++){ 
            printf("\t%8d %8d %8.2f %9d %12d\n",i,servidores[i].numeros_atendidos, 
                (float)servidores[i].tiempo_acumulado/servidores[i].numeros_atendidos, 
                servidores[i].tiempo_acumulado, t_simulacion-servidores[i].tiempo_acumulado); 
                acumulado += servidores[i].tiempo_acumulado; 
                cantidad += servidores[i].numeros_atendidos; 
        } 
        printf("\n\t Cantidad Atendida: %d Promedio: %.2f\n", 
                cantidad,(float)acumulado/cantidad); 
    } 
     
    // Calcula el intervarlo entre llegadas segun una distribucion exponencial 
     
    int intervalo(float lambda){ 
        float u,x; 
     
        u = (float)rand() / RAND_MAX; 
        x = -1.0*(float)log((double)(1.0-u)) * lambda; 
        return (int)x; 
    } 
     
    int main(void) { 
        int t_actual = 0, t_proximo; 
        int t, c, i; 
        int lambda_cliente = 60; 
     
        inicia_servidores(); 
        inicia_colas(); 
        t_proximo = intervalo(lambda_cliente); 
        while(t_actual <= t_simulacion){ 
            while(t_actual <= t_cierre && t_actual == t_proximo){ 
                c = cola_mas_corta(); 
                ColaMeter(&colas[c],t_actual); 
                t_proximo = t_actual + intervalo(lambda_cliente); 
            } 
            for(i=0;i<SERVIDORES;i++){ 
                if(servidores[i].ocupado && servidores[i].ocupado_hasta == t_actual) 
                    servidores[i].ocupado = 0; 
                if(!servidores[i].ocupado){ 
                    c = servidores[i].cola_que_atiende; 
                    for(i=0;i<SERVIDORES;i++){ 
                        actualiza_estadisticas_colas(c,t_actual-t); 
                        t = intervalo(servidores[i].media_atencion); 
                        servidores[i].ocupado = 1; 
                        servidores[i].numeros_atendidos++; 
                        servidores[i].tiempo_acumulado +=t ; 
                        servidores[i].ocupado_hasta = t_actual + t + 5; 
                    } 
                } 
            } 
            t_actual++; 
        } 
        resumen_colas(); 
        resumen_servidores(); 
        return EXIT_SUCCESS; 
    }
    Is a discrete event-based simulation.

    And then, just need to work with 5 servers, but with a single queue ... I did it that way as you can see in the code from the link above, but not how to make me work well with 5 servers with the same configuration, but using only a single queue. Can someone help me to modify my code? .

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by tadprox View Post
    Is a discrete event-based simulation.

    And then, just need to work with 5 servers, but with a single queue ... I did it that way as you can see in the code from the link above, but not how to make me work well with 5 servers with the same configuration, but using only a single queue. Can someone help me to modify my code? .
    I don't understand what your program does and the fact that all your names and comments are in Spanish doesn't make it easier.

    Nevertheless I just run it and it segfaults. Here's the debugging session:
    Code:
    $ gdb -q ./a.out
    Reading symbols from a.out...done.
    (gdb) r
    Starting program: a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x080487d9 in actualiza_estadisticas_colas (cola=2379297, intervalo=317) at foo.c:113
    113         if(estadisticas_colas[cola].minimo > intervalo) 
    (gdb) bt
    #0  0x080487d9 in actualiza_estadisticas_colas (cola=2379297, intervalo=317) at foo.c:113
    #1  0x08048d4a in main () at foo.c:189
    (gdb) frame 1
    #1  0x08048d4a in main () at foo.c:189
    189                         actualiza_estadisticas_colas(c,t_actual-t);
    "estadisticas_colas" is an one-element-array, but in your function, you try to call it with an index ("cola") which is way out of bounds.

    Two other problems I've noticed:
    1) You use "t" uninitialized the first time in your function call on line 189
    2) The inner for loop on line 188 changes the counter "i" from the outer loop on line 183.

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    oh gdb is use for debug the program.... i just know, and when i compile without -lm can work the program but if i dont use -lm the log cannot be compile, and if i use the option -lm . the output is segmentation fault... hahahaha i was sory for not helping your problem

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    2
    Quote Originally Posted by AndiPersti View Post
    I don't understand what your program does and the fact that all your names and comments are in Spanish doesn't make it easier.

    Nevertheless I just run it and it segfaults. Here's the debugging session:
    Code:
    $ gdb -q ./a.out
    Reading symbols from a.out...done.
    (gdb) r
    Starting program: a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x080487d9 in actualiza_estadisticas_colas (cola=2379297, intervalo=317) at foo.c:113
    113         if(estadisticas_colas[cola].minimo > intervalo) 
    (gdb) bt
    #0  0x080487d9 in actualiza_estadisticas_colas (cola=2379297, intervalo=317) at foo.c:113
    #1  0x08048d4a in main () at foo.c:189
    (gdb) frame 1
    #1  0x08048d4a in main () at foo.c:189
    189                         actualiza_estadisticas_colas(c,t_actual-t);
    "estadisticas_colas" is an one-element-array, but in your function, you try to call it with an index ("cola") which is way out of bounds.

    Two other problems I've noticed:
    1) You use "t" uninitialized the first time in your function call on line 189
    2) The inner for loop on line 188 changes the counter "i" from the outer loop on line 183.

    Bye, Andreas
    Sorry it is because i talk spanish... But.. "colas" is an queue... that is.. everything with the word "colas" means queue (for programming)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM