Advertisement
KDOXG

Multithreading POSIX

Sep 12th, 2019
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <random>
  4. #include <climits>
  5. #include <cstdlib>
  6. #include <cstdbool>
  7. #include <pthread.h>
  8.  
  9. /*  Enunciado: https://pastebin.com/84JzD3vL    */
  10.  
  11. /*  Relatório: https://docs.google.com/document/d/1qXxZWiY8NSQ7G89HWHr3YfpRt4M-mdzbEVR_OgL1LOU/    */
  12.  
  13. /*  AVISO
  14. *   O tipo "short int" para a lista foi escolhido apenas para rodar em tempo mais aceitável.
  15. *   Em tipos maiores como long int ou long long int tornavam o algoritmo de teste de primalidade muito lento.
  16. */
  17.  
  18. struct pth_arg{
  19.     unsigned long V;
  20.     std::list<short> *B;
  21. };
  22. unsigned long v, p, c, t;
  23. pthread_mutex_t m;
  24. void * produz(void *arg);
  25. void * consome(void *arg);
  26. bool prime_number(short p);
  27.  
  28. int main(int argc, char* argv[])
  29. {
  30.     std::list<short> buffer;
  31.     int flag = 1;
  32.     pth_arg *p_arg;
  33.     p_arg = new pth_arg;
  34.     if (argc <= 4)  return -1;
  35.  
  36.     v = strtoul(argv[1], NULL, 0);
  37.     p = strtoul(argv[2], NULL, 0);
  38.     c = strtoul(argv[3], NULL, 0);
  39.     t = strtoul(argv[4], NULL, 0);
  40.     pthread_t produtores[p];
  41.     pthread_t consumidores[c];
  42.     p_arg->V = v;
  43.     p_arg->B = &buffer; //Passagem por referência para ambas as funções poderem modificar esta lista
  44.     for (int i = 0; i < p; i++)
  45.     {
  46.         while (flag)    //verifica se a pthread foi criada, caso falhe, o retorno será diferente de zero, portanto o loop persistirá
  47.             flag = pthread_create(&produtores[i], NULL, produz, (void*)p_arg);
  48.         flag = 1;
  49.     }
  50.  
  51.     for (int i = 0; i < c; i++)
  52.     {
  53.         while (flag)
  54.             flag = pthread_create(&consumidores[i], NULL, consome, (void*)p_arg);
  55.         flag = 1;
  56.     }
  57.  
  58.     for (int i = 0; i < p; i++)
  59.     {
  60.         while(flag)
  61.             flag = pthread_join(produtores[i],NULL);
  62.         flag = 1;
  63.     }
  64.     pthread_mutex_lock(&m);
  65.     for (int i = 0; i < c; i++)
  66.         buffer.push_back(-1);
  67.     pthread_mutex_unlock(&m);
  68.  
  69.     for (int i = 0; i < c; i++)
  70.         while(flag)
  71.             flag = pthread_join(consumidores[i],NULL);
  72.  
  73.     return 0;
  74. }
  75.  
  76. void * produz(void* arg) //unsigned long V, std::list<short> B
  77. {
  78.     pth_arg *p_arg = (pth_arg*)arg;
  79.     unsigned long V = p_arg->V;
  80.     std::list<short> *B = p_arg->B;
  81.     std::random_device rd;
  82.     for (unsigned short i = 0; i < V; i++)
  83.         {
  84.         while (B->size() == t)  //Limite estipulado para o buffer
  85.             sched_yield();
  86.         pthread_mutex_lock(&m);
  87.         B->push_back(rand() % SHRT_MAX);
  88.         pthread_mutex_unlock(&m);
  89.      }
  90.     pthread_exit(NULL);
  91. }
  92.  
  93. void * consome(void* arg) //std::list<short> B
  94. {
  95.     pth_arg *p_arg = (pth_arg*)arg;
  96.     std::list<short> *B = p_arg->B;
  97.     while(1)    //Loop infinito
  98.     {
  99.         while (B->size() == 0)
  100.             sched_yield();
  101.         short a = B->front();
  102.         pthread_mutex_lock(&m);
  103.             B->pop_front();
  104.         pthread_mutex_unlock(&m);
  105.         if (a == -1)
  106.             break;
  107.         if (prime_number(abs(a)))
  108.             std::cout << '[' + std::to_string(pthread_self()) + ':' + std::to_string(a) + "]\n";    //[id:numero_primo]
  109.     }
  110.     pthread_exit(NULL);
  111. }
  112.  
  113. bool prime_number(short p)
  114. {
  115.     if (p <= 1) return false;
  116.     if (p == 2) return true;
  117.     p = (short)sqrt(p);
  118.     for (short i = 3; i <= p; i+=2) if (p % i == 0) return false;
  119.     return true;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement