C함수 지정 시간에 SIGALRM 전송 alarm()
지정한 시간이 되면 자기 자신에게 SIGALRM이 전송합니다. 프로세스는 SIGALRM을 받으면 종료되므로 유의해야 합니다. 알람처럼 주기적으로 호출 받으려면 사용자 시그널 핸들을 사용하면 됩니다.
- 헤더: unistd.h
- 형태: unsigned int alarm(unsigned int seconds)
- 인수: unsigned int seconds seconds 초 후에 시그널 전송
- 반환: unsigned int 0 == 이전에 설정한 알람 없음
0 < 이전에 alarm() 호출로 시그널이 발생하기 까지 남은 시간
C언어 alarm() 함수 예제 1
3초 후에 SIGALARM 호출합니다.
#include <stdio.h>
#include <unistd.h>
int main( void)
{
int count = 0;
alarm( 3);
while( 1){
printf( "count %d\n", count++);
sleep( 1);
}
}
C언어 alarm() 예제 1 실행 결과
]$ ./a.out
count 0
count 1
count 2
자명종 시계
]$
C언어 alarm() 함수 예제 2
아래의 예제는 SIGALRM로 종료되지 않고 알람처럼 시그널 핸들 함수를 호출하는방법입니다.
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int counter = 0;
void sigint_handler( int signo)
{
printf( "알람 발생 %d!!\n", counter++);
alarm( 1);
}
int main( void)
{
signal( SIGALRM, sigint_handler);
alarm( 1 );
while( 1 )
;
}
C언어 alarm() 예제 2 실행 결과
]$ ./a.out
알람 발생 0!!
알람 발생 1!!
알람 발생 2!!
알람 발생 3!!
알람 발생 4!!
알람 발생 5!!
알람 발생 6!!
^C
]$
'컴퓨터 > 프로그래밍' 카테고리의 다른 글
C언어 시그널 대기 상태로 설정 함수 sigpromask() (0) | 2020.03.16 |
---|---|
C언어 프로세스 자신에게 시그널 전송 함수 raise() (0) | 2020.03.15 |
C언어 시그널을 수신할 때까지 대기 함수 pause() (0) | 2020.03.15 |