为什么不仅在写入文件时(使用poll C Linux函数)显示此消息? [英] Why is this message not only displayed when a file is written to (using the poll C Linux function)?

查看:151
本文介绍了为什么不仅在写入文件时(使用poll C Linux函数)显示此消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关C编程中的轮询的信息,并构建了一个在投票(2)上给出的应用程序手册页.

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

这里是示例:

#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;
}

注意:dev0和dev1是普通文件.当我运行程序时,如果dev0和dev1中未发生任何事件,则会显示该消息.但是我期望当对文件进行一些写操作时,才应该显示该消息.我错了吗?

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?

推荐答案

为输出准备就绪对其进行轮询并不意味着您会在发生某些输出时得到通知:这意味着您将在有可用输出缓冲区空间时得到通知.因此可以进行输出(但是您仍然应该检查输出函数的返回值.在轮询和输出之间缓冲区状态可能已更改;请始终检查返回值).

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).

这篇关于为什么不仅在写入文件时(使用poll C Linux函数)显示此消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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