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


i want make 2 processes communicate each other via 2 named pipes on linux. each process unix filter : reads data on standard input , writes data on standard output. circularly linked in output of first input of second , other way around.

here code of first filter (a.c) :

#include <stdio.h>  int main( void  ){   file* ferr = fopen( "/dev/stderr", "w" );   double d;    fprintf(ferr,"a going write\n");   printf("%lf\n",1.);   fprintf(ferr,"a wrote %lf\n",1.);    while( 1 ){     fprintf(ferr,"a going read\n");     if( scanf("%lf",&d) == eof ){       break;     }     fprintf(ferr,"a recieved : %lf\n",d);     d += 1;     fprintf(ferr,"a going write\n");     printf("%lf\n",d);     fprintf(ferr,"a wrote %lf\n",d);   }   return 0; } 

here code of second filter (b.c) :

#include <stdio.h>  int main( void  ){   file* ferr = fopen( "/dev/stderr", "w" );   double d;    while( 1 ){     fprintf(ferr,"b going read\n");     if( scanf("%lf",&d) == eof ){       break;     }     fprintf(ferr,"b recieved : %lf\n",d);     d += 1;     fprintf(ferr,"b going write\n");     printf("%lf\n",d);     fprintf(ferr,"b wrote %lf\n",d);   }   return 0; } 

i compile (gcc -o a.c && gcc -o b b.c), create 2 fifos (mkfifo b2a ; mkfifo a2b), run first program in terminal (cat a2b | ./b > b2a), open new terminal , run second program (cat b2a | ./a > a2b).

what expected infinite loop , b increasing number in turn, b stuck, not being able read wrote.

in term launched b, :

b going read 

in terminal launched a, :

a going write wrote 1.000000 going read 

if 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       24414 john doe    1w  fifo   0,22      0t0 282149936 a2b 

why not expected ?

thanks in advance.

you need explicitly fflush after writing, output goes through pipe. otherwise, output may stay in writing process' stdio buffers. (you can turn off buffering setvbuf(stdio, null, _ionbf, 0).)


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -