3. Timer and Alarm

System Program/time 2014. 2. 24. 08:35

1. 개념

 

Timer 및 Alarm은 특정 시간에 Process에게 어떤 Job을 수행하도록 Schedule을 한다.

여기서는 전통적인 시스템 콜 함수에 대해서 알아본다.

 

2. Timer Interfaces

 

#include <sys/time.h>

int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value); 

 

1) which 설정 가능 값

    - ITIMER_REAL : 실제 시간 (벽시계 개념)

    - ITIMER_VIRTUAL

    - ITIMER_PROF

2) struct itimerval 구조체

{

struct timeval it_interval;

struct timeval it_value;

}

               성공하면 '0'을 리턴, 에러가 발생하면 '-1'을 리턴  

 

2.1. 일회성 Timer 설정법

 

new_value에서 struct timeval it_interval 값이 '0'으로 설정

 

2.2. 주기적 Timer 설정

 

new_value에서 struct timeval it_interval 값에 어떤 주기로 반복할 것인지에 대한 초 값을 적어준다.

 

new_value에서 struct it_value는 timer에 대한 expire되어야 할 시간값을 설정한다.

 

2.3. 일회성 Timer 설정에 대한 다른 방법

 

아래와 같이 간단한 함수를 사용하면 위의 setitmer를 흉내낼 수 있다. 일회성 Timer인 경우는 이것이 더 쉽다.

 

#include <unistd.h>

unsigned int alarm(unsigned int seconds); 

항상 성공, 이전에 남은 시간값을 리턴   

 

3. 예제

 

/*
* main.c -- C Test App.
*
* Copyright (C) 2012-2013, 2013 heesoon.kim <chipmaker.tistory.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>

static void signal_handler(int sig)
{
	printf("%s : signal received \n", strsignal(sig));
	
}

int main(int argc, char *argv[])
{
	int retval = -1;
	struct sigaction sa;
	struct itimerval itv = {0};
	struct itimerval curitv = {0};
	
	itv.it_interval.tv_sec = 2;
	itv.it_value.tv_sec = 2;
	
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = signal_handler;
	//sa.sa_flags = SA_RESETHAND;
	if(sigaction(SIGALRM, &sa, NULL) == -1)
		perror("sigaction");
	
 	printf("START Timer \n");
	
	setitimer(ITIMER_REAL, &itv, NULL);
	//alarm(2);
	while(1)
	{	
		if(getitimer(ITIMER_REAL, &curitv) == -1)
		{
			perror("getitimer");
			break;
		}
		printf("Remained secs : %ld\n", curitv.it_value.tv_sec);
		sleep(1);
	}
	
	printf("End Timer \n");
	return 0;
}


위 예제에서 Timer가 설정값에 도달하여 Event가 발생하면 기본 동작은 프로세서 종료이다.

위의 예제는 시그널 핸들러를 설치하여 시그널을 가로챘으며 SA_RESETHAND(SA_ONESHOT)를 설정하면

주기적 Timer에서 두 번째 시그널에 대해서 기본값으로 동작하여 프로세서가 종료한다.

 

같이 동작이지만 주석처린된 alarm(2)함수를 사용하면 동작은 같다.

 

위의 예제에서 itv.it_interval.tv_sec = 2로 설정하는 부분이 없으면 일회성 Timer로 동작한다.

 

4. 결론

 

일반적으로 Timer의 구현은 어떤 식으로 할 것인가?

위의 함수를 사용할까?

Timer를 처리하는 task를 통해서 모든 처리를 수행하고 Queu에 넣어서 관리 추적하는 기법도 있을 수 있다.

 

 

'System Program > time' 카테고리의 다른 글

1. time_t 시간  (0) 2014.02.21
2. 시간의 개념  (0) 2014.02.21
: