检测创建文件或删除文件会报告:信号处理机制
2016-06-27 14:18
330 查看
#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(); } } }
相关文章推荐
- RJ-45接口信号定义
- linux下基于C语言的信号编程实例
- Android实现信号强度监听的方法
- perl中使用signal(信号)实例
- Linux线程同步之信号C语言实例
- 详解Python的Flask框架中的signals信号机制
- Shell脚本中信号处理实践
- Linux环境进程间通信(二): 信号(上)
- Python Signal(信号) 异步系统事件
- qt学习之路3 ---信号与槽机制
- WOC无线网络
- sem_wait sem_post信号量操作进本函数
- Linux下的信号机制
- 信号发送函数kill()和raise()
- 信号捕捉函数alarm()和pause()(转)
- 信号处理函数signal()和信号集函数组
- linux进程间通信方法汇总
- [转载]Linux 内核笔记 -- 信号
- pthread_kill 和 sigaction 函数
- Linux信号深度解剖