POSIX Thread aufrufe werden nicht richtig synchronisiert



  • Hallo,

    ich habe im Internet ein schönes Beispiel gesehen mit dem sich Threadprogrammierung lernen lässt.
    Also wir habe einen Park(oder Museum; ähnlich wie im Jurassic Park). Sechs Besucher. Diese betreten den Park und steigen dann in zwei PKW, werden herum gefahren und betreten den Park erneut. Pro Person dreimal.
    Ich möchte entscheiden, ob die Besucher alleine fahren oder zu zweit ode zu dritt.
    Leider gelingt mir nur alleine.

    Ok genug der Prosa jetzt kommt ein Code...

    //globale Deklarationen.....
    
    #define VISITORS 6 
    #define CARS   2 
    #define CYCLES   3
    
    const char* VisitorNames[VISITORS+1] = {"Mike", "Jeff", "John", "Sandra", "Michelle", "Jack", "Amy"};
    const char* CarNames[CARS+1] = {"Explorer1", "Explorer2"};
    char* VisitorName = NULL;
    char* CarName = NULL;
    
    sem_t sem_car, sem_visitor; 
    pthread_t thread_id_visitor[VISITORS],  thread_id_car[CARS]; 
    
    void *Car (void *p) 
    { 
      int *index /*, paseos = 0*/; 
      extern sem_t sem_car, sem_visitor; 
      index = (int *) p; 
    
      /*The cars are always waiting for passengers*/ 
      while(1) 
        { 
    	  CarName = const_cast<char*>(CarNames[*index]);	  
    
    	  sem_wait (&sem_car); // car -1
    	  sem_wait (&sem_car); // car -1
    
    	  sleep (rand()%3);  
    
    	  sem_post (&sem_visitor);  //visitor +1
    	  sem_post (&sem_visitor);  //visitor +1
    
    	  fprintf (stdout, "Two of them join a ride!!.\n"); 	
    
        }  
    }
    
    void *Visitor (void *p) 
    { 
      extern sem_t sem_car, sem_visitor;
      int i, *index; 
      index = (int *)p;
    
      for (i = 0; i < CYCLES; i++) 
        { 
    	  //VisitorName = const_cast<char*>(VisitorNames[*index+1]);
          /* Visitor watching / waiting at the museum. */      
          fprintf (stdout, "Visitor %s enter the museum\n", /*VisitorName*/VisitorNames[*index+1]);            
          sleep (rand()%4); 
          /* Waiting for a ride by car. */        
          sem_post (&sem_car); //car+1
          sem_post (&sem_car); //car+1   
          sem_wait (&sem_visitor);//visitor -1
          sem_wait (&sem_visitor);//visitor -1
          fprintf (stdout, "Visitor %s climbs in the car.\n", /*VisitorName*/VisitorNames[*index+1]); 
          sleep (rand()%3);	 
        } 
        //VisitorName = const_cast<char*>(VisitorNames[*index+1]);
        fprintf (stdout, "Visitor %s has terminated his %d cycles.\n", /*VisitorName*/VisitorNames[*index+1], CYCLES); 
    
        return 0;
    } 
    
    void main() {
      void *Car (void *), *Visitor (void *); 
    				  int i, v[VISITORS], c[CARS]; 
    				  extern sem_t sem_car, sem_visitor; 
    
    				  sem_init (&sem_car,  //semaphore 
    						  		   0,  //no interprocess sharing
    						  		   0); //Intialer Wert
    
    				  sem_init (&sem_visitor,  //semaphore 
    						  			   0,  //no interprocess sharing
    						  			   0); //Intialer Wert
    
    				  srand (time (NULL));    
    
    				  for (i=0; i<VISITORS; i++) 
    				  { 
    				    v[i] = i; 
    				    if (pthread_create (&thread_id_visitor[i], NULL, Visitor, &v[i])) 
    				      exit (1); 
    				  } 				  				  	
    
    				  for (i = 0; i < CARS; i++) 
    				  { 
    				      c[i] = i; 
    				      if (pthread_create (&thread_id_car[i], NULL, Car, &c[i])) 
    				   exit (1); 
    				  } 
    
    				  //wait for thread termination 
    				  for (i=0; i<VISITORS; i++)
    				    pthread_join (thread_id_visitor[i], NULL); 
    
    				  printf("\n\t**- All visitors have toured the safari -**\n"); 
    }
    

    1)Was muß ich tun damit drei Passagiere zusammen fahren können.
    2)Visitorname liefert Mist wenn er gobal deklariert wird. Muß ich das mit Mutex arbeiten?

    Gruß
    Frank


Log in to reply