
inotify_read() 함수는 inotify 이벤트를 읽어오는 함수입니다. 이 함수의 반환값은 다음과 같은 정보를 포함합니다.
- 이벤트 유형 (EVENT_TYPE)
- 변경된 파일의 inode 번호 (ino)
- 변경된 파일의 파일 디스크립터 (fd)
- 이벤트 발생 시각 (tv_sec, tv_usec)
- 이벤트 발생 위치 (name_len, name)
inotify_read() 함수를 사용하여 inode 번호를 얻을 수 있는 방법은 다음과 같습니다.
1. inotify_init() 함수를 사용하여 inotify 파일 디스크립터를 생성합니다.
2. inotify_add_watch() 함수를 사용하여 파일이나 디렉터리를 감시합니다.
3. inotify_read() 함수를 사용하여 inotify 이벤트를 읽어옵니다.
4. 읽어온 이벤트의 inode 번호 (ino) 값을 얻습니다.
예를 들어, 다음과 같이 코드를 작성할 수 있습니다.
#hostingforum.kr
c
#include
#include
#include
#include
int main() {
int fd;
struct inotify_event *event;
char buf[4096];
// inotify 파일 디스크립터 생성
fd = inotify_init();
if (fd < 0) {
perror("inotify_init");
exit(1);
}
// 파일이나 디렉터리 감시
int wd = inotify_add_watch(fd, "/path/to/file", IN_MODIFY);
if (wd < 0) {
perror("inotify_add_watch");
exit(1);
}
// inotify 이벤트 읽기
while (1) {
int len = read(fd, buf, sizeof(buf));
if (len < 0) {
perror("read");
break;
}
// 이벤트 처리
event = (struct inotify_event *)buf;
if (event->mask & IN_MODIFY) {
printf("파일이 수정되었습니다. inode 번호: %dn", event->ino);
}
}
// inotify 파일 디스크립터 닫기
close(fd);
return 0;
}
이 코드는 inotify_read() 함수를 사용하여 inode 번호를 얻는 방법을 보여줍니다.
2025-03-24 07:35