Advertisement
Bkmz

Untitled

Apr 24th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <malloc.h>
  5. #include <time.h>
  6.  
  7. #include <sys/wait.h>
  8.  
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11.  
  12.  
  13. //amount of data in source array
  14. #define N 32
  15. // K defenition from task
  16. #define K 5
  17.  
  18. // maximum integer in source array
  19. #define MAX 128
  20. #define THREADS 4
  21.  
  22. struct equation {
  23.     int a;
  24.     int b;
  25. };
  26.  
  27. int *array;
  28. struct equation res[THREADS];
  29. int res_max[THREADS];
  30.  
  31.  
  32. int pids[THREADS][2];
  33.  
  34. void generate_array()
  35. {
  36.     srand(time(NULL));
  37.     int i;
  38.     array = (int *)malloc(sizeof(int)*N);
  39.     for (i=0;i<N;i++)
  40.     {
  41.         array[i] = rand() % MAX;
  42.     }
  43. }
  44.  
  45. void print(int *data)
  46. {
  47.     int i;
  48.     for(i=0;i<N;i++)
  49.     {
  50.         printf("%i ", data[i]);
  51.     }
  52.     printf("\n");
  53. }
  54.  
  55. struct equation make_equation(int a, int b)
  56. {
  57.     return *(struct equation[]){{a,b}};
  58. }
  59.  
  60.  
  61. int main(void)
  62. {
  63.     // loop index
  64.     int i,j;
  65.     pid_t p_id;
  66.     // equation variables:
  67.     int a,b;
  68.  
  69. //    if (THREADS < 4)
  70. //    {
  71. //        fprintf(stderr, "Minimum threads are: 4. Your's: %i\n", THREADS);
  72. //        exit(1);
  73. //    }
  74.  
  75.  
  76.     generate_array();
  77.     print(array);
  78.  
  79.     printf("K: %i\n\n", K);
  80.  
  81.     int max = -1;
  82.     struct equation tmp;
  83.  
  84.     for (p_id=0;p_id<THREADS;p_id++)
  85.     {
  86.         pipe(pids[p_id]);
  87.         pid_t pid = fork(); // <<<<-------------- FORK!!!
  88.  
  89.         if (pid == 0)
  90.         {//child
  91.             for(i=0;i<N;++i)
  92.             {
  93.                 for(j=p_id;j<N;j+=THREADS)
  94.                 {
  95.                     a = array[i];
  96.                     b = array[j];
  97.  
  98.                     if (b-a == K)
  99.                     {
  100.                         if (max < abs(a) * abs(b))
  101.                         {
  102.                             max = abs(a) * abs(b);
  103.                             tmp.a = a;
  104.                             tmp.b = b;
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109. //            printf("|%i|*|%i| = %i\n", tmp.a, tmp.b, max);
  110.             char buf[4096];
  111.             sprintf(buf, "%i;%i;%i", tmp.a,tmp.b,max);
  112.  
  113.             close(pids[p_id][0]);
  114.             write(pids[p_id][1], buf, 4096);
  115.  
  116. //            printf("Sended\n");
  117.             return 0; //stopping child
  118.         }else{
  119.             //parent
  120.             usleep(150); // for pretty printf :)
  121.         }
  122.     }
  123.  
  124.     //parent
  125.     max = -1;
  126.     tmp = *(struct equation[]){{0,0}};
  127.     pid_t tmp_pid;
  128.  
  129.     for (p_id=0;p_id<THREADS;p_id++)
  130.     {
  131.         wait(NULL);
  132.  
  133.         char buf[4096];
  134.         close(pids[p_id][1]);
  135.         read(pids[p_id][0],buf, 4096);
  136.         sscanf(buf, "%i;%i;%i", &res[p_id].a, &res[p_id].b, &res_max[p_id]);
  137.         printf("th: %i Received: |%i|*|%i| = %i\n", p_id, res[p_id].a, res[p_id].b, res_max[p_id]);
  138.         if (max < abs(res[p_id].a) * abs(res[p_id].b))
  139.         {
  140.             max = abs(res[p_id].a) * abs(res[p_id].b);
  141.             tmp = *(struct equation[]){{res[p_id].a,res[p_id].b}};
  142.             tmp_pid = p_id;
  143.         }
  144.  
  145.     }
  146.  
  147. printf("Max found\n");
  148. printf("|%i| * |%i| = %i\n", res[tmp_pid].a, res[tmp_pid].b, max);
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement