https://sysdocu.tistory.com/1461
c언어 파일 이벤트 감시 (inotify)
맨 하단에 있는 Example code 를 확인합니다.해당 코드는 특정 디렉토리 내의 하위 디렉토리 및 파일에 대한 이벤트를 감지하여 줍니다.단일 파일에 대한 감시를 원할 경우 디렉토리를 만들어 그 안
sysdocu.tistory.com
리눅스에서 파일 변경 이벤트 감지하는 방법.
위 블로그에서 소개한 방법을 실습해 본다.
위 블로그에서 코드를 들고 온다.
내가 감시하고 싶은 파일 위치 find명령어로 찾은 후에 INOTIFY_PATH에 입력해주면 된다.
pi@raspberrypi:~/C_WorkPlace/fileEvent $ cat fileEvent.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <poll.h>
#include <errno.h>
#include <unistd.h>
#include <sys/inotify.h>
#define INOTIFY_PATH "/home/pi/C_WorkPlace/fileEvent/TestDir"
static void __handle_inotify_event(const struct inotify_event *event)
{
if (event->mask & IN_ACCESS)
printf("IN_ACCESS ");
if (event->mask & IN_ATTRIB)
printf("IN_ATTRIB ");
if (event->mask & IN_CLOSE_NOWRITE)
printf("IN_CLOSE_NOWRITE ");
if (event->mask & IN_CLOSE_WRITE)
printf("IN_CLOSE_WRITE ");
if (event->mask & IN_CREATE)
printf("IN_CREATE ");
if (event->mask & IN_DELETE)
printf("IN_DELETE ");
if (event->mask & IN_ISDIR)
printf("IN_ISDIR ");
if (event->mask & IN_MODIFY)
printf("IN_MODIFY ");
if (event->mask & IN_MOVE_SELF)
printf("IN_MOVE_SELF ");
if (event->mask & IN_MOVED_FROM)
printf("IN_MOVED_FROM ");
if (event->mask & IN_MOVED_TO)
printf("IN_MOVED_TO ");
if (event->mask & IN_OPEN)
printf("IN_OPEN ");
if (event->len > 0)
printf(": name = %s\n", event->name);
}
int main(int argc, char *argv[])
{
int ret;
int fd;
int wd;
char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
char *ptr;
ssize_t size;
const struct inotify_event *event;
fd = inotify_init();
if (fd < 0) {
perror("inotify_init");
exit(EXIT_FAILURE);
}
wd = inotify_add_watch(fd, INOTIFY_PATH, IN_MODIFY | IN_CREATE | IN_DELETE);
if (wd < 0) {
fprintf(stderr, "Failed to add watch [%s] [%s]", INOTIFY_PATH, strerror(errno));
perror("inotify_add_watch");
exit(EXIT_FAILURE);
}
while (1) {
size = read(fd, buf, sizeof(buf));
if (size == -1 && errno != EAGAIN) {
perror("read");
fprintf(stderr, "read : %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (size <= 0)
break;
for (ptr = buf; ptr < buf + size; ptr += sizeof(struct inotify_event) + event->len) {
event = (struct inotify_event *)ptr;
__handle_inotify_event(event);
}
}
ret = inotify_rm_watch(fd, wd);
if (ret < 0) {
fprintf(stderr, "Failed to rm watch [fd : %d] [wd : %d] [%s]", fd, wd, strerror(errno));
perror("inotify_rm_watch");
exit(EXIT_FAILURE);
}
return 0;
}
dubpicate session으로 하나는 .c 파일 이벤트 돌리고 하나는 파일 접근 해본다.
이제 위 블로그에서 설명한 Multiflexing I/O 가 먼지 살펴보자.
출처.
https://plummmm.tistory.com/68?category=960903
입출력 다중화 (I/O Multiplexing)
이번에 배울 개념은 입출력 다중화, I/O Multiplexing 이다. 멀티플렉싱의 사전적 의미를 보면, "하나의 통신채널을 통해서 둘 이상의 데이터(시그널)을 전송하는데 사용되는 기술" 이다. 다시 말하자
plummmm.tistory.com
'공부,일 > 라즈베리 파이' 카테고리의 다른 글
라즈베리 파이엣 c 언어 컴파일. (0) | 2023.03.31 |
---|---|
라즈베리 파이 to 아두이노 실습(2) (0) | 2021.08.13 |
라즈베리파이 to 아두이노 실습 (0) | 2021.08.13 |
라즈베리 파이로 아두이노 조작하기 (0) | 2021.08.13 |
TCP/IP 4 Layer 설명 (0) | 2021.08.13 |
댓글