tags:

views:

128

answers:

3

Hello,

I am using inotify with "interupt io" by setting the O_ASYNC flag on the inotify file descriptor and then using fcntl(inotifyfd, F_SETOWN, getpid()) coupled with a signal(sighandler, SIGIO) call, all from a single process, in order to set up an inotify file descriptor event handler.

The idea is to have inotify's file descriptor generate the SIGIO signal (via the O_ASYNC flag set) and have teh signal(..) registered handler handle the SIGIO signals as they are emitted thus avoiding polling the inotify file descriptor.

After the initial setup and signal handler setting the process has nothing to do and would normally exit.

I need the same process to remain idle after the setup as it acts as a daemon awaiting the SIGIO signals. This is where I am looking for help.

How can I idle the process to take the very least amount of cpu resources?

Steve

+1  A: 

One easy way is to just sleep(3) for a very large amount of time. Since signals wake up the process, you need to put the sleep in a loop:

while(1)
    sleep(1000);

The more correct way is probably to use sigsuspend(2):

sigsuspend(NULL);
Adam Rosenfield
A: 

Just loop on a sigsuspend(); , or even sleep() in this situation. Keep in mind though, that one SIGIO signal might one or more IO events are available.

nos
+2  A: 

If your process is going to remain idle until it sees inotify events, then you're severely overcomplicating things.

Just do a normal blocking read() on the inotify file descriptor. Your process will wake up when there's an inotify event. If you want to block-with-timeout, so you can periodically check for something else, use select().

In general, I find that if the answer is "signals" then you've probably asked the wrong question :)

caf