Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1.  
  2. /*
  3.  *  Miguel Albergaria
  4.  *  1170551
  5.  * 
  6. */
  7.  
  8. #include <pthread.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13.  
  14. #define NUM_THREADS 100
  15.  
  16. pthread_mutex_t mutexVotacao = PTHREAD_MUTEX_INITIALIZER;
  17. pthread_mutex_t mutexVotar = PTHREAD_MUTEX_INITIALIZER;
  18. pthread_mutex_t mutexContar = PTHREAD_MUTEX_INITIALIZER;
  19. pthread_cond_t condVotacaoAbre = PTHREAD_COND_INITIALIZER;
  20. pthread_cond_t condVotacaoFecha = PTHREAD_COND_INITIALIZER;
  21.  
  22. int flag;
  23. int fechou;
  24.  
  25. char texto[100] = "Texto da votacao";
  26. char votos[NUM_THREADS];
  27.  
  28. int numVotos;
  29. int sim;
  30.  
  31. int value;
  32.  
  33. void *socioVota(void *arg) {
  34.    
  35.     pthread_mutex_lock(&mutexVotacao);
  36.         if (fechou == 1) {
  37.             printf("Socio não votou (Votacao está fechada)!\n");
  38.            
  39.             pthread_mutex_unlock(&mutexVotacao);
  40.            
  41.             value = 0;
  42.             pthread_exit((void *) &value);
  43.         }
  44.     pthread_mutex_unlock(&mutexVotacao);
  45.    
  46.     time_t t;
  47.     srand ((unsigned) time (&t));
  48.    
  49.     int *ptr = (int *) arg;
  50.     int index = *ptr;
  51.    
  52.     int votoInt = rand() % 2;
  53.    
  54.     char voto;
  55.     pthread_mutex_lock(&mutexContar);
  56.    
  57.     if (votoInt) {
  58.         voto = 'S';
  59.         sim++;
  60.     } else {
  61.         voto = 'N';
  62.     }
  63.    
  64.     numVotos++;
  65.     pthread_mutex_unlock(&mutexContar);
  66.    
  67.    
  68.     printf("%s\n", texto);
  69.     printf("Voto (%d): %c\n", index, voto);
  70.    
  71.     pthread_mutex_lock(&mutexVotar);
  72.    
  73.     votos[index] = voto;
  74.    
  75.     pthread_mutex_unlock(&mutexVotar);
  76.    
  77.     value = 1;
  78.     pthread_exit((void *) &value);
  79.    
  80. }
  81.  
  82.  
  83. void *simularVotos(void *arg) {
  84.    
  85.     pthread_mutex_lock(&mutexVotacao);
  86.    
  87.     while (flag == 0) {
  88.         pthread_cond_wait(&condVotacaoAbre, &mutexVotacao);  
  89.     }
  90.    
  91.     flag = 0;
  92.     pthread_mutex_unlock(&mutexVotacao);
  93.    
  94.     printf("Votacoes abertas...\n\n");  
  95.    
  96.     pthread_t threads[NUM_THREADS];
  97.    
  98.     int pos[NUM_THREADS];
  99.    
  100.     int i;
  101.     for (i = 0; i < NUM_THREADS; i++) {
  102.        
  103.         pos[i] = i;
  104.        
  105.         if (pthread_create(&threads[i], NULL, socioVota, &pos[i]) != 0) {
  106.             printf("Erro no pthread_create()!\n");
  107.             exit(1);
  108.         }
  109.        
  110.         /*void *ret = NULL;
  111.        
  112.         if(pthread_join(threads[i], &ret) != 0){
  113.             perror("Erro a terminar thread...");
  114.             exit(1);
  115.         }
  116.        
  117.         int v = *((int *) arg);
  118.        
  119.         printf("V: %d\n", v);*/
  120.        
  121.         sleep(2);
  122.        
  123.     }
  124.    
  125.     pthread_exit(NULL);
  126.    
  127. }
  128.  
  129.  
  130. int main() {
  131.    
  132.     flag = 0;
  133.     fechou = 0;
  134.    
  135.     numVotos = 0;
  136.     sim = 0;
  137.    
  138.     value = 0;
  139.    
  140.     pthread_t thread;
  141.    
  142.     if (pthread_create(&thread, NULL, simularVotos, NULL) != 0) {
  143.         printf("Erro no pthread_create()!\n");
  144.         exit(1);
  145.     }
  146.    
  147.     pthread_mutex_lock(&mutexVotacao);
  148.     flag = 1;
  149.     pthread_cond_broadcast(&condVotacaoAbre);  
  150.     pthread_mutex_unlock(&mutexVotacao);
  151.    
  152.     sleep(120);
  153.    
  154.     pthread_mutex_lock(&mutexVotacao);
  155.     fechou = 1;
  156.     pthread_mutex_unlock(&mutexVotacao);
  157.    
  158.     if(pthread_join(thread, NULL) != 0){
  159.         perror("Erro a terminar thread...");
  160.         exit(1);
  161.     }
  162.    
  163.     printf("\nVotos: %d de %d\n", numVotos, NUM_THREADS);
  164.     printf("Votos a favor: %d\n", sim);
  165.     printf("Votos contra: %d\n", numVotos - sim);
  166.    
  167.     return 0;
  168.    
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement