unsigned int alarm(unsigned int
seconds)函数,用来定时,当到达定时的时间后,内核会发送SIGALRM信号给进程,如果进程忽略该信号,该进程会终止,也可以自己定义SIGALRM信号的处理函数,当然是用的是signal()函数
void (*signal)(int signo, void (*func)(int)))(int
);
signo 指定要处理的信号,信号处理函数void func(),当要处理多个信号时候,func(int
sig)用sig参数传进信号,在func中判断信号类型,然后指定处理
alarm()函数并不轮询定时,因此可以在func()中收到SIGALRM后,在次设定alarm()函数
但是下面介绍的settimer()函数产生的定时信号讲会持续有效,
example:
#include
#include
#include
void fun(int sig);
int main()
{
alarm(3);
signal(SIGALRM,fun);
// pause(); //wait the signal
while(1); //always wait for signal
return 0;
}
void fun(int sig)
{
if(sig==SIGALRM)
printf("get timer
alarm!/n");
else
printf("get timer
failed!/n");
alarm(3);
}
仍旧有一个函数,settimer() 也是用来定时的,定时到了后内核会发出信号,
函数原型
#include
int do_setitimer(int which, struct itimerval *value, struct
itimerval *ovalue)
{
struct task_struct *tsk = current;
struct hrtimer *timer;
ktime_t expires;
cputime_t cval, cinterval, nval, ninterval;
check_itimerval(value);
switch (which) {
case ITIMER_REAL:
again:
spin_lock_irq(&tsk->sighand->siglock);
timer = &tsk->signal->real_timer;
if (ovalue) {
ovalue->it_value = itimer_get_remtime(timer);
ovalue->it_interval=
ktime_to_timeval_r(tsk->signal->it_real_incr);
}
if
(hrtimer_try_to_cancel(timer) < 0) {