Advertisement
EBobkunov

subscriber.c

Oct 8th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6.  
  7. #define MAX_MESSAGE_SIZE 1024
  8.  
  9. int main(int argc, char* argv[]) {
  10.     if (argc != 2) {
  11.         fprintf(stderr, "Usage: %s <subscriber_id>\n", argv[0]);
  12.         exit(EXIT_FAILURE);
  13.     }
  14.  
  15.     int subscriber_id = atoi(argv[1]);
  16.     char fifo_path[20];
  17.     snprintf(fifo_path, sizeof(fifo_path), "/tmp/ex1/s%d", subscriber_id);
  18.  
  19.     int pipe_fd = open(fifo_path, O_RDONLY);
  20.     if (pipe_fd == -1) {
  21.         perror("Failed to open named pipe");
  22.         exit(EXIT_FAILURE);
  23.     }
  24.  
  25.     char message[MAX_MESSAGE_SIZE];
  26.     ssize_t bytes_read;
  27.  
  28.     while ((bytes_read = read(pipe_fd, message, MAX_MESSAGE_SIZE)) > 0) {
  29.         write(STDOUT_FILENO, message, bytes_read);
  30.     }
  31.  
  32.     close(pipe_fd);
  33.  
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement