Advertisement
GustavoBernardi

primos

Jun 7th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.88 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.  
  22.     if (n%2 == 0){
  23.         return false;
  24.     }else if (n%3 == 0){
  25.         return false;
  26.     }else if (n%10 == 5){
  27.         return false;
  28.     }
  29.  
  30.     int n2 = 5;
  31.  
  32.     while (n2 <= n/2){
  33.         if(n%n2 == 0 )
  34.             return false;
  35.         n2= n2+2;
  36.     }
  37.     return true;
  38. }
  39.  
  40. void* Encontrar(void *encontro){
  41.     while (clock() - tempo <= tempofinal){
  42.             //while(total != ?){
  43.         if(Primo(num) == true && num > ultimo){
  44.             pthread_mutex_lock(&mutreta);
  45.             total++;
  46.             ultimo = num;
  47.             pthread_mutex_unlock(&mutreta);
  48.         }
  49.         num+=2;
  50.     }
  51.     pthread_exit(NULL);
  52. }
  53. int imprime(){
  54.     if (Primo(ultimo) == true){
  55.     total++;
  56.     printf("\n Numero de threads %d", opcao);
  57.     printf("\n Total de %ld com thread!", total);
  58.     printf("\n ultimo: %ld \n", ultimo);
  59.  
  60.     }
  61.     if(Primo(ultimo)== false){
  62.         ultimo= ultimo+2;
  63.         imprime();
  64.     }
  65. }
  66.  
  67. void ciclos(){
  68.     num = 5;
  69.     total = 2;
  70.     ultimo = 3;
  71.     tempo = clock();
  72.     pthread_t pthd8, pthd7, pthd6, pthd5, pthd4, pthd3, pthd2, pthd1;
  73.     switch (opcao){
  74.     case 1:
  75.         tempo = clock();
  76.         pthread_mutex_init( &mutreta, NULL );
  77.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  78.         pthread_join( pthd1, NULL );
  79.         break;
  80.     case 2:
  81.         tempo = clock();
  82.         pthread_mutex_init( &mutreta, NULL );
  83.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  84.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  85.         pthread_join( pthd2, NULL );
  86.         pthread_join( pthd1, NULL );
  87.         break;
  88.     case 3:
  89.         tempo = clock();
  90.         pthread_mutex_init( &mutreta, NULL );
  91.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  92.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  93.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  94.         pthread_join( pthd3, NULL );
  95.         pthread_join( pthd2, NULL );
  96.         pthread_join( pthd1, NULL );
  97.         break;
  98.     case 4:
  99.         tempo = clock();
  100.         pthread_mutex_init( &mutreta, NULL );
  101.         pthread_create(&pthd4,NULL,Encontrar,NULL);
  102.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  103.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  104.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  105.         pthread_join( pthd4, NULL );
  106.         pthread_join( pthd3, NULL );
  107.         pthread_join( pthd2, NULL );
  108.         pthread_join( pthd1, NULL );
  109.         break;
  110.     case 5:
  111.         tempo = clock();
  112.         pthread_mutex_init( &mutreta, NULL );
  113.         pthread_create(&pthd5,NULL,Encontrar,NULL);
  114.         pthread_create(&pthd4,NULL,Encontrar,NULL);
  115.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  116.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  117.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  118.         pthread_join( pthd5, NULL );
  119.         pthread_join( pthd4, NULL );
  120.         pthread_join( pthd3, NULL );
  121.         pthread_join( pthd2, NULL );
  122.         pthread_join( pthd1, NULL );
  123.         break;
  124.     case 6:
  125.         tempo = clock();
  126.         pthread_mutex_init( &mutreta, NULL );
  127.         pthread_create(&pthd6,NULL,Encontrar,NULL);
  128.         pthread_create(&pthd5,NULL,Encontrar,NULL);
  129.         pthread_create(&pthd4,NULL,Encontrar,NULL);
  130.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  131.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  132.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  133.         pthread_join( pthd6, NULL );
  134.         pthread_join( pthd5, NULL );
  135.         pthread_join( pthd4, NULL );
  136.         pthread_join( pthd3, NULL );
  137.         pthread_join( pthd2, NULL );
  138.         pthread_join( pthd1, NULL );
  139.         break;
  140.     case 7:
  141.         tempo = clock();
  142.         pthread_mutex_init( &mutreta, 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( pthd7, NULL );
  151.         pthread_join( pthd6, NULL );
  152.         pthread_join( pthd5, NULL );
  153.         pthread_join( pthd4, NULL );
  154.         pthread_join( pthd3, NULL );
  155.         pthread_join( pthd2, NULL );
  156.         pthread_join( pthd1, NULL );
  157.         break;
  158.      case 8:
  159.         tempo = clock();
  160.         pthread_mutex_init( &mutreta, NULL );
  161.         pthread_create(&pthd8,NULL,Encontrar,NULL);
  162.         pthread_create(&pthd7,NULL,Encontrar,NULL);
  163.         pthread_create(&pthd6,NULL,Encontrar,NULL);
  164.         pthread_create(&pthd5,NULL,Encontrar,NULL);
  165.         pthread_create(&pthd4,NULL,Encontrar,NULL);
  166.         pthread_create(&pthd3,NULL,Encontrar,NULL);
  167.         pthread_create(&pthd2,NULL,Encontrar,NULL);
  168.         pthread_create(&pthd1,NULL,Encontrar,NULL);
  169.         pthread_join( pthd8, NULL );
  170.         pthread_join( pthd7, NULL );
  171.         pthread_join( pthd6, NULL );
  172.         pthread_join( pthd5, NULL );
  173.         pthread_join( pthd4, NULL );
  174.         pthread_join( pthd3, NULL );
  175.         pthread_join( pthd2, NULL );
  176.         pthread_join( pthd1, NULL );
  177.         break;
  178.     }
  179.     pthread_mutex_destroy( &mutreta );
  180.     imprime();
  181.  
  182. }
  183.  
  184.  
  185. int main()
  186. {
  187.     printf("\n Programa Para encontrar primos \n");
  188.         printf("\n Tempo 1 ou 2 minutos?");
  189.         scanf("%d", &tempofinal);
  190.     if (tempofinal == 1){
  191.             tempofinal = 60000;
  192.     }else if (tempofinal == 2){tempofinal = 120000;}
  193.     printf("\n quantos nucleos?");
  194.     scanf("%d", &opcao);
  195.     ciclos();
  196.     system("Pause");
  197.     return 0;
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement