Bidirectional FIFO

Posted by nunos on Stack Overflow See other posts from Stack Overflow or by nunos
Published on 2010-04-27T15:00:30Z Indexed on 2010/04/27 15:03 UTC
Read the original article Hit count: 376

Filed under:
|

I would like to implement a bidirectional fifo. The code below is functioning but it is not using bidirectional fifo. I have searched all over the internet, but haven't found any good example...

How can I do that?

Thanks,

WRITER.c:

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/wait.h>
#include <fcntl.h>



#define MAXLINE 4096

#define READ 0

#define WRITE 1


int main (int argc, char** argv)
{
 int a, b, fd;

 do {
   fd=open("/tmp/myfifo",O_WRONLY);
   if (fd==-1) sleep(1);
  } while (fd==-1);

  while (1) {
   scanf("%d", &a);
   scanf("%d", &b);

   write(fd,&a,sizeof(int));
   write(fd,&b,sizeof(int));

   if (a == 0 && b == 0)
   {
    break;
   }

  }

  close(fd);
  return 0;
}

READER.c:

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>




#define MAXLINE 4096

#define READ 0

#define WRITE 1



int main(void)

{
  int n1, n2;
  int fd;

  mkfifo("/tmp/myfifo",0660);
  fd=open("/tmp/myfifo",O_RDONLY);

  while(read(fd, &n1, sizeof(int) ))
  {
 read(fd, &n2, sizeof(int));

 if (n1 == 0 && n2 == 0)
 {
  break;
 }

 printf("soma: %d\n",n1+n2);

  printf("diferenca: %d\n", n1-n2);

   printf("divisao: %f\n", n1/(double)n2);

   printf("multiplicacao: %d\n", n1*n2); 
  }

  close(fd);

  return 0;
}

© Stack Overflow or respective owner

Related posts about fifo

Related posts about c