linux \u5185\u6838\u5b9a\u65f6\u5668<\/strong>\uff0c\u5426\u5219\u8fd4\u56de0\uff1b<\/p>\nint del_timer(struct timer_list * timer)<\/p>\n
\u5220\u9664\u5b9a\u65f6\u5668\u3002<\/p>\n
3. Timer\u65f6\u95f4\u5355\u4f4d<\/p>\n
\u5728\u5185\u6838\u7684\u914d\u7f6e\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230CONFIG_HZ\u7684\u914d\u7f6e\uff0c\u5982\u4e0b\uff1a<\/p>\n
\u8fd9\u8868\u793a\u5185\u6838\u6bcf\u79d2\u4e2d\u4f1a\u53d1\u751f 100 \u6b21\u7cfb\u7edf\u6ef4\u7b54\u4e2d\u65ad(tick)\uff0c\u8fd9\u5c31\u50cf\u4eba\u7c7b\u7684\u5fc3\u8df3\u4e00\u6837\uff0c\u8fd9\u662f Linux \u7cfb\u7edf\u7684\u5fc3\u8df3\u3002\u6bcf\u53d1\u751f\u4e00\u6b21 tick \u4e2d\u65ad\uff0c\u5168\u5c40\u53d8\u91cf jiffies \u5c31\u4f1a\u7d2f\u52a0 1\u3002<\/p>\n
CONFIG_HZ=100 \u8868\u793a\u6bcf\u4e2a\u6ef4\u7b54\u662f 10ms\u3002<\/p>\n
\u5b9a\u65f6\u5668\u7684\u65f6\u95f4\u5c31\u662f\u57fa\u4e8e jiffies \uff0c\u6211\u4eec\u4fee\u6539\u8d85\u65f6\u65f6\u95f4\u65f6\uff0c\u4e00\u822c\u4f7f\u7528\u8fd9 2 \u79cd\u65b9\u6cd5\uff1a<\/p>\n
\n
mod_timer<\/span>(<\/span>&<\/span>timer,<\/span> jiffies +<\/span> xxx)<\/span>;<\/span> \/\/ xxx \u8868\u793a\u591a\u5c11\u4e2a\u6ef4\u7b54\u540e\u8d85\u65f6\uff0c\u4e5f\u5c31\u662f xxx*10ms<\/span>\nmod_timer<\/span>(<\/span>&<\/span>timer,<\/span> jiffies +<\/span> 2<\/span>*<\/span>HZ)<\/span>;<\/span> \/\/ HZ \u7b49\u4e8e CONFIG_HZ\uff0c2*HZ \u5c31\u76f8\u5f53\u4e8e 2 \u79d2<\/span>\n<\/code><\/pre>\n<\/p>\n4. Timer Demo\u6d4b\u8bd5\u9a8c\u8bc1<\/p>\n
\u4e0b\u9762Demo\u4f7f\u7528\u4e862\u79cd\u65b9\u6cd5\u521d\u59cb\u5316Timer\uff1a<\/p>\n
\u65b9\u6cd5\u4e00\uff1atimer_setup(&test_timer1, kernel_timer_expires, 0);\u65b9\u6cd5\u4e8c\uff1aDEFINE_TIMER(test_timer2, kernel_timer_expires);<\/p>\n
\u4f7f\u7528mod_timer\u4fee\u6539timer\u8d85\u65f6\u95f4\u5e76\u542f\u52a8\uff0c\u5728\u4e0d\u4f7f\u7528\u7684\u65f6del_timer\u5220\u9664Timer\u3002<\/p>\n
\n
#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n\n#<\/span>include<\/span> <\/span><\/span>\nstatic<\/span> void<\/span> kernel_timer_expires<\/span>(<\/span>struct<\/span> timer_list<\/span> *<\/span>t)<\/span>;<\/span>\nstatic<\/span> struct<\/span> timer_list<\/span> test_timer1;<\/span>\n\/\/\u521d\u59cb\u5316timer2<\/span>\nstatic<\/span> DEFINE_TIMER<\/span>(<\/span>test_timer2,<\/span> kernel_timer_expires)<\/span>;<\/span>\nstatic<\/span> void<\/span> kernel_timer_expires<\/span>(<\/span>struct<\/span> timer_list<\/span> *<\/span>t)<\/span>\n{<\/span>\n\tif<\/span>(<\/span>t ==<\/span> &<\/span>test_timer1)<\/span>{<\/span>\n\t\tprintk<\/span>(<\/span>\"Timer 1 is comingrn\"<\/span>)<\/span>;<\/span>\n\t\tmod_timer<\/span>(<\/span>&<\/span>test_timer1,<\/span> jiffies +<\/span> 1<\/span>*<\/span>HZ)<\/span>;<\/span> \/\/ \u91cd\u65b0\u8bbe\u7f6etimer1 1s timeout<\/span>\n\t}<\/span>else<\/span> if<\/span>(<\/span>t ==<\/span> &<\/span>test_timer2)<\/span>{<\/span>\n\t\tprintk<\/span>(<\/span>\"Timer 2 is comingrn\"<\/span>)<\/span>;<\/span>\n\t\tmod_timer<\/span>(<\/span>&<\/span>test_timer2,<\/span> jiffies +<\/span> 2<\/span>*<\/span>HZ)<\/span>;<\/span> \/\/ \u91cd\u65b0\u8bbe\u7f6etimer2 2s timeout<\/span>\n\t}<\/span>else<\/span>