Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <sys/time.h>
- #include <time.h>
- #define MAX 2147483647
- int vetorMin;
- long long numElementos = 159999999;
- int numThreads, interPorThread, max;
- unsigned char *vetor, *maiores;
- pthread_mutex_t mutex;
- double MyClock() {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return (tv.tv_sec * 1000000) + tv.tv_usec;
- }
- void *min(void *arg){
- int t = (long)arg, i, ini, fim;
- ini = t*interPorThread;
- unsigned char meuMin;
- if((t == numThreads-1) && (numElementos%numThreads != 0))
- fim = numElementos;
- else
- fim = t*interPorThread + interPorThread;
- meuMin = vetor[ini];
- for(i = ini+1; i < fim; i++){
- if(meuMin <= vetor[i])
- meuMin = vetor[i];
- }
- maiores[t] = meuMin;
- }
- int main(int argc, char *argv[]){
- srand(0);
- //srand( (unsigned)time(NULL) );
- double inicio;
- int i;
- if (argc != 2) {
- fprintf(stderr, "usage: ./a.out <threads>\n");
- exit(1);
- }
- vetor = (unsigned char*)malloc(numElementos* sizeof(unsigned char));
- maiores = (unsigned char*)malloc(numThreads* sizeof(unsigned char));
- if(vetor != NULL){
- for(i = 0; i < numElementos-1; i++){
- vetor[i] = 1;
- }
- vetor[i] = 123;
- //----BUSCA PARALELA----
- numThreads = atoi(argv[1]);
- interPorThread = numElementos/numThreads;
- pthread_mutex_init(&mutex, NULL);
- pthread_t *threads = (pthread_t *)malloc(numThreads*sizeof(pthread_t));
- int rc;
- for(i = 0; i < numThreads; i++){
- rc = pthread_create(&threads[i], NULL, min, (void *)i);
- if(rc){
- printf("ERROR: return code from pthread_create() is %d\n", rc);
- exit(1);
- }
- }
- for (i = 0; i < numThreads; i++){
- //join bloqueia a main ate que as threads terminem
- rc = pthread_join(threads[i], NULL);
- if(rc){
- printf("ERROR: return code from pthread_join() is %d\n", rc);
- exit(1);
- }
- }
- max = maiores[0];
- for(i = 0; i < numThreads; i++){
- if(max <= maiores[i])
- max = maiores[i];
- }
- printf("\nMin: %d\n", max);
- }
- else
- printf("vetor nul\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement