检测创建文件或删除文件会报告:信号处理机制
#include<stdio.h>#define_GNU_SOURC#include<fcntl.h>#include<stdlib.h>#include<signal.h>#include<unistd.h>staticvoidhandler_d(intsig,siginfo_t*s,void*data){inteven_fd,even_pid;even_fd=s->si_fd;even_pid=s->si_pid;printf("process:%d,hasdeletedsomethingfromfd:%d\n",even_pid,even_fd);}staticvoidhandler_m(intsig,siginfo_t*s,void*data){inteven_fd,even_pid;even_fd=s->si_fd;even_pid=s->si_pid;printf("process:%d,hasmotifiedsomethingfromfd:%d\n",even_pid,even_fd);}intmain(){structsigactionact_d,act_m;intfd1,fd2;pid_tpid;pid=fork();if(pid<0){printf("error\n");exit(1);}elseif(pid==0){//childfd1=open("/home/xiaozhi",O_RDONLY);if(fd1==-1){printf("open/home/xiaozhifail\n");}fd2=open("/home/rz",O_RDONLY);if(fd2==-1){printf("open/home/rzfail\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{//fatherfd1=open("/home/xiaozhi",O_RDONLY);if(fd1==-1){printf("open/home/xiaozhifail\n");}fd2=open("/home/rz",O_RDONLY);if(fd2==-1){printf("open/home/rzfail\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();}}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。