APUE - The implementations of sleep()
来源:互联网 发布:犀牛软件 视角 加速器 编辑:程序博客网 时间:2024/06/02 06:52
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.
- APUE - The implementations of sleep()
- state-of-the-art implementations related to visual recognition and search
- Comparison of different SQL implementations
- Three different implementations of MapReduce
- Comparison of Performance of Different poll implementations
- how to `make` the src code of APUE book
- Comparison of different SQL implementations(整理)
- Wrapper implementations in the java.util collections
- the diary of sleep jobs & fg command line in linux
- sleep的可靠实现(APUE)
- Summary of Implementations(Collection的实现总结)
- JavaScript implementations of standard and secure cryptographic algorithms
- What's New in the JMF 2.0 Reference Implementations
- Good Explain on Sleep in Kernel, let me understanding the mechanism of avoiding "missing wake"
- Resources and Implementations of Generative Adversarial Nets: GAN, DCGAN, WGAN, CGAN, InfoGAN
- Types of Edge Computing Implementations——边缘计算的实现
- apue
- APUE
- HashMap 理解
- b3log solo 试用日志
- poj 1845 Sumdiv
- canvas实战之一(画图,矩阵转换,定时)
- 抓取Java代码网络请求
- APUE - The implementations of sleep()
- 在测试spring mvc+junit4遇到的问题
- Android中hardware源码(android-5.0.2)
- [Codeforces - Gym100801H (NEERC)] Hash Code Hacker (字符串构造)
- web.xml
- String.split()应该怎么用?
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 link cut tree
- lvs+keepalived实现双机热备
- Codeforces #369(Div.2)B.Chris and Magic Square(矩阵找数)