#include <signal.h>
#include <unistd.h>
static void sig_alrm(int signo) {
printf("sig_alrm!!\n");
return;
}
unsigned int sleep1(unsigned int nsecs) {
if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
return(nsecs);
}
alarm(nsecs);
pause();
return(alarm(0));
}
int main() {
sleep1(10);
return 0;
}
=> alarm에서 pause 에서 넘어가지 전에 signal이 날라올 수 있기 때문에..
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
static jmp_buf env_alrm;
static void sig_alrm(int signo) {
printf("longjmp before\n");
longjmp(env_alrm, 1);
printf("longjmp after\n");
}
unsigned int sleep2(unsigned int nsecs) {
if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
return nsecs;
}
printf("0\n");
if (setjmp(env_alrm) == 0) {
printf("1\n");
alarm(nsecs);
printf("2\n");
pause();
printf("3\n");
}
printf("sleep2 end");
return (alarm(0));
}
int main() {
sleep2(5);
return 1;
}