Advertisement
GustavoBernardi

Untitled

Jun 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 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.         if(Primo(num) == true && num > ultimo){
  43.                 total++;
  44.             pthread_mutex_lock(&mutreta);
  45.  
  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.     pthread_mutex_init( &mutreta, NULL );
  72.     pthread_t pthd[opcao];
  73.     for (int contador =0; contador < opcao; contador++){
  74.              pthread_create(&pthd[contador],NULL,Encontrar,NULL);
  75.  
  76.     }
  77.     tempo = clock();
  78.     for (int contador = 0; contador < opcao; contador++ ){
  79.             pthread_join( pthd[contador], NULL );
  80.     }
  81.     pthread_mutex_destroy( &mutreta );
  82.     imprime();
  83.  
  84. }
  85.  
  86.  
  87. int main()
  88. {
  89.     printf("\n Programa Para encontrar primos \n");
  90.         printf("\n Tempo 1 ou 2 minutos?");
  91.         scanf("%d", &tempofinal);
  92.     if (tempofinal == 1){
  93.             tempofinal = 60000;
  94.     }else if (tempofinal == 2){tempofinal = 120000;}
  95.     printf("\n quantos nucleos?");
  96.     scanf("%d", &opcao);
  97.     ciclos();
  98.     system("Pause");
  99.     return 0;
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement