Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8.  
  9. #include "debug.h"
  10. #include "memory.h"
  11.  
  12. #define PING 0
  13. #define PONG 1
  14. #define MAX_ITERATIONS 10
  15.  
  16. void *ping(void *arg);
  17. void *pong(void *arg);
  18.  
  19. typedef struct
  20. {
  21.     int next;
  22.     pthread_mutex_t mutex;
  23.     pthread_cond_t cond;
  24. }thread_params_t;
  25.  
  26.  
  27. int main(int argc, char *argv[]){
  28.     /* Disable warnings */
  29.     (void)argc; (void)argv;
  30.  
  31.     pthread_t t1, t2;
  32.     thread_params_t thread_params;
  33.  
  34.     //Inicializa o mutex
  35.     if ((errno = pthread_mutex_init(&thread_params.mutex, NULL)) != 0)
  36.         ERROR(12, "pthread_mutex_init() failed");
  37.  
  38.     // Var.Condição: inicializa variável de condição
  39.     if ((errno = pthread_cond_init(&thread_params.cond, NULL)) != 0)
  40.         ERROR(14, "pthread_cond_init() failed!");
  41.  
  42.     //Criação das threads + passagem de parâmetro
  43.     if ((errno = pthread_create(&t1, NULL, ping, &thread_params) != 0))
  44.         ERROR(10, "Erro no pthread_create()!");
  45.     if ((errno = pthread_create(&t2, NULL, pong, &thread_params) != 0))
  46.         ERROR(10, "Erro no pthread_create()!");
  47.        
  48.     //Espera que as threads terminem
  49.     if ((errno = pthread_join(t1, NULL)) != 0)
  50.         ERROR(11, "Erro no pthread_join()!\n");
  51.     if ((errno = pthread_join(t2, NULL)) != 0)
  52.         ERROR(11, "Erro no pthread_join()!\n");
  53.  
  54.     // Var.Condição: destroi a variável de condição
  55.     if ((errno = pthread_cond_destroy(&thread_params.cond)) != 0)
  56.         ERROR(15,"pthread_cond_destroy failed!");
  57.  
  58.     //Liberta o mutex
  59.     if ((errno = pthread_mutex_destroy(&thread_params.mutex)) != 0)
  60.         ERROR(13, "pthread_mutex_destroy() failed");
  61.  
  62.     return 0;
  63. }
  64.  
  65. void *ping(void *arg)
  66. {
  67.     // cast para o tipo de dados enviado pela 'main thread'
  68.     thread_params_t *params = (thread_params_t *) arg;
  69.  
  70.     for (int i = 0; i < MAX_ITERATIONS; ++i)
  71.     {
  72.         // Mutex: entra na secção crítica
  73.         if ((errno = pthread_mutex_lock(&params->mutex)) != 0){    
  74.             WARNING("pthread_mutex_lock() failed");
  75.             return NULL;
  76.         }
  77.        
  78.         // Var.Condição: bloqueia a thread usando a variável de condição (em conjunto com o mutex)
  79.         while (params->next != PING){      
  80.             if ((errno = pthread_cond_wait(&params->cond, &params->mutex)!=0)){  
  81.                 WARNING("pthread_cond_wait() failed");
  82.                 return NULL;
  83.             }
  84.         }
  85.    
  86.         printf("PING...\n");
  87.         params->next = PONG;
  88.  
  89.         // Var.Condição: notifica a primeira thread à espera na variável de condição
  90.         if ((errno = pthread_cond_signal(&params->cond)) != 0){
  91.             WARNING("pthread_cond_signal() failed");
  92.             return NULL;
  93.         }
  94.            
  95.         // Mutex: sai da secção crítica
  96.         if ((errno = pthread_mutex_unlock(&params->mutex)) != 0){
  97.             WARNING("pthread_mutex_unlock() failed");
  98.             return NULL;
  99.         }
  100.     }
  101.  
  102.    
  103.     return NULL;
  104. }
  105.  
  106. void *pong(void *arg)
  107. {
  108.     // cast para o tipo de dados enviado pela 'main thread'
  109.     thread_params_t *params = (thread_params_t *) arg;
  110.    
  111.     for (int i = 0; i < MAX_ITERATIONS; ++i)
  112.     {
  113.         // Mutex: entra na secção crítica
  114.         if ((errno = pthread_mutex_lock(&params->mutex)) != 0){    
  115.             WARNING("pthread_mutex_lock() failed");
  116.             return NULL;
  117.         }
  118.        
  119.         // Var.Condição: bloqueia a thread usando a variável de condição (em conjunto com o mutex)
  120.         while (params->next != PONG){      
  121.             if ((errno = pthread_cond_wait(&params->cond, &params->mutex)!=0)){  
  122.                 WARNING("pthread_cond_wait() failed");
  123.                 return NULL;
  124.             }
  125.         }
  126.    
  127.         printf("\t...PONG!\n");
  128.         params->next = PING;
  129.  
  130.         // Var.Condição: notifica a primeira thread à espera na variável de condição
  131.         if ((errno = pthread_cond_signal(&params->cond)) != 0){
  132.             WARNING("pthread_cond_signal() failed");
  133.             return NULL;
  134.         }
  135.            
  136.         // Mutex: sai da secção crítica
  137.         if ((errno = pthread_mutex_unlock(&params->mutex)) != 0){
  138.             WARNING("pthread_mutex_unlock() failed");
  139.             return NULL;
  140.         }
  141.     }
  142.    
  143.     return NULL;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement