Advertisement
GustavoBernardi

Untitled

May 31st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include "pthread.h"
  6. #include <windows.h>
  7. #include <stdbool.h>
  8.  
  9. pthread_mutex_t mutreta;
  10.  
  11. long int num = 5;
  12. long int total = 2;
  13. long int ultimo = 3;
  14. clock_t tempo;
  15. int tempofinal = 1;
  16. int opcao = 1;
  17.  
  18. void entrada(void);
  19.  
  20. bool Primo(long int n){
  21.     if (n%3 == 0){
  22.         return false;
  23.     }else if (n%2 == 0){
  24.         return false;
  25.     }
  26.  
  27.     int n2 = 5;
  28.  
  29.     while (n2*n2 <= n){
  30.         if(n%n2 == 0 || n%(n2+2) ==0)
  31.             return false;
  32.         n2= n2+6;
  33.     }
  34.     return true;
  35. }
  36.  
  37. void* Encontrar(void *encontro){
  38.     while (clock() - tempo <= tempofinal){
  39.         if(Primo(num) == true && num != ultimo){
  40.             total++;
  41.             ultimo = num;
  42.         }
  43.         num+=2;
  44.     }
  45.     pthread_exit(NULL);
  46. }
  47.  
  48. void ciclos(){
  49.     num = 5;
  50.     total = 2;
  51.     ultimo = 3;
  52.     tempo = clock();
  53.     pthread_t pthd8, pthd7, pthd6, pthd5, pthd4, pthd3, pthd2, pthd1;
  54.     switch (opcao){
  55.     case 1:
  56.         tempo = clock();
  57.         pthread_mutex_init( &mutreta, NULL );
  58.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  59.         pthread_join( pthd1, NULL );
  60.         break;
  61.     case 2:
  62.         tempo = clock();
  63.         pthread_mutex_init( &mutreta, NULL );
  64.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  65.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  66.         pthread_join( pthd2, NULL );
  67.         pthread_join( pthd1, NULL );
  68.         break;
  69.     case 3:
  70.         tempo = clock();
  71.         pthread_mutex_init( &mutreta, NULL );
  72.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  73.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  74.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  75.         pthread_join( pthd3, NULL );
  76.         pthread_join( pthd2, NULL );
  77.         pthread_join( pthd1, NULL );
  78.         break;
  79.     case 4:
  80.         tempo = clock();
  81.         pthread_mutex_init( &mutreta, NULL );
  82.         pthread_create(&pthd4,NULL,Encontrar,NULL);
  83.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  84.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  85.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  86.         pthread_join( pthd4, NULL );
  87.         pthread_join( pthd3, NULL );
  88.         pthread_join( pthd2, NULL );
  89.         pthread_join( pthd1, NULL );
  90.         break;
  91.     case 5:
  92.         tempo = clock();
  93.         pthread_mutex_init( &mutreta, NULL );
  94.         pthread_create(&pthd5,NULL,Encontrar,NULL);
  95.         pthread_create(&pthd4,NULL,Encontrar,NULL);
  96.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  97.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  98.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  99.         pthread_join( pthd5, NULL );
  100.         pthread_join( pthd4, NULL );
  101.         pthread_join( pthd3, NULL );
  102.         pthread_join( pthd2, NULL );
  103.         pthread_join( pthd1, NULL );
  104.         break;
  105.     case 6:
  106.         tempo = clock();
  107.         pthread_mutex_init( &mutreta, NULL );
  108.         pthread_create(&pthd6,NULL,Encontrar,NULL);
  109.         pthread_create(&pthd5,NULL,Encontrar,NULL);
  110.         pthread_create(&pthd4,NULL,Encontrar,NULL);
  111.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  112.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  113.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  114.         pthread_join( pthd6, NULL );
  115.         pthread_join( pthd5, NULL );
  116.         pthread_join( pthd4, NULL );
  117.         pthread_join( pthd3, NULL );
  118.         pthread_join( pthd2, NULL );
  119.         pthread_join( pthd1, NULL );
  120.         break;
  121.     case 7:
  122.         tempo = clock();
  123.         pthread_mutex_init( &mutreta, NULL );
  124.         pthread_create(&pthd7,NULL,Encontrar,NULL);
  125.         pthread_create(&pthd6,NULL,Encontrar,NULL);
  126.         pthread_create(&pthd5,NULL,Encontrar,NULL);
  127.         pthread_create(&pthd4,NULL,Encontrar,NULL);
  128.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  129.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  130.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  131.         pthread_join( pthd7, NULL );
  132.         pthread_join( pthd6, NULL );
  133.         pthread_join( pthd5, NULL );
  134.         pthread_join( pthd4, NULL );
  135.         pthread_join( pthd3, NULL );
  136.         pthread_join( pthd2, NULL );
  137.         pthread_join( pthd1, NULL );
  138.         break;
  139.      case 8:
  140.         tempo = clock();
  141.         pthread_mutex_init( &mutreta, NULL );
  142.         pthread_create(&pthd8,NULL,Encontrar,NULL);
  143.         pthread_create(&pthd7,NULL,Encontrar,NULL);
  144.         pthread_create(&pthd6,NULL,Encontrar,NULL);
  145.         pthread_create(&pthd5,NULL,Encontrar,NULL);
  146.         pthread_create(&pthd4,NULL,Encontrar,NULL);
  147.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  148.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  149.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  150.         pthread_join( pthd8, NULL );
  151.         pthread_join( pthd7, NULL );
  152.         pthread_join( pthd6, NULL );
  153.         pthread_join( pthd5, NULL );
  154.         pthread_join( pthd4, NULL );
  155.         pthread_join( pthd3, NULL );
  156.         pthread_join( pthd2, NULL );
  157.         pthread_join( pthd1, NULL );
  158.         break;
  159.     }
  160.     pthread_mutex_destroy( &mutreta );
  161.     printf("\n Numero de threads %d", opcao);
  162.     printf("\n Total de %ld com thread!", total);
  163.     printf("\n ultimo: %ld \n", ultimo);
  164. }
  165.  
  166. int main()
  167. {
  168.     printf("\n Programa Para encontrar primos \n");
  169.         printf("\n Tempo 1 ou 2 minutos?");
  170.         scanf("%d", &tempofinal);
  171.     if (tempofinal == 1){
  172.             tempofinal = 60000;
  173.     }else{tempofinal = 120000;}
  174.     printf("\n quantos nucleos?");
  175.     scanf("%d", &opcao);
  176.     ciclos();
  177.     system("Pause");
  178.     return 0;
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement