A.2. ウェイクアップ
多くのアプリケーションは、設定ファイルの変更をスキャンします。多くの場合、スキャンは一定の間隔で、たとえば 1 分ごとに実行されます。ディスクがスピンダウンから強制的にウェイクアップするため、これは問題になる可能性があります。最善の解決策は、適切な間隔、適切なチェックメカニズムを見つけるか、inotify で変更をチェックしてイベントに反応することです。Inotify は、 ファイルまたはディレクトリーに対するさまざまな変更をチェックできます。
以下に例を示します。
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int fd;
int wd;
int retval;
struct timeval tv;
fd = inotify_init();
/* checking modification of a file - writing into */
wd = inotify_add_watch(fd, "./myConfig", IN_MODIFY);
if (wd < 0) {
printf("inotify cannot be used\n");
/* switch back to previous checking */
}
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(fd + 1, &rfds, NULL, NULL, &tv);
if (retval == -1)
perror("select()");
else if (retval) {
printf("file was modified\n");
}
else
printf("timeout\n");
return EXIT_SUCCESS;
}
このアプローチの利点は、実行できるさまざまなチェックです。
主な制限は、システムで使用できる監視の数が限られていることです。この番号は
/proc/sys/fs/inotify/max_user_watches から取得でき、変更することもできますが、これは推奨できません。さらに、inotify が 失敗した場合、コードは別のチェックメソッドにフォールバックする必要があります。これは通常、次のような問題が多数発生することを意味します。#if #defineソースコード内で。
inotify の詳細については、inotify(7)マニュアルページ。