Advertisement
tftrgi11

parallel

Jun 18th, 2020
1,379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.90 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <limits.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8.  
  9. #include <sys/time.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12.  
  13. #include <getopt.h>
  14.  
  15. #include "find_min_max.h"
  16. #include "utils.h"
  17.  
  18. int main(int argc, char **argv) {
  19.   int seed = -1;
  20.   int array_size = -1;
  21.   int pnum = -1;
  22.   bool with_files = false;
  23.  
  24.   while (true) {
  25.     int current_optind = optind ? optind : 1;
  26.  
  27.     static struct option options[] = {{"seed", required_argument, 0, 0},
  28.                                       {"array_size", required_argument, 0, 0},
  29.                                       {"pnum", required_argument, 0, 0},
  30.                                       {"by_files", no_argument, 0, 'f'},
  31.                                       {0, 0, 0, 0}};
  32.  
  33.     int option_index = 0;
  34.     int c = getopt_long(argc, argv, "f", options, &option_index);
  35.  
  36.     if (c == -1) break;
  37.  
  38.     switch (c) {
  39.       case 0:
  40.         switch (option_index) {
  41.           case 0:
  42.             seed = atoi(optarg);
  43.             if (seed <= 0)
  44.             {
  45.                 printf("Seed must be positive\n");
  46.                 return 1;
  47.             }
  48.             break;
  49.           case 1:
  50.             array_size = atoi(optarg);
  51.             if (array_size <= 0)
  52.             {
  53.                 printf("Array size must be positive\n");
  54.                 return 1;
  55.             }
  56.             break;
  57.           case 2:
  58.             pnum = atoi(optarg);
  59.             if (pnum <= 0)
  60.             {
  61.                 printf("Process number must be positive\n");
  62.                 return 1;
  63.             }
  64.             break;
  65.           case 3:
  66.             with_files = true;
  67.             break;
  68.  
  69.           defalut:
  70.             printf("Index %d is out of options\n", option_index);
  71.             break;
  72.         }
  73.         break;
  74.       case 'f':
  75.         with_files = true;
  76.         break;
  77.  
  78.       case '?':
  79.         break;
  80.  
  81.       default:
  82.         printf("getopt returned character code 0%o?\n", c);
  83.         break;
  84.     }
  85.   }
  86.  
  87.   if (optind < argc) {
  88.     printf("Has at least one no option argument\n");
  89.     return 1;
  90.   }
  91.  
  92.   if (seed == -1 || array_size == -1 || pnum == -1) {
  93.     printf("Usage: %s --seed \"num\" --array_size \"num\" --pnum \"num\" \n",
  94.            argv[0]);
  95.     return 1;
  96.   }
  97.  
  98.   int *array = malloc(sizeof(int) * array_size);
  99.   GenerateArray(array, array_size, seed);
  100.   int active_child_processes = 0;
  101.  
  102.   struct timeval start_time;
  103.   gettimeofday(&start_time, NULL);
  104.  
  105.   int (*fd)[2] = malloc(sizeof(int) * 2 * pnum);
  106.   for (int i = 0; i < pnum; i++)
  107.   {
  108.       if (pipe(fd[i])==-1)
  109.           {
  110.               printf("Pipe Failed\n");
  111.               return 1;
  112.           }
  113.   }
  114.  
  115.   for (int i = 0; i < pnum; i++) {
  116.     pid_t child_pid = fork();
  117.     if (child_pid >= 0) {
  118.       // successful fork
  119.       active_child_processes += 1;
  120.       if (child_pid == 0) {
  121.         // child process
  122.         int arrPart = array_size / pnum;
  123.         struct MinMax min_max0 = GetMinMax(array, i*arrPart, (i+1)*arrPart);
  124.  
  125.         if (with_files) {
  126.             FILE * fp1;
  127.             FILE * fp2;
  128.             fp1 = fopen ("min.txt", "a");
  129.             fp2 = fopen ("max.txt",  "a");
  130.             if (fp1 != NULL && fp2 != NULL)
  131.             {
  132.                 fprintf(fp1, " %d", min_max0.min);
  133.                 fprintf(fp2, " %d", min_max0.max);
  134.                 fclose(fp1);
  135.                 fclose(fp2);
  136.             }
  137.             else
  138.             {
  139.                 printf("Could not open files for writing\n");
  140.                 return 1;
  141.             }
  142.         }
  143.         else
  144.         {
  145.           // use pipe here
  146.  
  147.           struct MinMax min_max0 = GetMinMax(array, i*arrPart, (i+1)*arrPart);
  148.           int max_len = 30;
  149.           char buff[max_len];
  150.           char strMin[max_len/2];
  151.           char strMax[max_len/2];
  152.           sprintf(strMin, "%d", min_max0.min);
  153.           sprintf(strMax, "%d", min_max0.max);
  154.           strcpy(buff, strMin);
  155.           strcat(buff, " ");
  156.           strcat(buff, strMax);
  157.           close(fd[i][0]);
  158.           write(fd[i][1], buff, max_len);
  159.           close(fd[i][1]);
  160.         }
  161.         return 0;
  162.       }
  163.  
  164.     } else {
  165.       printf("Fork failed!\n");
  166.       return 1;
  167.     }
  168.   }
  169.   pid_t wpid;
  170.   int status = 0;
  171.   while ((wpid = wait(&status)) > 0);
  172.   /*while (active_child_processes > 0) {
  173.     active_child_processes -= 1;
  174.   }*/
  175.   struct MinMax min_max;
  176.   min_max.min = INT_MAX;
  177.   min_max.max = INT_MIN;
  178.  
  179.   for (int i = 0; i < pnum; i++) {
  180.     int min = INT_MAX;
  181.     int max = INT_MIN;
  182.  
  183.     if (with_files) {
  184.       FILE * fp1;
  185.       FILE * fp2;
  186.       fp1 = fopen ("min.txt", "r");
  187.       fp2 = fopen ("max.txt",  "r");
  188.       if (fp1 != NULL && fp2 != NULL)
  189.       {
  190.           fscanf(fp1, "%d", &min);
  191.           fscanf(fp2, "%d", &max);
  192.           fclose(fp1);
  193.           fclose(fp2);
  194.       }
  195.       else
  196.       {
  197.           printf("Could not open files for reading\n");
  198.           return 1;
  199.       }
  200.  
  201.     } else {
  202.         char buff[30];
  203.             if ( read( fd[i][0], buff, sizeof(buff)) <= 0) //read from pipe
  204.                 {
  205.                     perror( "read failed" );
  206.                     return 1;
  207.                 }
  208.                 char * token = strtok(buff, " ");
  209.                 min = atoi(token);
  210.                 token = strtok(NULL, " ");
  211.                 max = atoi(token);
  212.     }
  213.  
  214.     if (min < min_max.min) min_max.min = min;
  215.     if (max > min_max.max) min_max.max = max;
  216.   }
  217.  
  218.   struct timeval finish_time;
  219.   gettimeofday(&finish_time, NULL);
  220.  
  221.   double elapsed_time = (finish_time.tv_sec - start_time.tv_sec) * 1000.0;
  222.   elapsed_time += (finish_time.tv_usec - start_time.tv_usec) / 1000.0;
  223.  
  224.   free(array);
  225.   free(fd);
  226.  
  227.   printf("Min: %d\n", min_max.min);
  228.   printf("Max: %d\n", min_max.max);
  229.   printf("Elapsed time: %fms\n", elapsed_time);
  230.   fflush(NULL);
  231.   return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement