named pipes

Serge D. Mechveliani mechvel at
Wed Jan 18 18:59:04 CET 2012

Dear GHC team,

I am testing the IO operations of GHC with the Unix named pipes
(in  ghc-7.01  under Debian Linux).

In the below example, 
the pipe pair are created by   > mkfifo toA
                               > mkfifo fromA,

`main'  in  Main.hs  opens  toA    for writing,
                     opens  fromA  for reading,
                     outputs  "A1"  to  toA. 

The C program  fifo2  opens  toA    for reading,
                      opens  fromA  for writing,
                      inputs a string from  toA,
                      prints "done" to the screen

(fromA  is not really used, but will be needed in future).

The effect is that   fifo2    (on terminal-2)  hangs silent,  while
                     Main.hs  (on terminal-1)  does its part.

If I remove the   fopen(..fromA..)  

call in  fifo2.c, then it runs as expected
(after this example is fixed,  fifo2  will need to write to  fromA).

Main.hs       is built by   ghc --make Main,
The C program is built by   gcc -o fifo2 fifo2.c

First,  ./fifo2   is run on  terminal-2,
then    ./Main    is run on  terminal-1.

The source is quite small: 

-- Main.hs ---------------------------------------------------------
import System.IO (IOMode(..), IO(..), Handle, openFile, hPutStr, 
dir = showString "/home/mechvel/ghc/notes/npipe/"

toA_IO   = openFile (dir "toA")   WriteMode   :: IO Handle
fromA_IO = openFile (dir "fromA") ReadMode  

axiomIO :: Handle -> String -> IO()
axiomIO h str = do
                hPutStr h str 
                hFlush h
                putStr "hFlush done\n"
main = do
       h <- toA_IO
       putStr "str1  -->  " 
       axiomIO h "A1\n"

----------------------- fifo2.c -------------------------------------
#include <stdio.h>
#include <string.h>  
#define BOUND 64
static  char  str[BOUND];
  int l, i;   FILE *toA, *fromA;

  toA = fopen("/home/mechvel/ghc/notes/npipe/toA", "r");
  if (toA == NULL) {perror("fopen(toA, r)  failed:  ");  return;};   

  fromA = fopen("/home/mechvel/ghc/notes/npipe/fromA", "w");   // !

  if (fgets(str, BOUND, toA) == NULL) { 
     perror("fgets(str, bound, toA)  failed:  "); return;
  printf("input = %s\n", str);

Can you, please, explain the effect?

mechvel at

More information about the Glasgow-haskell-users mailing list