/**********************************************
* Author: lewiyon@hotmail.com
* File name: delay_wq.c
* Description: learn the function queue_delayed_work()
* Date: 2011-12-24
*********************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/workqueue.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
struct workqueue_struct *test_wq;
struct delayed_work test_dwq;
void delay_func(struct work_struct *work);
void delay_func(struct work_struct *work)
{
int i;
printk(KERN_INFO "My name is delay_func!\n");
for (i = 0; i < 3; i++) {
printk(KERN_ERR "delay_fun:i=%d\n", i);
msleep(1000);
}
}
static int __init example_init(void)
{
int i;
int ret;
test_wq = create_workqueue("test_wq");
if (!test_wq) {
printk(KERN_ERR "No memory for workqueue\n");
return 1;
}
printk(KERN_INFO "Create Workqueue successful!\n");
INIT_DELAYED_WORK(&test_dwq, delay_func);
ret = queue_delayed_work(test_wq, &test_dwq, 5000);
printk(KERN_INFO "first ret=%d!\n", ret);
for (i = 0; i < 3; i++) {
printk(KERN_INFO "Example:ret= %d,i=%d\n", ret, i);
msleep(100);
}
ret = queue_delayed_work(test_wq, &test_dwq, 0);
printk(KERN_INFO "second ret=%d!\n", ret);
return 0;
}
static void __exit example_exit(void)
{
int ret;
ret = cancel_delayed_work(&test_dwq);
flush_workqueue(test_wq);
destroy_workqueue(test_wq);
printk(KERN_INFO "Goodbay! ret=%d\n", ret);
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lewiyon <lewiyon@hotmail.com>");
运行结果:
Dec 24 20:50:40 RedHat kernel: Create Workqueue successful!
Dec 24 20:50:40 RedHat kernel: first ret=1!
Dec 24 20:50:40 RedHat kernel: Example:ret= 1,i=0
Dec 24 20:50:41 RedHat kernel: Example:ret= 1,i=1
Dec 24 20:50:41 RedHat kernel: Example:ret= 1,i=2
Dec 24 20:50:41 RedHat kernel: second ret=0!
Dec 24 20:50:43 RedHat kernel: Goodbay! ret=1
说明将任务添加到工作队列后,如果工作队列还在执行该任务,则queue_delayed_work()返回1,否则返回0,如上实例所述;
主线程delay_wq将任务添加到工作队列后,使得工作队列在延迟delay后执行函数delay_func(),而delay_wq线程继续执行;