Advertisement
Guest User

ak

a guest
May 30th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. // Mateusz Pindel
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. double licz (int i,int n){
  7. return ((i*i-12)*(i*i-12)*(i*i-12))/(n*n*n*i*i*i);
  8. }
  9.  
  10.  
  11. int main(int argc, char *argv[]){
  12.  
  13. double dataFromPipe;
  14. double sum = 0;
  15. double a;
  16. int i = 0, n, k;
  17. int countElementsByProces;
  18. int isChild;
  19. int potok[2];
  20. pipe(potok);
  21.  
  22. if( argc != 3 ) printf("Bledna ilosc parametrow");
  23. else {
  24. n = atoi(argv[1]);
  25. printf("Ilosc elementow: %d\n", n);
  26.  
  27. k = atoi(argv[2]);
  28. printf("Ilosc procesow potomnych: %d\n", k);
  29. printf("\n");
  30. }
  31.  
  32. countElementsByProces = n / k;
  33.  
  34. while ( isChild != 0 && i < k){
  35. isChild = fork();
  36. i++;
  37. }
  38.  
  39. // dziecko
  40. if( isChild == 0 ) {
  41.  
  42. double tmpsum = 0;
  43. printf ("Proces potomny: %d\t", i);
  44.  
  45. int j = i;
  46.  
  47. for(j; j <= n; j += k)
  48. {
  49. tmpsum += licz(j, n);
  50. }
  51.  
  52. printf("Obliczona suma procesu potomnego: %f\n", tmpsum);
  53. write(potok[1], &tmpsum, sizeof(double));
  54. exit(0);
  55.  
  56. }
  57. // rodzic
  58. else
  59. {
  60.  
  61. for(i=0; i < k; i++)
  62. {
  63. read(potok[0], &a, sizeof(double));
  64. sum = sum + a;
  65. }
  66. printf("\n\nSuma calkowita: %f", sum);
  67.  
  68. }
  69.  
  70.  
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement