wait()/alarm() race condition

Sean Hamilton sh at bel.bc.ca
Sun Mar 30 16:36:25 PST 2003


[asked in comp.unix.programmer without much luck]

Greetings,

I have a loop which calls wait(), and I want another function to be called
as close to once per minute as possible. Pseudo code:

int alarmed = 0;

void handle_sigalrm (int sig)
{
    alarmed = 1;
}

while (1)
{
    alarmed = 0;
    alarm (60);

    while (1)
    {
        wait (...);

        if (alarmed || wait was interrupted)
        {
            break;
        }
    }

    /* minutely code */
}

My concern is there is a small possibility that the alarm signal is
delivered after the if() but before the wait. So it is possible that this
wait takes several minutes or longer.

Moving the "if (alarmed)" above the wait() makes no difference, of course.

Is there a better way of doing something like this? Ideally wait() has a
timeout parameter, but, no such luck.

sh


More information about the freebsd-hackers mailing list