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 <signal.h>
#include <unistd.h>

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 <setjmp.h>
#include <signal.h>
#include <unistd.h>
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 <apue.h>

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.

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值