开发者

Two filters circularly linked by two named pipes (FIFO) on Linux

I want to make two processes communicate with each other via two named pipes on Linux开发者_运维知识库. Each process is a Unix filter : it reads data on its standard input and writes data on its standard output. They are circularly linked in that the output of the first is the input of the second and the other way around.

Here is the code of the first filter (a.c) :

#include <stdio.h>

int main( void  ){
  FILE* ferr = fopen( "/dev/stderr", "w" );
  double d;

  fprintf(ferr,"A is going to write\n");
  printf("%lf\n",1.);
  fprintf(ferr,"A wrote %lf\n",1.);

  while( 1 ){
    fprintf(ferr,"A is going to read\n");
    if( scanf("%lf",&d) == EOF ){
      break;
    }
    fprintf(ferr,"A recieved : %lf\n",d);
    d += 1;
    fprintf(ferr,"A is going to write\n");
    printf("%lf\n",d);
    fprintf(ferr,"A wrote %lf\n",d);
  }
  return 0;
}

Here is the code of the second filter (b.c) :

#include <stdio.h>

int main( void  ){
  FILE* ferr = fopen( "/dev/stderr", "w" );
  double d;

  while( 1 ){
    fprintf(ferr,"B is going to read\n");
    if( scanf("%lf",&d) == EOF ){
      break;
    }
    fprintf(ferr,"B recieved : %lf\n",d);
    d += 1;
    fprintf(ferr,"B is going to write\n");
    printf("%lf\n",d);
    fprintf(ferr,"B wrote %lf\n",d);
  }
  return 0;
}

I compile (gcc -o A a.c && gcc -o B b.c), create two fifos (mkfifo b2a ; mkfifo a2b), run the first program in a terminal (cat a2b | ./B > b2a), open a new terminal and run the second program (cat b2a | ./A > a2b).

What I expected was an infinite loop with A and B increasing the number in turn, but what I get is B stuck, not being able to read what A wrote.

In the term where I launched B, I get :

B is going to read

In the terminal where I launched A, I get :

A is going to write
A wrote 1.000000
A is going to read

If I use lsof :

lsof b2a a2b
COMMAND   PID      USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
cat     24382 john doe    3r  FIFO   0,22      0t0 282149936 a2b
B       24383 john doe    1w  FIFO   0,22      0t0 282149934 b2a
cat     24413 john doe    3r  FIFO   0,22      0t0 282149934 b2a
A       24414 john doe    1w  FIFO   0,22      0t0 282149936 a2b

Why do I not get what I expected ?

Thanks in advance.


You need to explicitly fflush after writing, so that the output goes through the pipe. Otherwise, the output may stay in the writing process' stdio buffers. (You can also turn off buffering with setvbuf(stdio, NULL, _IONBF, 0).)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜