inotify를 테스트하고 싶었 기 때문에 인터넷에서 몇 가지 예제를 가져 와서 다양한 측면을 배우기 위해 수정했지만 실패한 것처럼 작동하지 않았습니다. 처음에는 꽤 잘 작동하는 디렉토리를 살펴 보았습니다. inotify api가 한 두 번보고 한 후에 작동을 멈 춥니 다.
그래서 내가 약간의 수정과 파일이 예를 확장하지만 한 번만 작동하고#include <sys/inotify.h>
#include <unistd.h>
#include <stdio.h>
#define EVENT_SIZE (sizeof (struct inotify_event))
#define BUF_LEN (16 * (EVENT_SIZE + 16))
int main()
{
int fd;
fd = inotify_init();
if (fd < 0)
perror("inotify_init()");
int wd;
wd = inotify_add_watch(fd, "target", IN_CLOSE_WRITE);
if (wd < 0)
perror("inotify_add_watch");
char buf[BUF_LEN];
int len;
while(1) {
len = read(fd, buf, BUF_LEN);
printf("after read\n");
if (len > 0)
{
int i = 0;
while (i < len)
{
struct inotify_event *event;
event = (struct inotify_event *) &buf[i];
printf("wd=%d mask=%x cookie=%u len=%u\n",
event->wd, event->mask,
event->cookie, event->len);
if (event->mask & IN_MODIFY)
printf("file modified %s", event->name);
if (event->len)
printf("name=%s\n", event->name);
i += EVENT_SIZE + event->len;
}
}
}
return 0;
}
그래서 읽기 기능이 차단됩니다, i)는 (선택 이동하지만, 여기 또한, 그것은 작동 한 번, 두 번 신고 한 다음 변경 사항을보고하지 않습니다.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#define EVENT_SIZE (sizeof (struct inotify_event))
#define BUF_LEN (1024 * (EVENT_SIZE + 16))
int main(int argc, char **argv)
{
int length, i = 0;
int fd;
int wd;
char buffer[BUF_LEN];
struct timeval timeout;
fd = inotify_init();
if (fd < 0) {
perror("inotify_init");
}
wd = inotify_add_watch(fd, "target",
IN_CLOSE_WRITE);
fd_set rfds,rfdss;
int ret;
/* zero-out the fd_set */
FD_ZERO (&rfds);
FD_ZERO (&rfdss);
FD_SET (fd, &rfds);
timeout.tv_sec = 5;
timeout.tv_usec = 0;
while(1){
printf("Before select\n");
//rfds = rfdss;
ret = select (fd + 1, &rfds, NULL, NULL, NULL);
printf("After Select\n");
timeout.tv_sec = 5;
timeout.tv_usec = 0;
if (ret < 0)
perror ("select");
else if (!ret){
}
/* timed out! */
else if (FD_ISSET (fd, &rfds)){
printf("file changed============\n");
length = read(fd, buffer, BUF_LEN);
}
}
(void) inotify_rm_watch(fd, wd);
(void) close(fd);
exit(0);
}