读不阻塞命名管道 [英] read not blocking on named pipe

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

问题描述

我有C code的下位,它从一个管道中读取,然后应该阻止,但它永远不会阻止

  INT pipe_fd;
中期业绩;
INT open_mode = O_RDONLY;
焦炭BUF [100];
INT bytes_read缓存= 0;memset的(BUF,'\\ 0',sizeof的(BUF));
pipe_fd =开(FIFO_NAME,open_mode);如果(接入(FIFO_NAME,F_OK)== -1)
{
    RES = mkfifo子(FIFO_NAME,0777);
    如果(RES!= 0)
    {
            fprintf中(标准错误,无法创建FIFO%S \\ n,FIFO_NAME);
            出口(EXIT_FAILURE);
    }
}对于(;;)
{
    做
    {
        RES =读(pipe_fd,BUF,sizeof的(BUF));
        bytes_read缓存+ =资源;
    }而(RES大于0);    //处理数据,然后回去和块
    ............
}

这是由一些code发送一个简单的缓冲在bash脚本这样的'./test 1

 #!/斌/庆典管=的/ tmp /管如果[! -p $管]];然后
    回声读者不要跑
    1号出口
科幻如果[$ 1]];然后
     回声一些字符串> $管
其他
     回声Q> $管
科幻

我运行在GDB中C code程序,最初它并阻止对读操作,但只要我叫bash脚本的C code不再块,但它成功地读取从数据
缓冲区,然后每次读取有0字节读取所以不知道为什么它不再阻塞。的一些字串'数据被在另一侧正确地接收。

我只需要坐在那里等待数据处理,然后回去等待更多


解决方案

  

我运行在GDB中C code程序,最初它并阻止对读操作,但只要我叫bash脚本的C code不再块,但它成功地从缓冲区中读取数据然后每次读取有0字节读取所以不知道为什么它不再阻塞。的一些字串'数据被在另一侧正确地接收。


0 表示EOF。 FIFO可以读取或写入,只有当有连接到它的读取和写入过程。当没有更多的作家(您的shell脚本终止)的读者都是通过通知有关阅读()返回EOF。

FIFO的行为是那样要与壳管逻辑兼容例如:

  $ mkfifo子./tmp1
$猫<输入> ./tmp1&安培;
$猫< ./tmp1>的/ dev / null的

如果阅读()将不会返回EOF,第二个将永远阻塞。


  

我只需要坐在那里等待数据处理,然后回去等待更多


在你的C程序,你必须重新的open()的FIFO后阅读()第一返回EOF时间。

P.S。为您找到相当不错的FIFO总结。查看第二页上的表。

i have the following bit of C code which reads from a pipe and then should block but it never blocks

int pipe_fd;
int res;
int open_mode = O_RDONLY;
char buf[100];
int bytes_read = 0;

memset (buf, '\0', sizeof(buf));
pipe_fd = open(FIFO_NAME, open_mode);

if (access(FIFO_NAME, F_OK) == -1)
{
    res = mkfifo(FIFO_NAME, 0777);
    if (res != 0)
    {
            fprintf (stderr, "Could not create fifo %s\n", FIFO_NAME);  
            exit (EXIT_FAILURE);
    }
}

for(;;)
{        
    do     
    {     
        res = read(pipe_fd, buf, sizeof(buf));
        bytes_read += res;
    }while (res > 0);

    // process data then go back and block
    ............
}

It is sent a simple buffer by some code in a bash script like this './test 1'

#!/bin/bash

pipe=/tmp/pipe

if [[ ! -p $pipe ]]; then
    echo "Reader not running"
    exit 1
fi

if [[ "$1" ]]; then
     echo "some string" >$pipe
else
     echo "q" >$pipe
fi

I run the C code program in gdb and initially it does block on the read but as soon as i call the bash script the C code no longer blocks , it does successfully read the data from the buffer and then each time it reads there are 0 bytes read so not sure why its no longer blocking. The 'some string' data is correctly received at the other side.

I just need it to sit there waiting for data process it and then go back and wait for more

解决方案

I run the C code program in gdb and initially it does block on the read but as soon as i call the bash script the C code no longer blocks , it does successfully read the data from the buffer and then each time it reads there are 0 bytes read so not sure why its no longer blocking. The 'some string' data is correctly received at the other side.

0 means EOF. FIFO can be read or written only when there are processes connected to it for both reading and writing. When there are no more writers (your shell scripts terminated) readers are notified about that through read() returning the EOF.

FIFOs behave that way to be compatible with shell pipe logic e.g.:

$ mkfifo ./tmp1
$ cat < input > ./tmp1 &
$ cat < ./tmp1 > /dev/null

If read() will not return EOF, the second cat would block forever.

I just need it to sit there waiting for data process it and then go back and wait for more

In your C program you have to reopen() the FIFO after read() returned the EOF first time.

P.S. Found quite nice FIFO summary for your. Check the table on the second page.

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

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