inotify로 프로그램을 작성하여 변경 사항을 파악하기 위해 루프에서 파일 변경을 확인하십시오. 하지만이 방법이 더 효과적 일 수 있다고 생각합니다. 누구나이 코드를 더 잘 쓸 수 있습니까?inotify로 프로그램 최적화
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/inotify.h>
#include <sys/select.h>
#define EVENT_SIZE (sizeof (struct inotify_event))
#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16))
int event_check (int fd)
{
fd_set rfds;
FD_ZERO (&rfds);
FD_SET (fd, &rfds);
/* Wait until an event happens or we get interrupted
by a signal that we catch */
return select (FD_SETSIZE, &rfds, NULL, NULL, NULL);
}
int main()
{
int length, i = 0;
int fd;
int wd;
while(1){
i=0;
fd = inotify_init();
if (fd < 0) {
perror("inotify_init");
}
wd = inotify_add_watch(fd, "/tmp/test", IN_CLOSE_WRITE);
if (event_check (fd) > 0)
{
char buffer[EVENT_BUF_LEN];
int count = 0;
length = read(fd, buffer, EVENT_BUF_LEN);
if (length < 0) {
perror("read");
}
while (i < length) { struct inotify_event *event = (struct inotify_event *) &buffer[ i ];
printf("New file %s Editted.\n", event->name);
i += EVENT_SIZE + event->len;
}
}
}
inotify_rm_watch(fd, wd);
close(fd);
}
코드가 더 좋다고 말하면 무슨 뜻인지 정의 할 수 있습니까? 어떤면에서 더 좋습니까? –
더 잘 쓸 수 있습니까? 가능성이 매우 높다. 당신이 원했던 무엇인가 특별히 있습니까? –
코드를 제대로 다시 열면 "더 좋게"됩니다. – bstpierre