int ret1 = mkfifo(FIFO2,0666); if(ret1==-1) { perror("mkfifo"); exit(EXIT_FAILURE); }
int read_fd, write_fd; char buffer[BUFFER_SIZE]; // Open FIFO1 for reading read_fd = open(FIFO1, O_RDONLY); if (read_fd == -1) { perror("open FIFO1"); exit(EXIT_FAILURE); } // Open FIFO2 for writing write_fd = open(FIFO2, O_WRONLY); if (write_fd == -1) { perror("open FIFO2"); close(read_fd); exit(EXIT_FAILURE); } // Read a message from FIFO1 ssize_t count = read(read_fd, buffer, BUFFER_SIZE); if (count == -1) { perror("read"); close(read_fd); close(write_fd); exit(EXIT_FAILURE); } buffer[count] = '\0'; printf("Server received: %s\n", buffer); // Send a response to FIFO2 const char *response = "Hello from the server!"; if (write(write_fd, response, strlen(response)) == -1) { perror("write"); close(read_fd); close(write_fd); exit(EXIT_FAILURE); } close(read_fd); close(write_fd); return 0; }
Here we are doing a full duplex bidirectional communication using 2 FIFOs i.e. fifo1 and fifo2, one for each direction of communication. Both consumer and producer can do both read and write here.