linux2021. 7. 8. 15:37
#include <linux/types.h>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <poll.h>
#include <sys/epoll.h>
#include <pthread.h>


#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

#define SIGTIMER (SIGRTMAX)
#define ONESHOTTIMER (SIGRTMAX-1)

timer_t SetTimer(int signo, int sec, int mode);
timer_t timerid,oneshotTimer;

int count;

int main()
{
	timerid = SetTimer(SIGTIMER, 500, 1);
	//oneshotTimer = SetTimer(ONESHOTTIMER, 5000, 0);
	while(1){

        printf("sleep 5s count : %d\n",count);
        sleep(5);
        count++;
	    if (timerid == 0){
            printf("settimer again\n");
            timerid = SetTimer(SIGTIMER, 500, 1);
        } 
    };
	return 0;
}
static void timer_test_func(union sigval sigev_val)
{
    printf("[%d %s]\n",__LINE__,__func__);
    if ( count > 5){
        printf("timer_delete\n");
        timer_delete(timerid); 
        timerid = 0;
        count = 0;
    }
    return;
}


timer_t SetTimer(int signo, int sec, int mode)
{
	struct sigevent sigev;
	timer_t timerid;
	struct itimerspec itval;

	// Create the POSIX timer to generate signo
    memset(&sigev, 0, sizeof( struct sigevent));
	sigev.sigev_notify = SIGEV_THREAD;
    sigev.sigev_notify_function = timer_test_func;
	sigev.sigev_value.sival_ptr = &timerid;
    printf("timer_create\n");
	if (timer_create(CLOCK_REALTIME, &sigev, &timerid) == 0) {
		itval.it_value.tv_sec = 10;
		itval.it_value.tv_nsec = 0;
        itval.it_interval.tv_sec = 2;
        itval.it_interval.tv_nsec = 0;

        printf("timer_settime\n");
		if (timer_settime(timerid, 0, &itval, NULL) != 0) {
			perror("time_settime error!");
		}
        printf("timer_settime success\n");
	} else {
		perror("timer_create error!");
		return (timer_t)-1;
	}
	return timerid;
}

Timer test code in linux

'linux' 카테고리의 다른 글

custom vimrc  (0) 2022.07.05
git stash 참조  (0) 2022.06.30
Network interface Restart 시 주의 사항  (0) 2019.11.21
4.9.x driver 중, memcpy 시도 할 경우, kernel panic #PAN emulation  (0) 2019.10.15
Overview NEC protocol  (0) 2019.06.28
Posted by easy16