分类:
2008-12-08 12:19:59
Chapter 10 excerses
|
In , we showed a technique that's often used to set a timeout on an I/O
operation using setjmp and longjmp. The following code has also been seen:
signal(SIGALRM, sig_alrm); alarm(60); if
(setjmp(env_alrm) != 0) { /*
handle timeout */ ... } ... What else is wrong with this sequence of code? |
答案:alarm被调用之后,如果进程被schedule走了,当60秒钟后,alarm信号来了,handler被调用,handler里面会调用longjmp依据env_alrm的内容,跳转到指定位置,可是此时,setjmp还没有调用,因此这个跳转就是错误的。因此,这里面存在race condition,而且是造成错误的race condition,因此在要设置一个标记,来表明当前的env_alrm是否有了有效值,在handler里面要检查一下。