#include<sys/time.h>
#include<time.h>
#include <signal.h>
第一种:
void timer_handler(int m){
count++;
printf("m: %d count:%d \n",m,count);
}
void set_timer()
{
struct itimerval itv;
itv.it_value.tv_sec = 1; //timer will start after one seconds later
itv.it_value.tv_usec = 0;
itv.it_interval.tv_sec = 1; //run timer_handler at this time after timer started
itv.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL,&itv,NULL);
}
signal(SIGALRM,timer_handler);
set_timer();
第二种:使用:sigaction 结构体以及函数
//内核中的定义
typedef void __signalfn_t(int);
typedef __signalfn_t __user *__sighandler_t;
struct sigaction {
#ifndef __ARCH_HAS_IRIX_SIGACTION
__sighandler_t sa_handler;
unsigned long sa_flags;
#else
unsigned int sa_flags;
__sighandler_t sa_handler;
#endif
#ifdef __ARCH_HAS_SA_RESTORER
__sigrestore_t sa_restorer;
#endif
sigset_t sa_mask; /* mask last for extensibility */
};
//内核系统调用 sigaction函数:
asmlinkage long sys_sigaction(int, const struct old_sigaction __user *,
struct old_sigaction __user *);
执行函数:
int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
使用sigaction结构体和函数:
void map_timer_handler()
{
struct sigaction sig_act;
sig_act.sa_handler = timer_handler;
sigemptyset(&sig_act.sa_mask);
sig_act.sa_flags = 0;
sigaction(SIGALRM, &sig_act, NULL);
}
linux alarm 的使用
最新推荐文章于 2024-05-13 20:13:49 发布