Read on closed named pipe blocks

前端 未结 2 630
终归单人心
终归单人心 2021-01-24 13:24

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; rea

相关标签:
2条回答
  • 2021-01-24 13:43

    regarding the C program.

    A much better version would be:

    #include <stdio.h>
    #include <stdlib.h>  // exit(), EXIT_FAILURE
    
    int main( void ) // properly declare main()
    {
        char buf[256];
        FILE * test = NULL;
    
        if( NULL == (test = fopen("test", "r") ) ) // check for open error
        { // then fopen failed
            perror( "fopen for test for read failed");
            exit( EXIT_FAILURE );
        }
    
        // implied else, fopen successful
    
        while(fgets(buf, 256, test) ) // exit loop when fgets encounters EOF
        {
            printf("%s\n",buf);
        }
    
        fclose( test ); // cleanup before exiting
        return 0; // properly supply a return value
    }
    
    0 讨论(0)
  • 2021-01-24 13:48

    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.

    0 讨论(0)
提交回复
热议问题