Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <time.h>
- #include <sys/wait.h>
- #include <unistd.h>
- const int k = 10;
- const int N = 5;
- int sumOfTerms = 0;
- int fillArray(int *arr){
- for (int i = 0;i<k;i++){
- arr[i] = rand()%20;
- }
- }
- void printArray(int *arr){
- for(int i=0;i<k;i++){
- // printf("%d ",arr[i]);
- }
- }
- int findMax(int *arr){
- int max=arr[0];
- for(int i = 0; i<k;i++){
- if(max<arr[i]){
- max = arr[i];
- }
- }
- return max;
- }
- int fillSeriesArray(int *arr, int size){
- arr[0] = 12;
- arr[1] = -8;
- arr[2] = 6;
- for(int i=3;i<size;i++){
- arr[i] = -8 * arr[i-1] - sqrt(2/3 * arr[i-2]) + 13 * arr[i-3];
- }
- }
- int main(){
- srand(time(NULL));
- int max;
- int *randomNumbers = (int*)malloc(sizeof(int)* k);
- fillArray(randomNumbers);
- printArray(randomNumbers);
- max = findMax(randomNumbers);
- int* seriesArray=(int*)malloc(max*sizeof(int));
- fillSeriesArray(seriesArray,max);
- printArray(seriesArray);
- int input[2], output[2];
- pipe(input);
- pipe(output);
- int i = 0;
- // proces nadrzedny wysyla liczbe z tablicy do obliczenia
- for(i =0;i< k;i++){
- write(input[1],&randomNumbers[i],sizeof(randomNumbers[i]));
- }
- for(i = 0;i<N;i++){
- if(fork() == 0){
- while(1){
- int term = 0;
- read(input[0],&term, sizeof(term));
- if(term<0)
- exit(0);
- int result = seriesArray[term];
- write(output[1],&result,sizeof(result));
- }
- }
- }
- //
- for(i = 0; i<N;i++){
- int result = 0;
- read(output[0],&result,sizeof(result));
- sumOfTerms+=result;
- }
- for(i = 0; i<N;i++){
- int terminate = -1;
- write(input[1],&terminate,sizeof(terminate));
- }
- printf("Suma wszystkich wyrazów wynosi: %d",sumOfTerms);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement