无法使Unix FIFO正常工作吗? [英] Having trouble getting a Unix FIFO to work properly?

查看:79
本文介绍了无法使Unix FIFO正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Linux中编写一个简单的守护程序,该守护程序将创建一个FIFO,然后收集写入FIFO的所有内容,并在以后的时间将该数据写入文件中.

I'm trying to write a simple daemon in Linux, which will create a FIFO, then collect anything written to the FIFO and write that data to a file at a later time.

我的期望是,守护进程一旦创建了FIFO,就可以重复执行"echo text>/myfifo".完成后,我可以执行"echo quit>/myfifo",程序将退出并将所有数据写入磁盘.

My expectations are that once my daemon has created the FIFO, I can do "echo text > /myfifo" repeatedly. When I'm done, I can do "echo quit > /myfifo" and my program will exit and write all data to disk.

我目前正在使用poll()来了解FIFO上何时还有更多数据.直到我第一次将数据回显到FIFO后,此方法才能正常工作.数据回显良好,但之后我的民意测验不断返回SIGHUP.

I'm currently using poll() to know when there's more data on the FIFO. This works fine until after the first time I echo data to the FIFO. The data is echoed fine, but my poll continuously returns SIGHUP after that.

在每个进程写入FIFO后,我是否需要重置(或关闭并重新打开)FIFO?

Do I need to reset (or close & reopen) the FIFO after each process writes to it?

我的代码的伪代码如下:

Pseudo-code of my code looks like this:

ret = fifo(my_fifo, mode);
fd = open(my_fifo, O_RDONLY | O_NONBLOCK);

polling.fd = fd;
polling.events = POLLIN | POLLPRI;

do {
    ret = poll(&polling, 1, -1);
    amt = read(fd, buf, bufsize);
    // do stuff
} while (!done);

我认为

推荐答案

您必须继续重新打开FIFO.我有一个监视FIFO的程序,监视循环为:

You have to keep reopening the FIFO, I think. I have a program that monitors a FIFO, and the monitor loop is:

/* Implement monitor mode */
void sql_monitor(char *fifo)
{
    if (chk_fifo(fifo) != 0)
        cmd_error(E_NOTFIFO, fifo);

    /* Monitor -- device is assumed to be a FIFO */
    while (1)
    {
        ctxt_newcontext();
        if (ctxt_setinput(fifo) != 0)
            sql_exit(1);
        sql_file();
        ctxt_endcontext();
    }
}

ctxt_newcontext()函数隐藏当前的I/O状态; ctxt_setinput()函数将输入文件设置为命名文件-在这种情况下为FIFO. sql_file()函数从文件(FIFO)读取直到结束为止-文件已关闭. ctxt_endcontext()撤消ctxt_newcontext()的作用.重复此过程...此代码自1990年以来一直存在.

The ctxt_newcontext() function stashes the current I/O state; the ctxt_setinput() function sets the input file to the named file - a FIFO in this case. The sql_file() function reads from the file (FIFO) until the end is reached - the file is closed. The ctxt_endcontext() undoes what ctxt_newcontext() does. The process repeats... This code has been around since about 1990.

因此,,您将需要在读取文件末尾之后继续关闭并重新打开FIFO(在每个过程,例如echo完成写入FIFO之后).

So, YES, you will need to keep closing and reopening the FIFO after reading the end of the file (after each process such as echo finishes writing to the FIFO).

(您还应该注意,除非没有数据,否则确实不需要轮询FIFO,除非该进程还有其他事情要做.read()调用将等到有数据后才返回. )

(You should also notice that there really isn't a need to poll the FIFO unless there's something else for the process to be doing when there is no data. The read() call will wait until there is data before returning.)

这篇关于无法使Unix FIFO正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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