在下面的程序中,暂停被中断一次,但之后暂停永远不会返回。我已经设置了闹钟来中断暂停,所以我很困惑为什么暂停永远不会返回?
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
static void sig_alrm(int);
static jmp_buf env_alrm;
int main(int arc, char **argv)
{
int x;
x = setjmp(env_alrm);
printf("setjmp was created with return value: %d\n", x);
if(signal(SIGALRM, sig_alrm) == SIG_ERR)
{
printf("Error settting SIGALRM\n");
exit(1);
}
if((x!= 0) && (x!=1))
{
printf("Error setting setjmp\n");
exit(1);
}
printf("Line next to setjmp\n");
x = alarm(2);
printf("Alarm set for 2 seconds, remaning secs from previous alarm: %d\n");
pause();
printf("Line next to pause()\n");
alarm(0);
return 0;
}
static void sig_alrm(int signo)
{
longjmp(env_alrm, 1);
}
这是输出,最后一行显示应用程序暂停的位置
setjmp was created with return value: 0
Line next to setjmp
Alarm set for 2 seconds, remaining secs from previous alarm: 0
setjmp was created with return value: 1
Line next to setjmp
Alarm set for 2 seconds, remaining secs from previous alarm: 0