Advertisement
EBobkunov

ex1 w5 all

Oct 9th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5.  
  6. #define MAX_MESSAGE_SIZE 1024
  7.  
  8. int main() {
  9.     int pipe_fd[2];
  10.     if (pipe(pipe_fd) == -1) {
  11.         perror("pipe");
  12.         exit(EXIT_FAILURE);
  13.     }
  14.  
  15.     pid_t child_pid = fork();
  16.     if (child_pid == -1) {
  17.         perror("fork");
  18.         exit(EXIT_FAILURE);
  19.     }
  20.  
  21.     if (child_pid == 0) {  // Child process (subscriber)
  22.         close(pipe_fd[1]);  // Close write end
  23.         char message[MAX_MESSAGE_SIZE];
  24.         ssize_t read_bytes;
  25.         while ((read_bytes = read(pipe_fd[0], message, sizeof(message))) > 0) {
  26.             write(STDOUT_FILENO, message, read_bytes);
  27.         }
  28.         close(pipe_fd[0]);  // Close read end
  29.     } else {  // Parent process (publisher)
  30.         close(pipe_fd[0]);  // Close read end
  31.         char message[MAX_MESSAGE_SIZE];
  32.         ssize_t read_bytes;
  33.         while ((read_bytes = read(STDIN_FILENO, message, sizeof(message))) > 0) {
  34.             write(pipe_fd[1], message, read_bytes);
  35.         }
  36.         close(pipe_fd[1]);  // Close write end
  37.     }
  38.  
  39.     return 0;
  40. }
  41.  
  42.  
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <unistd.h>
  47. #include <string.h>
  48.  
  49. #define MAX_MESSAGE_SIZE 1024
  50.  
  51. int main(int argc, char *argv[]) {
  52.     if (argc != 2) {
  53.         fprintf(stderr, "Usage: %s <number_of_subscribers>\n", argv[0]);
  54.         exit(EXIT_FAILURE);
  55.     }
  56.  
  57.     int num_subscribers = atoi(argv[1]);
  58.  
  59.     char pipe_name[32];
  60.     int pipe_fd[num_subscribers];
  61.  
  62.     // Create named pipes
  63.     for (int i = 1; i <= num_subscribers; i++) {
  64.         snprintf(pipe_name, sizeof(pipe_name), "/tmp/ex1/s%d", i);
  65.         mkfifo(pipe_name, 0666);
  66.         pipe_fd[i - 1] = open(pipe_name, O_WRONLY);
  67.     }
  68.  
  69.     char message[MAX_MESSAGE_SIZE];
  70.     ssize_t read_bytes;
  71.  
  72.     while ((read_bytes = read(STDIN_FILENO, message, sizeof(message))) > 0) {
  73.         for (int i = 0; i < num_subscribers; i++) {
  74.             write(pipe_fd[i], message, read_bytes);
  75.         }
  76.     }
  77.  
  78.     // Close and remove the pipes
  79.     for (int i = 0; i < num_subscribers; i++) {
  80.         close(pipe_fd[i]);
  81.         snprintf(pipe_name, sizeof(pipe_name), "/tmp/ex1/s%d", i + 1);
  82.         unlink(pipe_name);
  83.     }
  84.  
  85.     return 0;
  86. }
  87.  
  88.  
  89. #include <stdio.h>
  90. #include <stdlib.h>
  91. #include <unistd.h>
  92. #include <string.h>
  93.  
  94. #define MAX_MESSAGE_SIZE 1024
  95.  
  96. int main(int argc, char *argv[]) {
  97.     if (argc != 2) {
  98.         fprintf(stderr, "Usage: %s <subscriber_id>\n", argv[0]);
  99.         exit(EXIT_FAILURE);
  100.     }
  101.  
  102.     int subscriber_id = atoi(argv[1]);
  103.     char pipe_name[32];
  104.     snprintf(pipe_name, sizeof(pipe_name), "/tmp/ex1/s%d", subscriber_id);
  105.  
  106.     int pipe_fd = open(pipe_name, O_RDONLY);
  107.     if (pipe_fd == -1) {
  108.         perror("open");
  109.         exit(EXIT_FAILURE);
  110.     }
  111.  
  112.     char message[MAX_MESSAGE_SIZE];
  113.     ssize_t read_bytes;
  114.  
  115.     while ((read_bytes = read(pipe_fd, message, sizeof(message))) > 0) {
  116.         write(STDOUT_FILENO, message, read_bytes);
  117.     }
  118.  
  119.     close(pipe_fd);
  120.  
  121.     return 0;
  122. }
  123.  
  124.  
  125. #!/bin/bash
  126.  
  127. if [ $# -ne 1 ]; then
  128.     echo "Usage: $0 <number_of_subscribers>"
  129.     exit 1
  130. fi
  131.  
  132. NUM_SUBSCRIBERS=$1
  133.  
  134. # Compile C programs
  135. gcc channel.c -o channel
  136. gcc publisher.c -o publisher
  137. gcc subscriber.c -o subscriber
  138.  
  139. # Run the publisher
  140. ./publisher $NUM_SUBSCRIBERS &
  141.  
  142. # Run subscribers
  143. for ((i = 1; i <= NUM_SUBSCRIBERS; i++)); do
  144.     ./subscriber $i &
  145. done
  146.  
  147. # Wait for processes to finish
  148. wait
  149.  
  150. # Clean up
  151. rm -f /tmp/ex1/s*
  152.  
  153. # Clean up compiled programs
  154. rm -f channel publisher subscriber
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement