您好,登錄后才能下訂單哦!
#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(); } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。