akela43

fifo

Mar 20th, 2023
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | Help | 0 0
  1. #include <iostream>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. const char * FIFO_NAME_IN = "/home/box/in.fifo";
  7. const char * FIFO_NAME_OUT = "/home/box/out.fifo";
  8. const int MAX_BUF = 80;
  9.  
  10. int main()
  11. {
  12.     int readfd, writefd, n;
  13.     char buf[MAX_BUF];
  14.  
  15.     mkfifo(FIFO_NAME_IN, 0666);
  16.     mkfifo(FIFO_NAME_OUT, 0666);
  17.  
  18.     if ((readfd = open(FIFO_NAME_IN, O_RDONLY)) < 0)
  19.     {
  20.         std::cout<<"Cannot open FIFO_IN\n";
  21.         return 0;
  22.     }
  23.     if ((writefd = open(FIFO_NAME_OUT, O_WRONLY)) < 0)
  24.     {
  25.         std::cout<<"Cannot open FIFO_out\n";
  26.         return 0;
  27.         }
  28.  
  29.     while ( (n = read(readfd, buf, MAX_BUF)) > 0)
  30.     {
  31.         if (write(writefd, buf, n) != n)
  32.         {
  33.             std::cout<<"Output error\n";
  34.             return 0;
  35.         }
  36.  
  37.  
  38.     }
  39.     close(readfd);
  40.     close(writefd);
  41.  
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment