0

I want to write a program with 1 sender and 3 receivers. The sender can send individual message to each receivers and group message to all receivers. I am using named pipes to achieve this but can't send group message to all receivers synchronously. Any idea to send broadcast message with named pipe?

Sender program:

/* Sender */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
 char pipename1[] = "/tmp/pipe1";
 char pipename2[] = "/tmp/pipe2";
 char pipename3[] = "/tmp/pipe3";
 char pipename4[] = "/tmp/pipe4";
 char buf1[80];
 char buf2[80];
 char buf3[80];
 char buf4[80];
 int fd1, fd2, fd3, fd4;
 int select1, select2;
 int n,pid;

 /* Pipe Creation */
 if (access(pipename1, F_OK) == -1) {
  fd1 = mkfifo(pipename1, 0700);
  if (fd1 != 0) {
   printf("Pipe creation error\n");
   exit(1);
  }
 }
 if (access(pipename2, F_OK) == -1) {
  fd2 = mkfifo(pipename2, 0700);
  if (fd2 != 0) {
   printf("Pipe creation error\n");
   exit(1);
  }
 }
 if (access(pipename3, F_OK) == -1) {
  fd3 = mkfifo(pipename3, 0700);
  if (fd3 != 0) {
   printf("Pipe creation error\n");
   exit(1);
  }
 }
  if (access(pipename4, F_OK) == -1) {
  fd4 = mkfifo(pipename4, 0700);
  if (fd4 != 0) {
   printf("Pipe creation error\n");
   exit(1);
  }
 }

 pid = fork();
 if (pid < 0) {
 printf("Fork failed\n");
 exit(1);
 } else if (pid == 0) {

  printf("1. Send individual message\n");
  printf("2. Send group message\n");
  printf("Please select an option: ");
  scanf("%d", &select1);

  switch(select1) {  
   case 1: 
    printf("1. Receiver 1 (Mary)\n");
    printf("2. Receiver 2 (John)\n");
    printf("3. Receiver 3 (Peter)\n");
    printf("Please select a receiver: ");
    scanf("%d", &select2);
    switch(select2) {
      case 1:
      /* Open pipe for writing */
      if ((fd1 = open(pipename1, O_WRONLY)) < 0) {
        printf("Pipe open error\n");
        exit(1);
       }
      while (1) {
       printf("Send message to Mary: \n");
       n = read(STDIN_FILENO,buf1,80);
       if (n <= 0) break;
       buf1[--n] = 0;
       printf("Sending message [%s] to Mary\n",buf1);
       write(fd1,buf1,n);
      }
      close(fd1);
     break;
     case 2:
      /* Open pipe for writing */
      if ((fd2 = open(pipename2, O_WRONLY)) < 0) {
        printf("Pipe open error\n");
        exit(1);
       }
      while (1) {
       printf("Send message to John: \n");
       n = read(STDIN_FILENO,buf2,80);
       if (n <= 0) break;
       buf2[--n] = 0;
       printf("Sending message [%s] to John\n",buf2);
       write(fd2,buf2,n);
      }
     break;
     case 3:
      /* Open pipe for writing */
      if ((fd3 = open(pipename3, O_WRONLY)) < 0) {
        printf("Pipe open error\n");
        exit(1);
       }
      while (1) {
       printf("Send message to Peter: \n");
       n = read(STDIN_FILENO,buf3,80);
       if (n <= 0) break;
       buf3[--n] = 0;
       printf("Sending message [%s] to Peter\n",buf3);
       write(fd3,buf3,n);
      }
     break;
     default:
      printf("Receiver not found\n");
     break;
   }
   case 2:
   /* Open pipe for writing */
     if ((fd4 = open(pipename4, O_WRONLY)) < 0) {
      printf("Pipe open error\n");
      exit(1);
     }
     while (1) {
      printf("Send message to Group: \n");
      n = read(STDIN_FILENO,buf4,80);
      if (n <= 0) break;
      buf4[--n] = 0;
      printf("Sending message [%s] to Group\n",buf4);
      write(fd4,buf4,n);
     }
   break;
   default:
    printf("Wrong Input!\n");
   break;
  }
 } else {
  wait(NULL);
 }
 unlink(pipename1);
 unlink(pipename2);
 unlink(pipename3);
 unlink(pipename4);
 exit(0);
}

Receiver1 program:

/* Receiver1 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
 char pipename1[] = "/tmp/pipe1";
 char pipename4[] = "/tmp/pipe4";
 char buf1[80];
 char buf4[80];
 int fd1, fd4;
 int n, pid;

 printf("Mary is online\n");

 pid = fork();
 if (pid < 0) {
 printf("Fork failed\n");
 exit(1);
 } else if (pid == 0) {
  /* Open pipe for reading */
  if ((fd1 = open(pipename1, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd1, buf1, 80)) > 0) {
   buf1[n] = 0;
   printf("[Message received:] %s\n", buf1, n);
  }
  close(fd1);
  exit(0);
 } else {
  /* Open pipe for reading */
  if ((fd4 = open(pipename4, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd4, buf4, 80)) > 0) {
   buf4[n] = 0;
   printf("[Message received:] %s\n", buf4, n);
  }
  close(fd4);
  wait(NULL);
  exit(0);
 }
}

Receiver2 program:

/* Receiver2 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
 char pipename2[] = "/tmp/pipe2";
 char pipename4[] = "/tmp/pipe4";
 char buf2[80];
 char buf4[80];
 int fd2, fd4;
 int n, pid;

 printf("John is online\n");

 pid = fork();
 if (pid < 0) {
 printf("Fork failed\n");
 exit(1);
 } else if (pid == 0) {
  /* Open pipe for reading */
  if ((fd2 = open(pipename2, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd2, buf2, 80)) > 0) {
   buf2[n] = 0;
   printf("[Message received:] %s\n", buf2, n);
  }
  close(fd2);
  exit(0);
 } else {
  /* Open pipe for reading */
  if ((fd4 = open(pipename4, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd4, buf4, 80)) > 0) {
   buf4[n] = 0;
   printf("[Message received:] %s\n", buf4, n);
  }
  close(fd4);
  wait(NULL);
  exit(0);
 }
}

Receiver3 program:

/* Receiver3 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
 char pipename3[] = "/tmp/pipe3";
 char pipename4[] = "/tmp/pipe4";
 char buf3[80];
 char buf4[80];
 int fd3, fd4;
 int n, pid;

 printf("Peter is online\n");

 pid = fork();
 if (pid < 0) {
 printf("Fork failed\n");
 exit(1);
 } else if (pid == 0) {
  /* Open pipe for reading */
  if ((fd3 = open(pipename3, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd3, buf3, 80)) > 0) {
   buf3[n] = 0;
   printf("[Message received:] %s\n", buf3, n);
  }
  close(fd3);
  exit(0);
 } else {
  /* Open pipe for reading */
  if ((fd6 = open(pipename4, O_RDONLY)) < 0) {
  printf("Pipe open error\n");
  exit(1);
  } 
  while ((n = read(fd4, buf4, 80)) > 0) {
   buf4[n] = 0;
   printf("[Message received:] %s\n", buf4, n);
  }
  close(fd4);
  wait(NULL);
  exit(0);
 }
}
Jeff Son
  • 15
  • 2
  • I don't think you can do that with pipes. See http://stackoverflow.com/questions/1634580/named-pipes-fifos-on-unix-with-multiple-readers – JJF Nov 09 '15 at 17:03
  • the closest you can expect to get is to implement three calls to `write()` with the same data, one call to each pipe. Writes to a pipe are (almost) instant as the output is buffered and will only actually only be input by the receivers when they perform a read(), which could be an extended amount of time later – user3629249 Nov 10 '15 at 00:20
  • How can do this? Can you amend my code for this? Many Thanks – Jeff Son Nov 10 '15 at 01:50

0 Answers0