#include #include #include #include #include #include #include // Receive an array of random numbers through FIFO from another process // Calculate the sum and send it back through FIFO to that process int main(int argc, char *argv[]) { // read numbers from process // calculate sum // send sum back int a2b = open("./a2b", O_RDONLY); if(-1 == a2b) { perror("Error on opening fifo a2b"); exit(1); } int b2a = open("./b2a", O_WRONLY); if(-1 == b2a) { close(a2b); perror("Error on opening fifo b2a"); exit(1); } int sum = 0; int nr; int n; if(-1 == read(a2b, &n, sizeof(int))) { perror("Error reading number of elems"); close(a2b); close(b2a); exit(1); } for(int i = 0; i < n; i++) { if(-1 == read(a2b, &nr, sizeof(int))) { perror("Error reading number"); close(a2b); close(b2a); exit(1); } sum += nr; } if(-1 == write(b2a, &sum, sizeof(int))) { perror("Error writing sum to parent"); close(a2b); close(b2a); exit(1); } close(a2b); close(b2a); return 0; }