Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 30 Mar 2003 16:36:21 -0800
From:      "Sean Hamilton" <sh@bel.bc.ca>
To:        <hackers@freebsd.org>
Subject:   wait()/alarm() race condition
Message-ID:  <001101c2f71d$8d9e4fb0$0300000a@slugabed.org>

next in thread | raw e-mail | index | archive | help
[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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?001101c2f71d$8d9e4fb0$0300000a>