dykow

Untitled

May 4th, 2021
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. double funkcja(int i, int n)
  5.         {
  6.         double p = ((i*i)-12);
  7.         double l = (p*p*p);
  8.         double m = (n*n*n*i*i*i);
  9.         return l/m;
  10.         }
  11.        
  12. int main(int argc, char* argv[])
  13. {              
  14.         int n = atoi(argv[1]);
  15.         int k = atoi(argv[2]);
  16.         int j=0;
  17.         int i=1;
  18.         double suma, e, tmp;
  19.         int pipefd[2];
  20.         pid_t pid;
  21.        
  22.         if(pipe(pipefd) == -1)
  23.         {
  24.                 perror("err pipe\n");
  25.                 exit(EXIT_FAILURE);
  26.         }
  27.        
  28.         while(i<=n)
  29.         {
  30.        
  31.                 for(j=0; j<k; j++)
  32.                 {
  33.                         if(i>n)
  34.                                 break;
  35.                         pid = fork();
  36.        
  37.                         if(pid == -1)
  38.                         {
  39.                                 perror("err fork\n");
  40.                                 exit(EXIT_FAILURE);
  41.                         }
  42.        
  43.                         if(pid==0)
  44.                         {
  45.                                 e=funkcja(i,n);
  46.                                 write(pipefd[1], &e, sizeof(double));
  47.                                 exit(EXIT_SUCCESS);
  48.                         }
  49.                         i++;
  50.                 }
  51.         }
  52.        
  53.         close(pipefd[1]);
  54.         while(read(pipefd[0], &tmp, sizeof(double)))
  55.                 suma+=tmp;
  56.         printf("suma: %f\n", suma);
  57.         return EXIT_SUCCESS;  
  58. }
  59.  
  60.  
Advertisement
Add Comment
Please, Sign In to add comment