#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();
}
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。