c alarm() and pause() causing pause forever

c

Solution

use `sigsetjmp()` and `siglongjmp()` instead, to save and restore the signal masks, which are not saved by default in Linux, to clear any pending signals, from man setjmp():

POSIX does not specify whether setjmp() will save the signal mask. In System V it will not.By default, Linux/glibc follows the System V behavior. If you want to portably save and restore signal masks, use sigsetjmp() and siglongjmp().

Note: I'm not sure what you're trying to accomplish, but your code looks like it's supposed to run in an infinite loop, calling `longjmp()` restores execution as if it had just returned from `setjmp()` and it goes on forever.

Problem

In the following program, pause is interrupted once, but then pause never returns. I have set alarm to interrupt pause, so i am confused why pause never returns? ``` #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); } ``` Here is the output and the last line shows where the application pauses ``` 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 ```

Original source