使用poll()检查文件修改

一个文件,我想用poll()检查内容是否已更改.

在Raspi上有一个名为gpio value的文件,如果值改变了poll()是用POLLPRI触发的,我想对普通文件做同样的事情.

这是我用来检查GPIO文件代码

int gpio_fd = gpio_fd_open();
int timeout = POLL_TIMEOUT;
struct pollfd fdset;
int nfds = 1;
char *buf[MAX_BUF];
int len;
int rc;
fdset.fd = gpio_fd;
fdset.events =  POLLPRI | POLLERR | POLLHUP | POLLNVAL; // POLLIN | | POLLOUT 
unsigned int c1,c2,c3;
do{
    rc = poll(&fdset,1,timeout);

和gpio_fd_open函数

int gpio_fd_open() {
    printf("opening File: " SYSFS_GPIO_DIR "\n");

    int fd,len;
    char buf[MAX_BUF];

    len = snprintf(buf,sizeof (buf),SYSFS_GPIO_DIR);
    fd = open(buf,O_RDONLY | O_NONBLOCK);
    if (fd < 0) {
        perror("gpio/fd_open");
    }
    return fd;
}
最佳答案
在Linux中,与您的特殊GPIO文件不同,您无法将fd轮询到这样的开放普通文件.

要查看文件以进行更改,您可以使用inotify系列函数.您可以方便地使用文件描述符,该文件描述符可以与您的GPIO文件描述符一起传递给poll(),因此您可以同时查看两者.

因此,对代码进行一些小的添加/更改:

int fw_fd = file_watch_fd("/some/file/to/watch");
struct pollfd fdset[2];
int nfds = 2;
int rc;
fdset[0].fd = gpio_fd;
fdset[0].events =  POLLPRI | POLLERR | POLLHUP | POLLNVAL;
fdset[1].fd = fw_fd;
fdset[1].events = POLLIN;
do {
    rc = poll(fdset,nfds,timeout);

您正在查看inotify fd,您阅读该fd以返回发生的另一个观察事件.由于此代码仅针对一个事件观看一个文件,因此我们非常确定事件是什么,但我们仍然需要从fd中读取它,然后我们才能真正读取该文件.

    if (fdset[1].revents & POLLIN) {
        if (ready(fw_fd)) {
            /* file has changed,read it */
        }
    }

这是file_watch_fd()函数

int file_watch_fd ( const char * filename ) {
        static int      inot    = ERR;
        static int      iflags  = IN_CLOEXEC | IN_NONBLOCK;
        static uint32_t mask    = IN_MODIFY;
        int             watch;

        if (inot == ERR) {
                inot = inotify_init1(iflags);
                if (inot == ERR) return ERR;
        }
        watch = inotify_add_watch(inot,filename,mask);
        if (watch == ERR) return ERR;
        return inot;
}

这是ready()函数

int ready ( int inot ) {
        uint64_t                buffer[8192];
        ssize_t                 nr;
        char *                  p;
        size_t                  n;
        struct inotify_event *  evP;
        int                     ready   = 0;

        while ((nr = read(inot,(char *)buffer,sizeof(buffer))) > 0) {
                for (p = buffer; p < buffer + nr; p += n) {
                        evP = (struct inotify_event *)p;
                        if (evP->mask & IN_MODIFY) ready = 1;
                        n = sizeof(struct inotify_event) + evP->len;
                }
        }
        return ready;
}

(人们使用inotify的经验会注意到我对它的功能进行粗暴操作以保持这个例子尽可能简单)

相关文章

1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} ...
## 步骤 1:安装必要的软件包 首先,需要确保系统已安装 `dh...