alarm과 sleep
2019. 7. 24. 12:15ㆍ임베디드/리눅스시스템프로그래밍
"alarm이 울리면 시스템이 sleep 상태에 있다면 깨어나게 된다, 한 번 깨어난 프로세스는 다시 잠들지 않는다"
아래 코드는 타임아웃 signal handler에 들어가게 되면 sleep 상태에 있는 프로세스는 깨어나면서 다시 잠들지 않아서 alarm 3번에 프로그램이 종료된다
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if 0 | |
SYNOPSIS | |
#include <signal.h> | |
typedef void (*sighandler_t)(int); | |
sighandler_t signal(int signum, sighandler_t handler); | |
시그널 함수는 void 반환형에 signum을 매개변수로 하는 함수로 작성하면 된다 | |
#endif | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <signal.h> | |
void timeout(int signum) | |
{ | |
if(signum == SIGALRM) { | |
printf("Time out\n"); | |
alarm(2); | |
} | |
} | |
void keycontrol(int signum) | |
{ | |
if(signum == SIGINT) { | |
printf("CTRL+C pressed\n"); | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
int ret; | |
/* 시그널 등록 */ | |
signal(SIGALRM, timeout); | |
signal(SIGINT, keycontrol); | |
ret = alarm(2); | |
if(ret > 0) printf("exist prev alarm\n"); | |
for(int i=0; i<3; i++) { | |
printf("SIGINT... wait\n"); | |
sleep(100); | |
} | |
return 0; | |
} |
'임베디드 > 리눅스시스템프로그래밍' 카테고리의 다른 글
세마포어 Semaphore (0) | 2019.07.25 |
---|---|
sigprocmask oldset (0) | 2019.07.24 |
wait와 waitpid (0) | 2019.07.23 |
프로세스 생성 fork (0) | 2019.07.23 |
read API (0) | 2019.07.22 |