linux中使用定時器

2021-06-25 23:04:26 字數 1394 閱讀 9884

1.使用14號訊號sigalrm,呼叫alarm函式

#include #include #include #include #include void handle(int signum)

int main(int argc, const char *argv)

return 0;

}

//每隔3秒向自身傳送乙個sigalrm訊號,signal收到 sigalrm訊號後呼叫handle進行處理

--》alarm只能精確到秒

2.setitimer  -->也是通過向自身傳送訊號進行處理的,不過可以精確到毫妙級別

int setitimer(int which, const struct itimerval *new_value,

struct itimerval *old_value);

itimer_real    decrements in real time, and delivers sigalrm upon expiration.

itimer_virtual decrements  only  when  the  process  is executing, and delivers

sigvtalrm upon expiration.

struct itimerval ;

struct timeval ;

--》3種模式產生3中不同的訊號,使用方法類似

在該例子中,每隔一秒發出乙個sigalrm,每隔0.5秒發出乙個sigvtalrm訊號:

#include #include #include #include #include #include int sec;

void sigroutine(int signo)

fflush(stdout);

return;

}int main()

3.timerfd_create   timerfd_settime   -->通過時間設定fd可讀,然後通過select輪詢,呼叫處理函式

#include #include #include #include #include #include #include #include #define err_exit(m) \

do while(0)

int main(int argc, const char *argv)

printf("come\n");}}

return 0;

}

4.直接使用select   -->精確到毫秒級別

int mssleep(long ms)

其他資料:



linux核心中使用定時器

一 使用linux 核心定時器 linux 核心定時器採用系統時鐘來實現,linux 核心定時器使用很簡單,只需要提供超時時間 相當於定時值 和定時處理函式即可,當超時時間到了以後設定的定時處理函式就會執行,和我們使用硬體定時器的套路一樣,只是使用核心定時器不需要做一大堆的暫存器初始化工作。在使用核...

Springboot中使用定時器

1 在入口類中加入 enablescheduling註解 enablescheduling public static void main string args throws exception 2 建立定時器類,類中的方法註解了 scheduled就是乙個定時器 方式一 component pu...

Dorado中使用定時器

本篇主要介紹在dorado中如何使用定時器。1.先實現initializingbean介面,為billjobpr類新增定時任務,quartzmanager只是個工具類,下面也提供了addjob方法。component public class initservlet implements initi...