阅读关闭命名管道块 [英] Read on closed named pipe blocks

查看:391
本文介绍了阅读关闭命名管道块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个命名管道(FIFO)与Fortran语言阅读。读取数据的作品,但是Fortran程序似乎并不当管在另一端封闭,以通知;简单地读取块,而不是得到一个EOF。

样例程序:

 程序小猫
  字符(256):: buf中  开(22,文件=测试)
  做
     阅读(22 *)BUF
     打印*,修剪(BUF)
  做到底
最终方案的小猫

现在以

  $ mkfifo子测试
$回声-e'1 \\ N2 \\ N3'>测试和放大器;
$ ./kitten

程序打印 1 \\ N2 \\ N3 \\ n 如预期,但后来干脆挂断。

相反,程序如果

返回上EOF错误

  1. 测试是一个普通文件;或

  2. 您更改小猫从标准输入读取并执行 ./小猫<试验

  3. 回声-e'1 \\ N2 \\ N3| ./kitten ;或

  4. 您用C等效小猫程序。

我用测试这ifort 15.0.1 gfortran 4.9.2 ,具有相同的结果。

对于C我用 GCC

 的#include<&stdio.h中GT;主(){
        焦炭BUF [256];
        FILE *测试;        测试= FOPEN(测试,R);        而(与fgets(buf中,256,测试)){
                的printf(BUF);
        }
}


解决方案

我不很了解FORTRAN,但我知道,你可以在打开的使用读/写模式重现挂行为C(为例如的fopen(测试,R +)

直到写文件描述符的用户的数量下降到0。当你读的文件描述符也可写的钢管没有得到一个EOF,你永远不会得到EOF。

所以我的猜测是,在FORTRAN读/写模式在默认情况下打开,你需要告诉它不要那样做。 有关FORTRAN只读标志可能有助于这个问题。

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

  1. test is a regular file; or
  2. you change kitten to read from STDIN and do ./kitten <test or
  3. echo -e '1\n2\n3' | ./kitten; or
  4. 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);
        }
}

解决方案

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.

这篇关于阅读关闭命名管道块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆