Hi there!

I am a student studying computer science.

시스템 프로그래밍

시스템 프로그래밍 11장 - Timer

만능성구 2020. 6. 11. 03:05
728x90
#include<sys/time.h>
int setitimer (int which, const struct itimerval *value, struct itimerval *oval);
interval time, 자기가 설정한 시간마다 event발생하도록 설정
주기적으로 signal이 생성된다.
which : timer type
ITMER_REAL : 실제시간 마다 
ITIMER_VIRTURE : user mode 시간
ITIMER_PROF : process running time user mode+kernel mode 
value : 설정할 시간 값 itemerval 
oval : 현재 설정된 timer interval 값
return 0 / -1
#include<sys/time.h>
int getitimer (int which, struct itimerval *oval);
현재 설정된 timer interval값 확인
which : timer type
ITMER_REAL : 실제시간 마다 
ITIMER_VIRTURE : user mode 시간
ITIMER_PROF : process running time user mode+kernel mode 
oval : 현재 설정된 timer interval 값
return 0 / -1

itimerval structure

struct itimerval {
	struct timeval it_interval; // periodic interval after the 1st alarm
	struct timeval it_value; // first interval
}
struct timeval {
	long tv_sec; // seconds
	long tv_usec; // micro seconds
}
/*
 it_interval = 0; // for one time itimer
 it_value = 0; // to turn off the itimer
*/

itimer.c

 

POSIX TIMER (Advanced)

-The POSIX Timer is a more advanced & controllable timer 

        for realtime applications

-See manual pages (e.g. $ man timer_create)

      • timer_create

      • timer_settime

      • timer_gettime

      • timer_getoverrun

      • timer_delete

Note

      • Instead of the SIGALRM, another signal can be specified to be used. (SIGRTMIN ~ SIGRTMAX)

      다른 signal들을 사용한다

      • Instead of a signal hander, a handler thread can be specified to be used.

      등록하는 방법이 sigaction을 통해서 한다.

      • at compile time, “-lrt” must be added. (rt library)

      -lrt해야 컴파일 가능

#include <sys/time.h>
int timer_create(clockid_t clockid, struct sigevent *restrict evp, timer_t *restrict timerid);
timer를 생성하는 함수
clockid : timer colock id 어떤 timer를 쓸거냐
CLOCK_REALTIME : real time 
CLOCK_MONOTONIC : 
...
evp : time event
timeid : 생성된 time id
return 0 / -1
#include <time.h>
int timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);
반환받은 timer id에 대해서 timer interval 시간을 설정한다.
timerid : timer id
flags : 특징 flag,  자세히 안볼게요.. 설정안할게요 ..
new_value : 설정값 (구조체 뒤에서 확인)
old_value : 기존의 값
return 0 / -1
#include <time.h>
int timer_gettime(timer_t timerid, struct itimerspec *curr_va lue);
현재 설정된 timer interval 값 확인
timerid : timer id
curr_va lue: 기존의 값
return 0 / -1

itimerspec structure

struct timespec {
	time_t tv_sec; /* Seconds */
	long tv_nsec; /* Nanoseconds */
};
struct itimerspec {
	struct timespec it_interval; /* Timer interval */
	struct timespec it_value; /* Initial expiration */
};
#include <signal.h>
int sigaction (int signo, const struct sigaction *act, struct sigaction *oldact);
POSIX Advence timer handler를 등록하는 함수
signo : signal number (min ~ max)
act : sigaction구조체에 데이터를 넣어서 등록한다.
oldact : 기존의 값
 
struct sigaction {
	void (*sa_handler)(int): /* address of handler */
	sigset_t sa_mask; /* signals blocked during handler invocation*/
	int sa_flags; /* flags controlling handler invocation */
	void (*sa_restorer)(void); /* NOT for application use */
};

 

posix-timer.c

728x90