#include #include #include #include #include #include #include #include // Send an array of random numbers through FIFO to another process // The other process calculates the sum and sends it back through FIFO int main(int argc, char *argv[]) { if(-1 == mkfifo("./a2b", 0600)) { perror("Error creating fifo a2b"); exit(1); } if(-1 == mkfifo("./b2a", 0600)) { perror("Error creating fifo b2a"); exit(1); } int a2b = open("./a2b", O_WRONLY); if(-1 == a2b) { perror("Error on opening fifo a2b"); exit(1); } int b2a = open("./b2a", O_RDONLY); if(-1 == b2a) { close(a2b); perror("Error on opening fifo b2a"); exit(1); } srandom(getpid()); int n; printf("n="); scanf("%d", &n); if(n > 0) { int *arr = malloc(sizeof(int) * n); // initialize array with random values for(int i = 0; i < n; i++) { arr[i] = random() % 100; printf("a[%d] = %d\n", i, arr[i]); } // send array to other process if(-1 == write(a2b, &n, sizeof(int))) { perror("Error writing array size"); free(arr); close(a2b); close(b2a); exit(1); } for(int i = 0; i < n; i++) { if(-1 == write(a2b, &arr[i], sizeof(int))) { perror("Error writing array"); free(arr); close(a2b); close(b2a); exit(1); } } free(arr); int sum = 0; // read sum if(-1 == read(b2a, &sum, sizeof(int))) { perror("Error reading sum"); close(a2b); close(b2a); exit(1); } printf("sum = %d\n", sum); } close(a2b); close(b2a); unlink("./a2b"); unlink("./b2a"); return 0; }