Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #define MAX_MESSAGE_SIZE 1024
- int main() {
- int pipe_fd[2];
- if (pipe(pipe_fd) == -1) {
- perror("pipe");
- exit(EXIT_FAILURE);
- }
- pid_t child_pid = fork();
- if (child_pid == -1) {
- perror("fork");
- exit(EXIT_FAILURE);
- }
- if (child_pid == 0) { // Child process (subscriber)
- close(pipe_fd[1]); // Close write end
- char message[MAX_MESSAGE_SIZE];
- ssize_t read_bytes;
- while ((read_bytes = read(pipe_fd[0], message, sizeof(message))) > 0) {
- write(STDOUT_FILENO, message, read_bytes);
- }
- close(pipe_fd[0]); // Close read end
- } else { // Parent process (publisher)
- close(pipe_fd[0]); // Close read end
- char message[MAX_MESSAGE_SIZE];
- ssize_t read_bytes;
- while ((read_bytes = read(STDIN_FILENO, message, sizeof(message))) > 0) {
- write(pipe_fd[1], message, read_bytes);
- }
- close(pipe_fd[1]); // Close write end
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #define MAX_MESSAGE_SIZE 1024
- int main(int argc, char *argv[]) {
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <number_of_subscribers>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- int num_subscribers = atoi(argv[1]);
- char pipe_name[32];
- int pipe_fd[num_subscribers];
- // Create named pipes
- for (int i = 1; i <= num_subscribers; i++) {
- snprintf(pipe_name, sizeof(pipe_name), "/tmp/ex1/s%d", i);
- mkfifo(pipe_name, 0666);
- pipe_fd[i - 1] = open(pipe_name, O_WRONLY);
- }
- char message[MAX_MESSAGE_SIZE];
- ssize_t read_bytes;
- while ((read_bytes = read(STDIN_FILENO, message, sizeof(message))) > 0) {
- for (int i = 0; i < num_subscribers; i++) {
- write(pipe_fd[i], message, read_bytes);
- }
- }
- // Close and remove the pipes
- for (int i = 0; i < num_subscribers; i++) {
- close(pipe_fd[i]);
- snprintf(pipe_name, sizeof(pipe_name), "/tmp/ex1/s%d", i + 1);
- unlink(pipe_name);
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #define MAX_MESSAGE_SIZE 1024
- int main(int argc, char *argv[]) {
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <subscriber_id>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- int subscriber_id = atoi(argv[1]);
- char pipe_name[32];
- snprintf(pipe_name, sizeof(pipe_name), "/tmp/ex1/s%d", subscriber_id);
- int pipe_fd = open(pipe_name, O_RDONLY);
- if (pipe_fd == -1) {
- perror("open");
- exit(EXIT_FAILURE);
- }
- char message[MAX_MESSAGE_SIZE];
- ssize_t read_bytes;
- while ((read_bytes = read(pipe_fd, message, sizeof(message))) > 0) {
- write(STDOUT_FILENO, message, read_bytes);
- }
- close(pipe_fd);
- return 0;
- }
- #!/bin/bash
- if [ $# -ne 1 ]; then
- echo "Usage: $0 <number_of_subscribers>"
- exit 1
- fi
- NUM_SUBSCRIBERS=$1
- # Compile C programs
- gcc channel.c -o channel
- gcc publisher.c -o publisher
- gcc subscriber.c -o subscriber
- # Run the publisher
- ./publisher $NUM_SUBSCRIBERS &
- # Run subscribers
- for ((i = 1; i <= NUM_SUBSCRIBERS; i++)); do
- ./subscriber $i &
- done
- # Wait for processes to finish
- wait
- # Clean up
- rm -f /tmp/ex1/s*
- # Clean up compiled programs
- rm -f channel publisher subscriber
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement