APUE - The implementations of sleep()

Note: All statements of my understanding on APUE are based on its 3rd edition.

APUE shows 3 distinct implementations of sleep() in chapter 10. Although only the last one is reliable, yet I note all three of them, in order to gain an adequate understanding of the system call sleep().

The Prototype of sleep()

Figure 10-7 provides a naive implementation of sleep(). It uses alarm() and pause(). It seems to satisfy the requirements of this function, yet it fails in sophisticated runtime environments.

#include 
#include 

static void sig_alarm(int signo)
{
    // do nothing
    // just return from the handler
    // to wake up the pause
}

unsigned int sleep1(unsigned int seconds)
{
    signal(SIGALRM, sig_alrm);
    alarm(seconds);
    pause();
    return(alarm(0)); // turn off timer and return unslept time
}

Why naive

This code is “naive” because:
1. If an alarm is already set before sleep, this alarm will be reset by the alarm inside sleep1();
2. This code modifies the action of SIGALRM and forgets to recover it.
3. There exists a race codition between the call of alarm() and pause(). Suppose this code is running in an extremely busy system: the alarm clock may expire before the call of pause(), which results in the permanent blockage of the caller of sleep1().

The SVR2 Implementation of sleep()

The SVR2 version of sleep() relies on setjmp and longjmp in order to prevent the race condition in the previous example.
To reduce the reading complexity, this code does not handle the first and second issues mentioned above.

#include 
#include 
#include 
static jmp_buf env_alrm;
static void sig_alrm(int signo)
{
    longjmp(env_alrm, 1);
}

unsigned int sleep2(unsigned int sec)
{
    signal(SIGALRM, sig_alrm);
    if (setjmp(env_alrm)==0)
    {
        // start the timer
        alarm(sec);
        // wait until next signal
        pause();
    }
    // turn off the timer, return unslept time
    return alarm(0);
}

The function sleep2() successfully avoids the race condition between alarm() and pause(): Even though pause() is not executed, sleep2() can still return when SIGALRM arises.
Obviously, when SIGARLM arises, the longjmp will help the program go out of the if-block.
However, there is a chance that the longjmp interrupt another signal handler. Recall section 10.6 of APUE:

if the signal handler did not RETURN, the data structures it involves may be PARTIALLY UPDATED.
So this implementation is unsafe with the existence other signal handlers.

The POSIX.1 implementation of sleep()

This version will not bring unexpected side effects to other signal handlers, nor will it suffer a race condition. Yet is still affects previously-set timers, because there is no explicit definition in POSIX.1.

#include 

static void sig_alrm(int signo)
{
    // do nothing
    // just return to wake up sigsuspend()
}

unsigned int sleep(unsigned int seconds)
{
    struct sigaction  newact,  oldact;
    sigset_t          newmask, oldmask, suspmask;
    unsigned int      unslept;

    /*
     * set the handler and save previous handler
     */
    newact.sa_handler = sig_alrm;
    sigemptyset(&newact.sa_mask);
    newact.sa_flags = 0;
    sigaction(SIGALRM, &newact, &oldact);

    /*
     * block SIGALRM and save current signal mask
     * in order to avoid incoming SIGALRM before starting the timer
     */
    sigemptyset(&newmask);
    sigaddset(&newmask, SIGALRM);
    sigprocmask(SIG_BLOCK, &newmask, &oldmask);

    /*
     * start the timer
     * NOTE: any previously-set timer becomes useless here
     */
    alarm(seconds);
    suspmask = oldmask;

    /*
     * unblock SIGALRM 
     */
    sigdelset(&suspmask, SIGALRM);

    /*
     * wait for SIGALRM 
     */
    sigsuspend(&suspmask);
    // after sigsuspend(), the signal mask of current process
    // recovers to newmask, which blocks SIGALRM again

    unslept = alarm(0);

    /*
     * reset previous handler, signal mask
     */
    sigaction(SIGALRM, &oldact, 0);
    sigprocmask(SIG_SETMASK, &oldmask, 0);

    return unslept;
}

This code uses no “jump”, so it has no effect on other signal handlers when handling SIGALRM.

你可能感兴趣的:(Unix编程)