Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #define MAX_MESSAGE_SIZE 1024
- int main(int argc, char *argv[]) {
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <subscriber index>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- int subscriber_index = atoi(argv[1]);
- char pipe_name[20]; // "/tmp/ex1/sX" where X is the subscriber index
- char message[MAX_MESSAGE_SIZE];
- // Create the named pipe filename for this subscriber
- snprintf(pipe_name, sizeof(pipe_name), "/tmp/ex1/s%d", subscriber_index);
- int pipe_fd = open(pipe_name, O_RDONLY);
- if (pipe_fd == -1) {
- perror("Failed to open the named pipe");
- exit(EXIT_FAILURE);
- }
- while (1) {
- ssize_t bytes_read = read(pipe_fd, message, MAX_MESSAGE_SIZE);
- if (bytes_read <= 0) {
- break; // Exit if the pipe is closed or an error occurs
- }
- write(STDOUT_FILENO, message, bytes_read); // Print the received message
- }
- close(pipe_fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement