#include<stdio.h> #define _GNU_SOURC #include<fcntl.h> #include<stdlib.h> #include<signal.h> #include<unistd.h> static void handler_d(int sig,siginfo_t *s,void* data) { int even_fd,even_pid; even_fd=s->si_fd; even_pid=s->si_pid; printf("process:%d,has deleted something from fd:%d\n",even_pid,even_fd); } static void handler_m(int sig,siginfo_t *s,void* data) { int even_fd,even_pid; even_fd=s->si_fd; even_pid=s->si_pid; printf("process:%d,has motified something from fd:%d\n",even_pid,even_fd); } int main() { struct sigaction act_d,act_m; int fd1,fd2; pid_t pid; pid=fork(); if(pid<0) { printf("error\n"); exit(1); } else if(pid==0) {//child fd1=open("/home/xiaozhi",O_RDONLY); if(fd1==-1) { printf("open /home/xiaozhi fail\n"); } fd2=open("/home/rz",O_RDONLY); if(fd2==-1) { printf("open /home/rz fail\n"); } act_m.sa_sigaction=handler_m; sigemptyset(&act_m.sa_mask); act_m.sa_flags=SA_SIGINFO; sigaction(SIGRTMIN+2,&act_m,NULL); fcntl(fd1,F_SETSIG,SIGRTMIN+2); fcntl(fd1,F_NOTIFY,DN_MODIFY|DN_MULTISHOT); fcntl(fd2,F_SETSIG,SIGRTMIN+2); fcntl(fd2,F_NOTIFY,DN_MODIFY|DN_MULTISHOT); while(1) { pause(); } } else { //father fd1=open("/home/xiaozhi",O_RDONLY); if(fd1==-1) { printf("open /home/xiaozhi fail\n"); } fd2=open("/home/rz",O_RDONLY); if(fd2==-1) { printf("open /home/rz fail\n"); } act_d.sa_sigaction=handler_d; sigemptyset(&act_d.sa_mask); act_d.sa_flags=SA_SIGINFO; sigaction(SIGRTMIN+1,&act_d,NULL); fcntl(fd1,F_SETSIG,SIGRTMIN+1); fcntl(fd1,F_NOTIFY,DN_MODIFY|DN_MULTISHOT); fcntl(fd2,F_SETSIG,SIGRTMIN+1); fcntl(fd2,F_NOTIFY,DN_MODIFY|DN_MULTISHOT); while(1) { pause(); } } }
时间: 2024-10-10 10:19:38