Read on closed named pipe blocks

谁都会走 提交于 2019-12-02 05:11:33

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.

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!