加入收藏 | 设为首页 | 会员中心 | 我要投稿 拼字网 - 核心网 (https://www.hexinwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Handling of asynchronous events---reference

发布时间:2021-01-27 07:29:08 所属栏目:Linux 来源:网络整理
导读:http://www.win.tue.nl/~aeb/linux/lk/lk-12.html 12.?Handling of asynchronous events One wants to be notified of various events,like data that has become available,files that have changed,and signals that have been raised. FreeBSD has the ni

There are many problems with this interface. It can only watch directories. If one wants to watch many directories,it takes many file descriptors. Moreover,the open file pins the filesystem so that it cannot be unmounted. When something happens it is unknown what,and a?stat()?on all files of interest is needed. The communication mechanism,signals,is unfortunate. Dnotify is obsolete now.

12.7?inotify

(Since 2.6.13.) Inotify is implemented using three new system calls and the usual?read(),?poll(),?close()?calls:

int inotify_init(void);
int inotify_add_watch (int fd,const char *pathname,int mask);
int inotify_rm_watch (int fd,int wd);

The first returns a file descriptor:?fd = inotify_init(). The second tells what to watch,and what to watch for,and returns a watch descriptor:?wd = inotify_add_watch(fd,"/home/aeb",IN_CREATE | IN_DELETE). The file descriptor?fd?can be used in a?read()?call,and then returns an array of struct inotify_event's. One can use?select()?and?poll()on it. A watch is removed by?inotify_rm_watch(fd,wd). The inotify instance is closed by?close(fd).

An inotify_event is defined by

struct inotify_event {
        int      wd;       /* Watch descriptor */
        uint32_t mask;     /* Mask of events */
        uint32_t cookie;   /* Unique cookie associating related
                              events (for rename(2)) */
        uint32_t len;      /* Size of 'name' field */
        char     name[];   /* Optional null-terminated name */
};

The?name?field defines the file involved,when one is watching a directory.

There is a?/proc?interface with settable limits:

% ls /proc/sys/fs/inotify/
max_queued_events  max_user_instances  max_user_watches
% cat $_/*
16384
128
8192

Applications are?,??and?.

/* inotify demo,mimicking the above dnotify one */
#include 
#include 
#include 
#include 
#include 

define BUFSZ 16384

static void errexit(char *s) {
fprintf(stderr,"%sn",s);
exit(1);
}

int main(void) {
int ifd,wd,i,n;
char buf[BUFSZ];

    ifd = inotify_init();
    if (ifd < 0)
            errexit("cannot obtain an inotify instance");

    wd = inotify_add_watch(ifd,".",IN_MODIFY|IN_CREATE|IN_DELETE);
    if (wd < 0)
            errexit("cannot add inotify watch");

    while (1) {
            n = read(ifd,buf,sizeof(buf));
            if (n <= 0)
                    errexit("read problem");

            i = 0;
            while (i < n) {
                    struct inotify_event *ev;

                    ev = (struct inotify_event *) &amp;buf[i];
                    if (ev->len)
                            printf("file %s %sn",ev->name,(ev->mask &amp; IN_CREATE) ? "created" :
                                   (ev->mask &amp; IN_DELETE) ? "deleted" :
                                   "modified");
                    else
                            printf("unexpected event - wd=%d mask=%dn",ev->wd,ev->mask);

                    i += sizeof(struct inotify_event) + ev->len;
            }
            printf("---n");
    }

}

(编辑:拼字网 - 核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!