Advertisement
Guest User

Untitled

a guest
Jul 1st, 2018
237
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <sys/time.h>
  5. #include <time.h>
  6.  
  7. #define MAX 2147483647
  8. int vetorMin;
  9.  
  10. long long numElementos =    159999999;
  11.  
  12. int numThreads, interPorThread, max;
  13. unsigned char *vetor, *maiores;
  14. pthread_mutex_t mutex;
  15.  
  16. double MyClock() {
  17.     struct timeval tv;
  18.     gettimeofday(&tv, NULL);
  19.     return (tv.tv_sec * 1000000) + tv.tv_usec;
  20. }
  21.  
  22. void *min(void *arg){
  23.     int t = (long)arg, i, ini, fim;    
  24.     ini = t*interPorThread;
  25.     unsigned char meuMin;
  26.    
  27.     if((t == numThreads-1) && (numElementos%numThreads != 0))
  28.         fim = numElementos;
  29.     else
  30.         fim = t*interPorThread + interPorThread;
  31.  
  32.     meuMin = vetor[ini];
  33.     for(i = ini+1; i < fim; i++){
  34.         if(meuMin <= vetor[i])
  35.             meuMin = vetor[i];
  36.     }
  37.     maiores[t] = meuMin;
  38. }
  39.                                                                              
  40. int main(int argc, char *argv[]){
  41.     srand(0);
  42.     //srand( (unsigned)time(NULL) );
  43.  
  44.     double inicio;
  45.     int i;
  46.     if (argc != 2) {
  47.       fprintf(stderr, "usage: ./a.out <threads>\n");
  48.       exit(1);
  49.     }
  50.  
  51.     vetor = (unsigned char*)malloc(numElementos* sizeof(unsigned char));
  52.     maiores = (unsigned char*)malloc(numThreads* sizeof(unsigned char));
  53.  
  54.     if(vetor != NULL){
  55.         for(i = 0; i < numElementos-1; i++){
  56.             vetor[i] = 1;
  57.         }
  58.         vetor[i] = 123;
  59.  
  60.         //----BUSCA PARALELA----
  61.         numThreads = atoi(argv[1]);        
  62.         interPorThread = numElementos/numThreads;
  63.        
  64.         pthread_mutex_init(&mutex, NULL);
  65.         pthread_t *threads = (pthread_t *)malloc(numThreads*sizeof(pthread_t));
  66.        
  67.         int rc;
  68.  
  69.         for(i = 0; i < numThreads; i++){
  70.             rc = pthread_create(&threads[i], NULL, min, (void *)i);
  71.             if(rc){
  72.                 printf("ERROR: return code from pthread_create() is %d\n", rc);
  73.                 exit(1);
  74.             }
  75.         }
  76.        
  77.         for (i = 0; i < numThreads; i++){
  78.             //join bloqueia a main ate que as threads terminem
  79.             rc = pthread_join(threads[i], NULL);
  80.             if(rc){
  81.                 printf("ERROR: return code from pthread_join() is %d\n", rc);
  82.                 exit(1);
  83.             }
  84.         }
  85.        
  86.         max = maiores[0];
  87.         for(i = 0; i < numThreads; i++){            
  88.             if(max <= maiores[i])
  89.                 max = maiores[i];
  90.         }
  91.  
  92.  
  93.         printf("\nMin:   %d\n", max);
  94.     }
  95.     else
  96.         printf("vetor nul\n");
  97.  
  98.     return 0;
  99. }
Advertisement
RAW Paste Data Copied
Advertisement