Q:

Read on closed named pipe blocks

I am trying to read from a named pipe (FIFO) with Fortran.  Reading the data works, but the Fortran program does not seem to notice when the pipe is closed on the other end; reads simply block rather than getting an EOF.
Sample program:
program kitten
  character(256) :: buf

  open(22, file='test')
  do
     read(22, *) buf
     print*, trim(buf)
  end do
end program kitten

Now with
$ mkfifo test
$ echo -e '1\n2\n3' >test &
$ ./kitten

the program prints 1\n2\n3\n as expected, but then simply hangs.
Instead, the program returns an error on EOF if 

test is a regular file; or
you change kitten to read from STDIN and do ./kitten <test or
echo -e '1\n2\n3' | ./kitten; or
you write an equivalent kitten program in C.

I tested this with ifort 15.0.1 and gfortran 4.9.2, with the same results.
For C I used gcc and
#include <stdio.h>

main() {
        char buf[256];
        FILE *test;

        test = fopen("test", "r");

        while(fgets(buf, 256, test)) {
                printf(buf);
        }
}

A:

I don't know much about fortran, but I do know you could reproduce the hanging behavior in C by using a read/write mode in your open (for example fopen("test", "r+")
The pipe doesn't get an EOF until the number of writable file descriptors on it drops to 0. When your read file descriptor is also writable, you never get EOF.
So my guess is that fortran opens in read/write mode by default, and you need to tell it not to do that. This question about a fortran readonly flag may help.

