linux schedule and queue

来源:互联网 发布:java元注解 编辑:程序博客网 时间:2024/04/26 04:32

http://blog.sina.com.cn/s/blog_78d30f6b0102uyaf.html

http://www.xuebuyuan.com/1198936.html

http://www.360doc.com/content/13/0411/11/11645800_277544630.shtml


1.使用内核提供的共享队列

#include<linux/module.h>#include<linux/init.h>#include<linux/kernel.h>#include<linux/types.h>#include<linux/kthread.h>#include<linux/wait.h>#include<linux/string.h>#include<linux/sysctl.h>#include<linux/workqueue.h>static void defense_work_handler(struct work_struct *work);static DECLARE_DELAYED_WORK(defense_work, defense_work_handler);static void defense_work_handler(struct work_struct *work){        printk(KERN_INFO "defense_work_handler function.\n");        schedule_delayed_work(&defense_work, msecs_to_jiffies(5000));}static int __init main_init(void){        schedule_delayed_work(&defense_work, 1 * HZ);        return 0;}static void __exit main_exit(void){        cancel_delayed_work_sync(&defense_work);}module_init(main_init);module_exit(main_exit);MODULE_LICENSE("GPL");

2.使用自定义工作队列

#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_workqueue;struct delayed_work test_delaywork;void delay_func(struct work_struct *work){        printk(KERN_ALERT "delay_func() called...\n");        queue_delayed_work(test_workqueue, &test_delaywork, msecs_to_jiffies(5000));}static int __init example_init(void){        test_workqueue = create_workqueue("test_workqueue");        if (!test_workqueue)                return 1;        INIT_DELAYED_WORK(&test_delaywork, delay_func);        queue_delayed_work(test_workqueue, &test_delaywork, 500);        return 0;}static void __exit example_exit(void){        int ret;        ret = cancel_delayed_work(&test_delaywork);        flush_workqueue(test_workqueue);        destroy_workqueue(test_workqueue);        printk(KERN_INFO "Goodbye! ret=%d\n", ret);}module_init(example_init);module_exit(example_exit);MODULE_LICENSE("GPL");





0 0
原创粉丝点击