linux

如何利用inotify进行进程间通信

小樊
49
2025-07-25 17:24:09
栏目: 编程语言

inotify 是 Linux 内核提供的一种文件系统事件监控机制,可以用来监控文件或目录的变化,如创建、删除、修改等。虽然 inotify 本身不是进程间通信(IPC)机制,但可以通过监控共享文件或目录的变化来实现进程间的通信。

以下是利用 inotify 进行进程间通信的基本步骤:

1. 初始化 inotify 实例

首先,需要包含必要的头文件并初始化 inotify 实例。

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

int main() {
    int fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
        exit(EXIT_FAILURE);
    }
    // 继续下一步...
}

2. 添加监控

接下来,需要添加要监控的文件或目录。

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

3. 读取事件

然后,通过读取 inotify 文件描述符来获取事件。

char buffer[4096];
while (1) {
    ssize_t length = read(fd, buffer, sizeof(buffer));
    if (length < 0) {
        perror("read");
        break;
    }

    int i = 0;
    while (i < length) {
        struct inotify_event *event = (struct inotify_event *) &buffer[i];
        if (event->len) {
            if (event->mask & IN_MODIFY) {
                printf("File %s was modified\n", event->name);
            }
            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);
            }
        }
        i += sizeof(struct inotify_event) + event->len;
    }
}

4. 处理事件

根据读取到的事件类型,执行相应的操作。例如,可以发送信号、写入共享内存或通过管道通知其他进程。

5. 清理资源

最后,记得清理资源。

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

示例:使用信号进行进程间通信

假设我们有两个进程:一个监控进程和一个工作进程。监控进程使用 inotify 监控文件变化,并通过信号通知工作进程。

监控进程

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

volatile sig_atomic_t stop = 0;

void signal_handler(int signum) {
    stop = 1;
}

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

    int wd = inotify_add_watch(fd, "/path/to/watch", IN_MODIFY);
    if (wd < 0) {
        perror("inotify_add_watch");
        close(fd);
        exit(EXIT_FAILURE);
    }

    struct sigaction sa;
    sa.sa_handler = signal_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGUSR1, &sa, NULL);

    while (!stop) {
        char buffer[4096];
        ssize_t length = read(fd, buffer, sizeof(buffer));
        if (length < 0) {
            perror("read");
            break;
        }

        int i = 0;
        while (i < length) {
            struct inotify_event *event = (struct inotify_event *) &buffer[i];
            if (event->len && event->mask & IN_MODIFY) {
                printf("File modified, sending signal to worker process\n");
                kill(getpid(), SIGUSR1);
            }
            i += sizeof(struct inotify_event) + event->len;
        }
    }

    inotify_rm_watch(fd, wd);
    close(fd);
    return 0;
}

工作进程

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

volatile sig_atomic_t stop = 0;

void signal_handler(int signum) {
    stop = 1;
    printf("Worker process received signal, stopping...\n");
}

int main() {
    struct sigaction sa;
    sa.sa_handler = signal_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGUSR1, &sa, NULL);

    printf("Worker process is running...\n");
    while (!stop) {
        sleep(1);
    }

    printf("Worker process stopped.\n");
    return 0;
}

在这个示例中,监控进程监控文件变化并通过 SIGUSR1 信号通知工作进程。工作进程接收到信号后停止运行。

通过这种方式,可以利用 inotify 实现进程间的简单通信。

0
看了该问题的人还看了