Advertisement
tftrgi11

Untitled

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