Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <dirent.h>
  5. #include <sys/types.h>
  6.  
  7. void *th_rt(void *arg);
  8.  
  9. int MIN = -1;
  10.  
  11. pthread_mutex_t MutexMIN = PTHREAD_MUTEX_INITIALIZER;
  12.  
  13. int main(int argc, char const *argv[]){
  14.  
  15.     pthread_t tid[5];
  16.  
  17.     int i;
  18.  
  19.     if(argc < 6){
  20.         printf("usage: ./a.out <dir> <dir> <dir> <dir> <dir> \n");
  21.         return -1;
  22.     }
  23.  
  24.     for(i=0;i<5;i++)
  25.         pthread_create(&tid[i],NULL,th_rt,(void *)argv[i+1]);
  26.  
  27.     for(i=0;i<5;i++)
  28.         pthread_join(tid[i],NULL);
  29.  
  30.     printf("Min: %d\n",MIN);
  31.  
  32.     return 0;
  33. }
  34.  
  35. void *th_rt(void *arg){
  36.     int counter=0;
  37.     char *dirName;
  38.     DIR *d;
  39.  
  40.     dirName = (char *)arg;
  41.     d = opendir(dirName);
  42.  
  43.     while(readdir(d) != NULL)
  44.         counter++;
  45.  
  46.     printf("%s - %d\n",dirName,counter);
  47.  
  48.     pthread_mutex_lock(&MutexMIN);
  49.     if(counter < MIN || MIN == -1) MIN = counter;
  50.     pthread_mutex_unlock(&MutexMIN);
  51.  
  52.     pthread_exit(NULL);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement