Show Table of Contents
A.2. ウェイクアップ
多くのアプリケーションは、設定ファイルの変更を確認するためにスキャンします。多くの場合、スキャンは、たとえば毎分、決まった間隔で実行されます。スキャンによりディスクがスピンダウンから強制的にウェイクアップさせられるため、スキャンが問題になることがあります。最善策は、適切な間隔または適切な確認メカニズムを見つけるか、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;
}
この方法の利点は、さまざまな確認を実行できることです。
主な制限は、1 つのシステムで利用できる監視の数が限定されることです。この数は
/proc/sys/fs/inotify/max_user_watches から取得できます。この数値を変更することは可能ですが、推薦されません。また、inotify が失敗すると、コードは別の確認方法にフォールバックする必要がありますが、これは通常ソースコードで #if #define を数多く使用することを意味します。
inotify の詳細については、inotify(7) man ページを参照してください。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.