Advertisement
tftrgi11

Untitled

Jun 19th, 2020
1,484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <getopt.h>
  5. #include <stdbool.h>
  6. #include <sys/time.h>
  7.  
  8. #include <pthread.h>
  9.  
  10. int begin = 2;
  11. int end = 0;
  12. int mod = 1;
  13.  
  14. pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  15.  
  16.  
  17. int Fact(const int *args) {
  18.   int a = -1;
  19.   int b = 1;
  20.   int m = mod;
  21.   int prod = 1;
  22.   while (a<=b)
  23.   {
  24.       pthread_mutex_lock(&mut);
  25.       a = begin;
  26.       b = end;
  27.       ++begin;
  28.       --end;
  29.       while (a == begin || b == end);
  30.       if (a==b)
  31.       {
  32.           prod = (prod * a) % m;
  33.           pthread_mutex_unlock(&mut);
  34.           break;
  35.       }
  36.       if (a>b)
  37.       {
  38.           pthread_mutex_unlock(&mut);
  39.           break;
  40.       }
  41.       pthread_mutex_unlock(&mut);
  42.       prod = (prod * a * b) % m;
  43.   }
  44.   return prod;
  45. }
  46.  
  47. void *ThreadFact(void *args) {
  48.   return (void *)(size_t)Fact(args);
  49. }
  50.  
  51. int main(int argc, char **argv) {
  52.   int k = 0;
  53.   int pnum = 0;
  54.   pthread_t threads[pnum];
  55.  
  56.   while (true) {
  57.     int current_optind = optind ? optind : 1;
  58.  
  59.     static struct option options[] = {{"k", required_argument, 0, 0},
  60.                                       {"pnum", required_argument, 0, 0},
  61.                                       {"mod", required_argument, 0, 0},
  62.                                       {0, 0, 0, 0}};
  63.  
  64.     int option_index = 0;
  65.     int c = getopt_long(argc, argv, "f", options, &option_index);
  66.  
  67.     if (c == -1) break;
  68.  
  69.     switch (c) {
  70.       case 0:
  71.         switch (option_index) {
  72.           case 0:
  73.             k = atoi(optarg);
  74.             end = k;
  75.             if (k <= 0)
  76.             {
  77.                 printf("Threads number must be positive\n");
  78.                 return 1;
  79.             }
  80.             break;
  81.           case 1:
  82.             pnum = atoi(optarg);
  83.             if (pnum <= 0)
  84.             {
  85.                 printf("Seed must be positive\n");
  86.                 return 1;
  87.             }
  88.             break;
  89.           case 2:
  90.             mod = atoi(optarg);
  91.             if (mod <= 0)
  92.             {
  93.                 printf("Array size must be positive\n");
  94.                 return 1;
  95.             }
  96.             break;
  97.           defalut:
  98.             printf("Index %d is out of options\n", option_index);
  99.             break;
  100.         }
  101.         break;
  102.  
  103.       case '?':
  104.         break;
  105.  
  106.       default:
  107.         printf("getopt returned character code 0%o?\n", c);
  108.         break;
  109.     }
  110.   }
  111.  
  112. if (optind < argc) {
  113.     printf("Has at least one no option argument\n");
  114.     return 1;
  115.   }
  116.  
  117. if (mod == 1 || pnum == 0 || k == 0) {
  118.     printf("Usage: %s --threads_num \"num\" --seed \"num\" --array_size \"num\" \n",
  119.            argv[0]);
  120.     return 1;
  121. }
  122.  
  123.  
  124.   int *array = malloc(sizeof(int) * 1);
  125.  
  126.   struct timeval start_time;
  127.   gettimeofday(&start_time, NULL);
  128.  
  129.   for (uint32_t i = 0; i < pnum; i++) {
  130.     if (pthread_create(&threads[i], NULL, ThreadFact, (void *)&array)) {
  131.       printf("Error: pthread_create failed!\n");
  132.       return 1;
  133.     }
  134.   }
  135.  
  136.   int total_prod = 1;
  137.   for (uint32_t i = 0; i < pnum; i++) {
  138.     int prod = 0;
  139.     pthread_join(threads[i], (void **)&prod);
  140.     total_prod = (total_prod * prod) % mod;
  141.   }
  142.  
  143.   struct timeval finish_time;
  144.   gettimeofday(&finish_time, NULL);
  145.   double elapsed_time = (double)(finish_time.tv_usec - start_time.tv_usec)/1000;
  146.  
  147.   free(array);
  148.   printf("Total: %d\n", total_prod);
  149.   printf("Elapsed time: %fms\n", elapsed_time);
  150.   return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement