为什么不只在写入文件时显示此消息(使用 poll C Linux 函数)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12444679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:22:06  来源:igfitidea点击:

Why is this message not only displayed when a file is written to (using the poll C Linux function)?

clinuxpolling

提问by duslabo

I was reading about poll in C programming and built an application given on the poll(2) man page.

我正在阅读有关 C 编程中的 poll 并构建了poll(2) 手册页中给出的应用程序。

Here is the example:

这是示例:

#include<stdio.h>
#include <stropts.h>
#include <poll.h>
#include <fcntl.h>

int main() {

    struct pollfd fds[2];
    int timeout_msecs = -1;
    int ret;
    int i;

    /* Open STREAMS device. */
    fds[0].fd = open("/home/jeshwanth/mywork/poll/dev0", O_RDONLY);
    fds[1].fd = open("/home/jeshwanth/mywork/poll/dev1", O_RDONLY);
    fds[0].events = POLLOUT | POLLWRBAND;
    fds[1].events = POLLOUT | POLLWRBAND;

    while (1) {
        ret = poll(fds, 2, timeout_msecs);

        if (ret > 0) {
            /* An event on one of the fds has occurred. */
            for (i = 0; i < 2; i++) {
                if (fds[i].revents != 0) {
                    /* Priority data may be written on device number i. */
                    printf(
                            "Priority Data may be written on device number %d POLLWRBAND\n",
                            i);

                }
                if (fds[i].revents = !0) {
                    /* Data may be written on device number i. */
                    printf("Data may be written on device number %d POLLOUT\n",
                            i);
                }
                if (fds[i].revents = !0) {
                    /* A hangup has occurred on device number i. */
                    printf("A hangup has occurred on device number %d\n", i);

                }
            }
        }
    }
    return 0;
}

Note: dev0 and dev1 are normal files. When I run the program, if no event occurred in dev0 and dev1, the message is displayed. But I was expecting when some write into the file happens, only then should it display the message. Am I wrong?

注意:dev0 和 dev1 是普通文件。当我运行程序时,如果 dev0 和 dev1 中没有发生任何事件,则会显示该消息。但是我期待当一些写入文件发生时,它才应该显示消息。我错了吗?

采纳答案by ninjalj

Polling it for output readiness doesn't mean you will get notified when some output occurs: it means that you'll get notified when there is output buffer space available so you can output (but you should still check the return value of your output function. The buffer state may have changed between polling and outputting; always check return values).

轮询它是否准备好输出并不意味着您会在发生某些输出时收到通知:这意味着您会在有可用的输出缓冲区空间时收到通知以便您可以输出(但您仍然应该检查输出函数的返回值. 缓冲区状态可能在轮询和输出之间发生了变化;始终检查返回值)。

回答by oldrinb

I'll give you a hint on how to correct it. reventsis interpreted as several bit flags.

我会给你一个关于如何纠正它的提示。revents被解释为几个位标志。

/* check for priority write readiness */
if (fds[i].revents & POLLWRBAND) {
  printf("Priority Data may be written on device number %d POLLWRBAND\n", i);
}

/* check for write readiness */
if (fds[i].revents & POLLOUT) {
  printf("Data may be written on device number %d POLLOUT\n", i);
}

/* check for hang-up */
if (fds[i].revents & POLLHUP) {
  printf("A hangup has occurred on device number %d\n", i);
}