Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Michal A Wilk
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <signal.h>
- #define RMIN -15
- #define RMAX 15
- double fun( double x )
- {
- return ( 8*pow(x,5) - 2*pow(x,4) + 15*pow(x,2) - 24*x + 18 );
- }
- int main( int argc, char* argv[] )
- {
- printf("start parent\n");
- if(argc != 3)
- return 1;
- double n = atof(argv[1]);
- int k = atoi(argv[2]);
- int pipecp[2];
- double range = 30.0/(double)k;
- printf("RANGE %f\n",range);
- pipe( pipecp );
- int i;
- for( i = 0; i < k; ++i )
- {
- int forkval = fork();
- if( forkval < 0 )
- return 2;
- if( forkval == 0 )
- {
- printf("child created\n");
- double min = RMIN + range*i;
- double max = min + range;
- double result = 0.0;
- if( min >= RMAX )
- {
- printf("%f - %f [%f] : %f\n",min,max,range,result);
- write( pipecp[1], &result, sizeof(result) );
- exit(0);
- }
- if( max > RMAX )
- max = RMAX;
- double j;
- for( j = min; j < max; j += n )
- result += ( ( fun(j+n) + fun(j) )/2 * n);
- printf("%f - %f [%f] : %f\n",min,max,range,result);
- write( pipecp[1], &result, sizeof(result) );
- printf("child exit\n");
- exit(0);
- break;
- }
- printf("loop iterate\n");
- }
- printf("loop exit\n");
- while( wait(0) > 0) {}
- printf("children dead\n");
- double sum = 0;
- double part;
- for( i = 0; i < k; ++i )
- {
- read( pipecp[0], &part, sizeof(part) );
- printf("%f\n", part);
- sum += part;
- }
- printf("WYNIK KONCOWY : %f\n", sum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement