Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- double funkcja(int i, int n)
- {
- double p = ((i*i)-12);
- double l = (p*p*p);
- double m = (n*n*n*i*i*i);
- return l/m;
- }
- int main(int argc, char* argv[])
- {
- int n = atoi(argv[1]);
- int k = atoi(argv[2]);
- int j=0;
- int i=1;
- double suma, e, tmp;
- int pipefd[2];
- pid_t pid;
- if(pipe(pipefd) == -1)
- {
- perror("err pipe\n");
- exit(EXIT_FAILURE);
- }
- while(i<=n)
- {
- for(j=0; j<k; j++)
- {
- if(i>n)
- break;
- pid = fork();
- if(pid == -1)
- {
- perror("err fork\n");
- exit(EXIT_FAILURE);
- }
- if(pid==0)
- {
- e=funkcja(i,n);
- write(pipefd[1], &e, sizeof(double));
- exit(EXIT_SUCCESS);
- }
- i++;
- }
- }
- close(pipefd[1]);
- while(read(pipefd[0], &tmp, sizeof(double)))
- suma+=tmp;
- printf("suma: %f\n", suma);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment