Problem with POSIX semaphores

Discussion in 'C' started by kaseo88, Jun 26, 2007.

  1. kaseo88

    kaseo88 New Member

    Joined:
    Jun 26, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have to do the following exercise (sorry for my English):

    Jurassic Park is a museum with dino's and a park with a safari. There are M visitors and N cars for doing the safari, each of them can carry 2 visitors.

    The visitors walk some time in the museum and later they wait for the cars. When a car is available, it carries his 2 visitors and run for an random time. If all the cars are being used, the visitors have to wait until one car is available. If the car is available but there is no visitor to carry, it will have to wait for visitors. If a car is available but there is only a visitor inside, the car will have to wait for another visitor. (so the car can just carry 2 visitors in each travel)

    I have made the following program, but from 5 times i execute, 1 or 2 times it suffers interlocking or something that makes it not to finish.
    Please, if you can help me....


    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <pthread.h> 
    #include <semaphore.h> 
    #include <unistd.h> 
    
    #define VISITANTES 6 
    #define COCHES   2 
    #define CICLOS   3 
    
    sem_t sem_visitante, sem_coche; 
    
    main (){ 
      pthread_t visi[VISITANTES], coche[COCHES]; 
      void *Coche (void *), *Visitante (void *); 
      int i, v[VISITANTES]; 
      extern sem_t sem_coche, sem_visitante; 
    
      sem_init (&sem_coche, 0, 0); 
      sem_init (&sem_visitante, 0, 0); 
      
      srand (time (NULL));    
      
      for (i=0; i<VISITANTES; i++) 
      { 
        v[i] = i; 
        if (pthread_create (&visi[i], NULL, Visitante, &v[i])) 
          exit (1); 
      } 
    
      for (i = 0; i < COCHES; i++) 
      { 
          v[i] = i; 
          if (pthread_create (&coche[i], NULL, Coche, &v[i])) 
       exit (1); 
      } 
      
      for (i=0; i<VISITANTES; i++) 
        pthread_join (visi[i], NULL); 
        
      printf("\n\t**- Todos los visitantes han recorrido el safari -**\n"); 
    } 
    
    
    void *Visitante (void *p) 
    { 
      extern sem_t sem_coche, sem_visitante; 
      int i, *index; 
      
      index = (int *)p; 
    
      for (i = 0; i < CICLOS; i++) 
        { 
          /* Visitante viendo/esperando en el museo. */ 
          fprintf (stdout, "Visitante %d en el museo\n", *index+1); 
          sleep (rand()%4); 
    
          /* Espera para dar un paseo en coche. */ 
          sem_post (&sem_coche); 
          sem_wait (&sem_visitante); 
          
          /* Paseo. */ 
          fprintf (stdout, "Visitante %d subido en el coche\n", *index+1); 
          sleep (rand()%3); 
    
          /* Libera el coche. */ 
        } 
        fprintf (stdout, "Visitante %d ha terminado sus %d ciclos.\n", *index+1, CICLOS); 
    } 
    
    
    void *Coche (void *p) 
    { 
      int *index, paseos = 0; 
      extern sem_t sem_coche, sem_visitante; 
      index = (int *) p; 
      
      /*Los coches siempre estan esperando viajeros*/ 
      while(1) 
        { 
          /* Espera a ser necesitado. */ 
          sem_wait (&sem_coche);      
    
          /* Espera a que haya dos viajeros. */ 
          sem_wait (&sem_coche); 
          
          /*Realiza el safari*/ 
          sleep (rand()%3); 
          
          /*Vuelve al museo y se bajan los 2 visitantes */ 
          sem_post (&sem_visitante); 
          sem_post(&sem_visitante); 
        }  
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice