Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. //Michal A Wilk
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <signal.h>
  7.  
  8. #define RMIN -15
  9. #define RMAX 15
  10.  
  11. double fun( double x )
  12. {
  13. return ( 8*pow(x,5) - 2*pow(x,4) + 15*pow(x,2) - 24*x + 18 );
  14. }
  15.  
  16. int main( int argc, char* argv[] )
  17. {
  18. printf("start parent\n");
  19. if(argc != 3)
  20. return 1;
  21. double n = atof(argv[1]);
  22. int k = atoi(argv[2]);
  23. int pipecp[2];
  24. double range = 30.0/(double)k;
  25. printf("RANGE %f\n",range);
  26. pipe( pipecp );
  27. int i;
  28. for( i = 0; i < k; ++i )
  29. {
  30. int forkval = fork();
  31. if( forkval < 0 )
  32. return 2;
  33. if( forkval == 0 )
  34. {
  35. printf("child created\n");
  36. double min = RMIN + range*i;
  37. double max = min + range;
  38. double result = 0.0;
  39. if( min >= RMAX )
  40. {
  41. printf("%f - %f [%f] : %f\n",min,max,range,result);
  42. write( pipecp[1], &result, sizeof(result) );
  43. exit(0);
  44. }
  45. if( max > RMAX )
  46. max = RMAX;
  47. double j;
  48.  
  49. for( j = min; j < max; j += n )
  50. result += ( ( fun(j+n) + fun(j) )/2 * n);
  51. printf("%f - %f [%f] : %f\n",min,max,range,result);
  52. write( pipecp[1], &result, sizeof(result) );
  53. printf("child exit\n");
  54. exit(0);
  55. break;
  56. }
  57. printf("loop iterate\n");
  58. }
  59. printf("loop exit\n");
  60. while( wait(0) > 0) {}
  61. printf("children dead\n");
  62. double sum = 0;
  63. double part;
  64. for( i = 0; i < k; ++i )
  65. {
  66. read( pipecp[0], &part, sizeof(part) );
  67. printf("%f\n", part);
  68. sum += part;
  69. }
  70. printf("WYNIK KONCOWY : %f\n", sum);
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement