位置:海鸟网 > IT > linux/Unix >

高手进阶 Linux系统中内核定时器的应用

以下内容为程序代码

#include<linux/init.h>#include<linux/sched.h>#include<linux/module.h>#include<linux/jiffies.h>#include<asm/io.h>#include<linux/timer.h>#include<linux/wait.h>MODULE_LICENSE("GPL");static DECLARE_WAIT_QUEUE_HEAD(wq);static int flag = 0;struct timer_list my_timer;void timer_test(unsigned long data){ printk("this is a test\n"); flag = 1; wake_up_interruptible(&wq);}static int __init hello_init(void){ init_timer(&my_timer); my_timer.expires = jiffies + 5000; my_timer.data = 0; my_timer.function = timer_test; add_timer(&my_timer); wait_event_interruptible(wq, flag != 0); flag = 0; return 0;}static void __exit hello_exit(void){ del_timer(&my_timer); printk(KERN_ALERT "Goodbye\n");}module_init(hello_init);module_exit(hello_exit);