Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <sys/time.h>
  7.  
  8. int N;
  9. int P;
  10. pthread_mutex_t s = PTHREAD_MUTEX_INITIALIZER;
  11. int Min = 999;
  12. int Max = -1;
  13.  
  14. int *A;
  15.  
  16. void vermatriz(int *m, int modo)
  17. {
  18.     for (int i = 0; i < N; i++)
  19.     {
  20.         for (int j = 0; j < N; j++)
  21.             printf("%d\t", m[i * N + j]);
  22.         printf("\n");
  23.     }
  24. }
  25.  
  26. void *Funcion_maxmin(void *arg)
  27. {
  28.  
  29.     int tid = *(int *)arg;
  30.     int ini = tid * (P);
  31.     int fin = (tid + 1) * (P);
  32.     int min = 999;
  33.     int max = -1;
  34.  
  35.     for (int i = ini; i < fin; i++)
  36.     {
  37.         for (int j = 0; j < N; j++)
  38.         {
  39.             if (A[i * N + j] < min)
  40.                 min = A[i * N + j];
  41.             if (A[i * N + j] > max)
  42.                 max = A[i * N + j];
  43.         }
  44.     }
  45.     // se lo pasamos a la variable global
  46.     printf("Soy %d\tmin=%d\tmax=%d\n", tid, min, max);
  47.     pthread_mutex_lock(&s);
  48.     if (min < Min)
  49.         Min = min;
  50.     pthread_mutex_unlock(&s);
  51.  
  52.     pthread_mutex_lock(&s);
  53.     if (max > Max)
  54.         Max = max;
  55.  
  56.     pthread_mutex_unlock(&s);
  57.     pthread_exit(NULL); //finalizacion del hilo
  58. }
  59.  
  60. int main(int argc, char *argv[])
  61. {
  62.     int H = 2; // es un valor por defecto en el caso de que no se
  63.     // introduzca ningun valor por consola.
  64.  
  65.     //Controla los argumentos al programa
  66.     if ((argc != 3) || ((N = atoi(argv[1])) <= 0) || ((H = atoi(argv[2])) <= 0))
  67.     {
  68.         printf("\nUsar: %s n\n  h: cant hilos  \n", argv[0]);
  69.         exit(1);
  70.     }
  71.  
  72.     /*Declarar e inicializaciones*/
  73.     N = atoi(argv[1]); // Inicializa valor de mi arreglo de N elementos
  74.     H = atoi(argv[2]); //incializar valor de hilos
  75.  
  76.     //calculamos la proporcion
  77.     P = N / H;
  78.  
  79.     /*Matriz A original*/
  80.     A = (int *)malloc(N * N * sizeof(int));
  81.  
  82.     for (int i = 0; i < N; i++)
  83.         for (int j = 0; j < N; j++)
  84.             A[i * N + j] = rand()%100+1;
  85.     vermatriz(A, 0);
  86.  
  87.     //Reservo memoria para el vector de hilos
  88.     pthread_t *tid = malloc(sizeof(pthread_t) * H);
  89.     int *threads_ids = malloc(sizeof(int) * H);
  90.     pthread_mutex_init(&s, NULL); // inicializacion s
  91.  
  92.     for (int t = 0; t < H; t++)
  93.     {
  94.         threads_ids[t] = t;
  95.         pthread_create(&tid[t], NULL, Funcion_maxmin, (void *)&threads_ids[t]);
  96.     }
  97.    
  98.     for (int t = 0; t < H; t++)
  99.         pthread_join(tid[t], NULL);
  100.  
  101.     pthread_mutex_destroy(&s);
  102.  
  103.     printf("resultado max es: %d \n", Max);
  104.     printf("resultado min es: %d \n", Min);
  105.  
  106.     /*Matriz A original*/
  107.     free(A);
  108.     return (0);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement