#include #include #include #include #include // this process says receives hi from another process // and send back another hi int main() { int r_fd = open("./fifo_a2b", O_RDONLY); int w_fd = open("./fifo_b2a", O_WRONLY); printf("I am process with PID %d\n", getpid()); char *msg; int size = 0; if(-1 == read(r_fd, &size, sizeof(int))) { perror("Error reading message length"); } if(size > 0) { msg = malloc(sizeof(char) * (size + 1)); if(-1 == read(r_fd, msg, sizeof(char) * size)) { perror("Error reading message"); } msg[size] = 0; printf("Received message: %s\n", msg); free(msg); } msg = malloc(sizeof(char) * 100); memset(msg, 0, sizeof(char) * 100); sprintf(msg, "Hello, my name is %d", getpid()); size = strlen(msg); if(-1 == write(w_fd, &size, sizeof(int))) { perror("Error sending message length"); } if(-1 == write(w_fd, msg, size * sizeof(char))) { perror("Error saying hi"); } free(msg); close(r_fd); close(w_fd); return 0; }