A poll() example

来源:互联网 发布:成都魔方软件 编辑:程序博客网 时间:2024/05/16 16:10

这是一个简单的poll函数的例子。  用来检测/dev/scullpipe0设备是否可读。

 

从console中可以看出,当调用了poll后,.poll函数被调用到了。

 

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

int main()
{
    struct pollfd  fda;
    int retval;
    fda.fd = open("/dev/scullpipe0", O_RDWR);
    if (fda.fd < 0)
    {
        perror("open()");
        return -1;
    }

    fda.events = POLLIN | POLLRDNORM  ;

    retval = poll(&fda, 1, 5000*10);
    if ( retval == 0)
        printf("timeout return/n");
    else
    {
        if (fda.revents & POLLRDNORM)
            printf("ready to read/n");
        if (fda.revents & POLLWRNORM)
            printf("ready to write/n");
    }
}

原创粉丝点击