使用信号处理linux文件系统监视,(if no inotify)

本文介绍了一种在Linux下使用C语言和Python结合的方式进行文件系统监控的方法。通过C语言设置信号处理函数,配合Python的信号处理和文件系统通知,实现实时监控指定目录的创建、修改等事件。示例代码展示了如何注册信号处理函数,并使用`fcntl`和`signal`库进行操作。
摘要由CSDN通过智能技术生成
linux下c的代码原型如下:
#define _GNU_SOURCE

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

static volatile int event_fd;

static void handler(int signum, siginfo_t *si, void *data){
    event_fd = si->si_fd;
    printf("info size:%d, data:%d/n", sizeof(siginfo_t), sizeof(data));
}

int main(int argc, char **argv){
    struct sigaction action;
    int fd;
    action.sa_sigaction = handler;
    sigemptyset(&action.sa_mask);
    action.sa_flags = SA_SIGINFO;
    sigaction(SIGRTMIN+1, &action, NULL);
   
    fd = open("test", O_RDONLY);
    fcntl(fd, F_SETSIG, SIGRTMIN+1);
    fcntl(fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_MULTISHOT);

    fd = open(".", O_RDONLY);
    fcntl(fd, F_SETSIG, SIGRTMIN+1);
    fcntl(fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_MULTISHOT);
    while(1){
        pause();
        printf("got event on fd=%d/n", event_fd);
    }
}
 

我要使用python,可以使用嵌入式方式,如果不是嵌入式方式,将会有1个以上的进程出现。
//#define _GNU_SOURCE
#include "Python.h"
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

typedef void my_handler_t(int, int);
typedef my_handler_t * my_handler;

static void * current_handler;

static volatile int event_fd;
static void default_h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值