首页主机资讯Linux下inotify API如何调用

Linux下inotify API如何调用

时间2025-11-27 03:11:03发布访客分类主机资讯浏览710
导读:在Linux下,inotify API允许应用程序监控文件系统事件,如文件的创建、删除、修改等。以下是使用inotify API的基本步骤: 1. 包含必要的头文件 #include <sys/inotify.h> #inclu...

在Linux下,inotify API允许应用程序监控文件系统事件,如文件的创建、删除、修改等。以下是使用inotify API的基本步骤:

1. 包含必要的头文件

#include <
    sys/inotify.h>
    
#include <
    unistd.h>
    
#include <
    stdio.h>
    
#include <
    stdlib.h>
    
#include <
    string.h>
    

2. 创建inotify实例

int fd = inotify_init();
    
if (fd <
 0) {
    
    perror("inotify_init");
    
    exit(EXIT_FAILURE);

}
    

3. 添加监控

使用inotify_add_watch函数来添加对特定文件或目录的监控。该函数返回一个唯一的监视描述符(watch descriptor)。

int wd = inotify_add_watch(fd, "/path/to/directory", IN_MODIFY | IN_CREATE | IN_DELETE);
    
if (wd <
 0) {
    
    perror("inotify_add_watch");
    
    close(fd);
    
    exit(EXIT_FAILURE);

}
    
  • IN_MODIFY: 文件被修改时触发。
  • IN_CREATE: 文件或目录被创建时触发。
  • IN_DELETE: 文件或目录被删除时触发。

4. 读取事件

使用read函数从inotify文件描述符中读取事件。

char buffer[4096];
    
ssize_t length = read(fd, buffer, sizeof(buffer));
    
if (length <
 0) {
    
    perror("read");
    
    close(fd);
    
    exit(EXIT_FAILURE);

}
    

5. 解析事件

解析读取到的缓冲区,提取事件信息。

int i = 0;
    
while (i <
 length) {
    
    struct inotify_event *event = (struct inotify_event *) &
    buffer[i];
    
    if (event->
len) {
    
        if (event->
    mask &
 IN_CREATE) {
    
            printf("File %s was created.\n", event->
    name);

        }
    
        if (event->
    mask &
 IN_DELETE) {
    
            printf("File %s was deleted.\n", event->
    name);

        }
    
        if (event->
    mask &
 IN_MODIFY) {
    
            printf("File %s was modified.\n", event->
    name);

        }

    }
    
    i += sizeof(struct inotify_event) + event->
    len;

}
    

6. 移除监控并关闭inotify实例

inotify_rm_watch(fd, wd);
    
close(fd);
    

完整示例代码

#include <
    sys/inotify.h>
    
#include <
    unistd.h>
    
#include <
    stdio.h>
    
#include <
    stdlib.h>
    
#include <
    string.h>


int main() {
    
    int fd = inotify_init();
    
    if (fd <
 0) {
    
        perror("inotify_init");
    
        exit(EXIT_FAILURE);

    }
    

    int wd = inotify_add_watch(fd, "/path/to/directory", IN_MODIFY | IN_CREATE | IN_DELETE);
    
    if (wd <
 0) {
    
        perror("inotify_add_watch");
    
        close(fd);
    
        exit(EXIT_FAILURE);

    }
    

    char buffer[4096];
    
    ssize_t length = read(fd, buffer, sizeof(buffer));
    
    if (length <
 0) {
    
        perror("read");
    
        close(fd);
    
        exit(EXIT_FAILURE);

    }
    

    int i = 0;
    
    while (i <
 length) {
    
        struct inotify_event *event = (struct inotify_event *) &
    buffer[i];
    
        if (event->
len) {
    
            if (event->
    mask &
 IN_CREATE) {
    
                printf("File %s was created.\n", event->
    name);

            }
    
            if (event->
    mask &
 IN_DELETE) {
    
                printf("File %s was deleted.\n", event->
    name);

            }
    
            if (event->
    mask &
 IN_MODIFY) {
    
                printf("File %s was modified.\n", event->
    name);

            }

        }
    
        i += sizeof(struct inotify_event) + event->
    len;

    }
    

    inotify_rm_watch(fd, wd);
    
    close(fd);
    

    return 0;

}
    

注意事项

  • inotify API有监控数量的限制,可以通过/proc/sys/fs/inotify/max_user_watches文件查看和修改。
  • 监控大量文件或目录时,可能需要优化事件处理逻辑,避免阻塞。

通过以上步骤,你可以在Linux下使用inotify API来监控文件系统的变化。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Linux下inotify API如何调用
本文地址: https://pptw.com/jishu/757461.html
inotify在实时监控中的应用 Linux环境下如何优化Node.js的文件读写操作

游客 回复需填写必要信息